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
Provenance
- Commit
e39a6d9- Tree
08c6dd2- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
cf59703
1 file changed+69 added−69 removed
modified:tests/test_multipart.py+69 added-69 removed
6 unmodified lines7 import httpcore8 import pytest9 10 import httpx11-from httpx._content_streams import AsyncIteratorStream, MultipartStream, encode11+from httpx._content_streams import MultipartStream, encode12 from httpx._utils import format_form_param13 14 15-class MockTransport(httpcore.AsyncHTTPTransport):16- async def request(15+class 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", [], content33 34 35 @pytest.mark.parametrize(("value,output"), (("abc", b"abc"), (b"abc", b"abc")))36-@pytest.mark.asyncio37-async 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 == 20036+def test_multipart(value, output):37+ client = httpx.Client(transport=MockTransport())38 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)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 == 20044 55- # 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)54 55+ # 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>"]59 60+61 @pytest.mark.parametrize(("key"), (b"abc", 1, 2.3, None))62-@pytest.mark.asyncio63-async 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)62+def test_multipart_invalid_key(key):63+ client = httpx.Client(transport=MockTransport())64 65+ 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)74 75+76 @pytest.mark.parametrize(("value"), (1, 2.3, None, [None, "abc"], {None: "abc"}))77-@pytest.mark.asyncio78-async 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)77+def test_multipart_invalid_value(value):78+ client = httpx.Client(transport=MockTransport())79 80+ 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)85 87-@pytest.mark.asyncio88-async 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 == 20086 97- # 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)87+def test_multipart_file_tuple():88+ client = httpx.Client(transport=MockTransport())89 107- # 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 == 20096 97+ # 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)106 107+ # 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>"]111+112+113 def 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