Skip to content
Myr

Use sync client in test cases (#1241)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Use sync client in test cases * Use plain client __init__ style in preference to context manager

authored by Tom Christie · committed by GitHub · parent cf59703 · Browse files · 1 file+69 added−69 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Florimond Manca <florimond.manca@gmail.com>

Diff stats

1 file+69 added−69 removed

  • tests:+69 added−69 removed

1 file+69 added−69 removed

modified:tests/test_multipart.py+69 added-69 removed
6 unmodified lines
7import httpcore8import pytest910import httpx11from httpx._content_streams import AsyncIteratorStream, MultipartStream, encode11from httpx._content_streams import MultipartStream, encode12from httpx._utils import format_form_param131415class MockTransport(httpcore.AsyncHTTPTransport):16 async def request(15class MockTransport(httpcore.SyncHTTPTransport):16 def request(17 self,18 method: bytes,19 url: typing.Tuple[bytes, bytes, typing.Optional[int], bytes],20 headers: typing.List[typing.Tuple[bytes, bytes]] = None,21 stream: httpcore.AsyncByteStream = None,21 stream: httpcore.SyncByteStream = None,22 timeout: typing.Mapping[str, typing.Optional[float]] = None,23 ) -> typing.Tuple[24 bytes,25 int,26 bytes,27 typing.List[typing.Tuple[bytes, bytes]],28 httpcore.AsyncByteStream,28 httpcore.SyncByteStream,29 ]:30 assert stream is not None31 content = AsyncIteratorStream(aiterator=(part async for part in stream))31 content = httpcore.IteratorByteStream(iterator=(part for part in stream))32 return b"HTTP/1.1", 200, b"OK", [], content333435@pytest.mark.parametrize(("value,output"), (("abc", b"abc"), (b"abc", b"abc")))36@pytest.mark.asyncio37async def test_multipart(value, output):38 async with httpx.AsyncClient(transport=MockTransport()) as client:39 # Test with a single-value 'data' argument, and a plain file 'files' argument.40 data = {"text": value}41 files = {"file": io.BytesIO(b"<file content>")}42 response = await client.post("http://127.0.0.1:8000/", data=data, files=files)43 assert response.status_code == 20036def test_multipart(value, output):37 client = httpx.Client(transport=MockTransport())3845 # We're using the cgi module to verify the behavior here, which is a46 # bit grungy, but sufficient just for our testing purposes.47 boundary = response.request.headers["Content-Type"].split("boundary=")[-1]48 content_length = response.request.headers["Content-Length"]49 pdict: dict = {50 "boundary": boundary.encode("ascii"),51 "CONTENT-LENGTH": content_length,52 }53 multipart = cgi.parse_multipart(io.BytesIO(response.content), pdict)39 # Test with a single-value 'data' argument, and a plain file 'files' argument.40 data = {"text": value}41 files = {"file": io.BytesIO(b"<file content>")}42 response = client.post("http://127.0.0.1:8000/", data=data, files=files)43 assert response.status_code == 2004455 # Note that the expected return type for text fields56 # appears to differs from 3.6 to 3.7+57 assert multipart["text"] == [output.decode()] or multipart["text"] == [output]58 assert multipart["file"] == [b"<file content>"]45 # We're using the cgi module to verify the behavior here, which is a46 # bit grungy, but sufficient just for our testing purposes.47 boundary = response.request.headers["Content-Type"].split("boundary=")[-1]48 content_length = response.request.headers["Content-Length"]49 pdict: dict = {50 "boundary": boundary.encode("ascii"),51 "CONTENT-LENGTH": content_length,52 }53 multipart = cgi.parse_multipart(io.BytesIO(response.content), pdict)5455 # Note that the expected return type for text fields56 # appears to differs from 3.6 to 3.7+57 assert multipart["text"] == [output.decode()] or multipart["text"] == [output]58 assert multipart["file"] == [b"<file content>"]596061@pytest.mark.parametrize(("key"), (b"abc", 1, 2.3, None))62@pytest.mark.asyncio63async def test_multipart_invalid_key(key):64 async with httpx.AsyncClient(transport=MockTransport()) as client:65 data = {key: "abc"}66 files = {"file": io.BytesIO(b"<file content>")}67 with pytest.raises(TypeError) as e:68 await client.post(69 "http://127.0.0.1:8000/",70 data=data,71 files=files,72 )73 assert "Invalid type for name" in str(e.value)62def test_multipart_invalid_key(key):63 client = httpx.Client(transport=MockTransport())6465 data = {key: "abc"}66 files = {"file": io.BytesIO(b"<file content>")}67 with pytest.raises(TypeError) as e:68 client.post(69 "http://127.0.0.1:8000/",70 data=data,71 files=files,72 )73 assert "Invalid type for name" in str(e.value)747576@pytest.mark.parametrize(("value"), (1, 2.3, None, [None, "abc"], {None: "abc"}))77@pytest.mark.asyncio78async def test_multipart_invalid_value(value):79 async with httpx.AsyncClient(transport=MockTransport()) as client:80 data = {"text": value}81 files = {"file": io.BytesIO(b"<file content>")}82 with pytest.raises(TypeError) as e:83 await client.post("http://127.0.0.1:8000/", data=data, files=files)84 assert "Invalid type for value" in str(e.value)77def test_multipart_invalid_value(value):78 client = httpx.Client(transport=MockTransport())7980 data = {"text": value}81 files = {"file": io.BytesIO(b"<file content>")}82 with pytest.raises(TypeError) as e:83 client.post("http://127.0.0.1:8000/", data=data, files=files)84 assert "Invalid type for value" in str(e.value)8587@pytest.mark.asyncio88async def test_multipart_file_tuple():89 async with httpx.AsyncClient(transport=MockTransport()) as client:90 # Test with a list of values 'data' argument,91 # and a tuple style 'files' argument.92 data = {"text": ["abc"]}93 files = {"file": ("name.txt", io.BytesIO(b"<file content>"))}94 response = await client.post("http://127.0.0.1:8000/", data=data, files=files)95 assert response.status_code == 2008697 # We're using the cgi module to verify the behavior here, which is a98 # bit grungy, but sufficient just for our testing purposes.99 boundary = response.request.headers["Content-Type"].split("boundary=")[-1]100 content_length = response.request.headers["Content-Length"]101 pdict: dict = {102 "boundary": boundary.encode("ascii"),103 "CONTENT-LENGTH": content_length,104 }105 multipart = cgi.parse_multipart(io.BytesIO(response.content), pdict)87def test_multipart_file_tuple():88 client = httpx.Client(transport=MockTransport())89107 # Note that the expected return type for text fields108 # appears to differs from 3.6 to 3.7+109 assert multipart["text"] == ["abc"] or multipart["text"] == [b"abc"]110 assert multipart["file"] == [b"<file content>"]90 # Test with a list of values 'data' argument,91 # and a tuple style 'files' argument.92 data = {"text": ["abc"]}93 files = {"file": ("name.txt", io.BytesIO(b"<file content>"))}94 response = client.post("http://127.0.0.1:8000/", data=data, files=files)95 assert response.status_code == 2009697 # We're using the cgi module to verify the behavior here, which is a98 # bit grungy, but sufficient just for our testing purposes.99 boundary = response.request.headers["Content-Type"].split("boundary=")[-1]100 content_length = response.request.headers["Content-Length"]101 pdict: dict = {102 "boundary": boundary.encode("ascii"),103 "CONTENT-LENGTH": content_length,104 }105 multipart = cgi.parse_multipart(io.BytesIO(response.content), pdict)106107 # Note that the expected return type for text fields108 # appears to differs from 3.6 to 3.7+109 assert multipart["text"] == ["abc"] or multipart["text"] == [b"abc"]110 assert multipart["file"] == [b"<file content>"]111112113def test_multipart_encode(tmp_path: typing.Any) -> None:114 path = str(tmp_path / "name.txt")115 with open(path, "wb") as f:116 f.write(b"<file content>")
162 unmodified lines