test_multipart.py
This file
Path
tests/test_multipart.py
201 lines · 7.9 KiB · Python
Last changed by
Switch to private module names (#785)Florimond Mancabefore adoption82dc6f3
History of this fileTake it
The walk stopped after 200 commits: lines older than fc3df51 point at it.
Don't check trio import in definition of backend fixture params (#447)Florimond Mancabefore adoptionfc3df51
lines 1–8why, one step back1import binascii2import cgi3import io4import os5from unittest import mock67import pytest8Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 9why, one step back9import httpxSwitch to private module names (#785)Florimond Mancabefore adoption82dc6f3
lines 10–13why, one step back10from httpx._config import CertTypes, TimeoutTypes, VerifyTypes11from httpx._content_streams import encode12from httpx._dispatch.base import AsyncDispatcher13from httpx._utils import format_form_paramDon't check trio import in definition of backend fixture params (#447)Florimond Mancabefore adoptionfc3df51
lines 14–15why, one step back1415Dispatcher -> AsyncDispatcher (#725)Tom Christiebefore adoptione19bd9b
line 16why, one step back16class MockDispatch(AsyncDispatcher):Drop sync (#544)Tom Christiebefore adoption206c537
line 17why, one step back17 async def send(Don't check trio import in definition of backend fixture params (#447)Florimond Mancabefore adoptionfc3df51
line 18why, one step back18 self,Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 19why, one step back19 request: httpx.Request,Don't check trio import in definition of backend fixture params (#447)Florimond Mancabefore adoptionfc3df51
lines 20–22why, one step back20 verify: VerifyTypes = None,21 cert: CertTypes = None,22 timeout: TimeoutTypes = None,Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 23why, one step back23 ) -> httpx.Response:Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
line 24why, one step back24 content = b"".join([part async for part in request.stream])Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 25why, one step back25 return httpx.Response(200, content=content, request=request)Don't check trio import in definition of backend fixture params (#447)Florimond Mancabefore adoptionfc3df51
lines 26–28why, one step back262728@pytest.mark.parametrize(("value,output"), (("abc", b"abc"), (b"abc", b"abc")))Drop sync (#544)Tom Christiebefore adoption206c537
lines 29–30why, one step back29@pytest.mark.asyncio30async def test_multipart(value, output):Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 31why, one step back31 client = httpx.AsyncClient(dispatch=MockDispatch())Don't check trio import in definition of backend fixture params (#447)Florimond Mancabefore adoptionfc3df51
lines 32–35why, one step back3233 # Test with a single-value 'data' argument, and a plain file 'files' argument.34 data = {"text": value}35 files = {"file": io.BytesIO(b"<file content>")}Drop sync (#544)Tom Christiebefore adoption206c537
line 36why, one step back36 response = await client.post("http://127.0.0.1:8000/", data=data, files=files)Don't check trio import in definition of backend fixture params (#447)Florimond Mancabefore adoptionfc3df51
lines 37–52why, one step back37 assert response.status_code == 2003839 # We're using the cgi module to verify the behavior here, which is a40 # bit grungy, but sufficient just for our testing purposes.41 boundary = response.request.headers["Content-Type"].split("boundary=")[-1]42 content_length = response.request.headers["Content-Length"]43 pdict = {"boundary": boundary.encode("ascii"), "CONTENT-LENGTH": content_length}44 multipart = cgi.parse_multipart(io.BytesIO(response.content), pdict)4546 # Note that the expected return type for text fields47 # appears to differs from 3.6 to 3.7+48 assert multipart["text"] == [output.decode()] or multipart["text"] == [output]49 assert multipart["file"] == [b"<file content>"]505152@pytest.mark.parametrize(("key"), (b"abc", 1, 2.3, None))Drop sync (#544)Tom Christiebefore adoption206c537
lines 53–54why, one step back53@pytest.mark.asyncio54async def test_multipart_invalid_key(key):Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 55why, one step back55 client = httpx.AsyncClient(dispatch=MockDispatch())Don't check trio import in definition of backend fixture params (#447)Florimond Mancabefore adoptionfc3df51
lines 56–58why, one step back56 data = {key: "abc"}57 files = {"file": io.BytesIO(b"<file content>")}58 with pytest.raises(TypeError) as e:Drop sync (#544)Tom Christiebefore adoption206c537
line 59why, one step back59 await client.post("http://127.0.0.1:8000/", data=data, files=files)Don't check trio import in definition of backend fixture params (#447)Florimond Mancabefore adoptionfc3df51
lines 60–63why, one step back60 assert "Invalid type for name" in str(e.value)616263@pytest.mark.parametrize(("value"), (1, 2.3, None, [None, "abc"], {None: "abc"}))Drop sync (#544)Tom Christiebefore adoption206c537
lines 64–65why, one step back64@pytest.mark.asyncio65async def test_multipart_invalid_value(value):Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 66why, one step back66 client = httpx.AsyncClient(dispatch=MockDispatch())Don't check trio import in definition of backend fixture params (#447)Florimond Mancabefore adoptionfc3df51
lines 67–69why, one step back67 data = {"text": value}68 files = {"file": io.BytesIO(b"<file content>")}69 with pytest.raises(TypeError) as e:Drop sync (#544)Tom Christiebefore adoption206c537
line 70why, one step back70 await client.post("http://127.0.0.1:8000/", data=data, files=files)Don't check trio import in definition of backend fixture params (#447)Florimond Mancabefore adoptionfc3df51
lines 71–73why, one step back71 assert "Invalid type for value" in str(e.value)7273Drop sync (#544)Tom Christiebefore adoption206c537
lines 74–75why, one step back74@pytest.mark.asyncio75async def test_multipart_file_tuple():Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 76why, one step back76 client = httpx.AsyncClient(dispatch=MockDispatch())Don't check trio import in definition of backend fixture params (#447)Florimond Mancabefore adoptionfc3df51
lines 77–80why, one step back7778 # Test with a list of values 'data' argument, and a tuple style 'files' argument.79 data = {"text": ["abc"]}80 files = {"file": ("name.txt", io.BytesIO(b"<file content>"))}Drop sync (#544)Tom Christiebefore adoption206c537
line 81why, one step back81 response = await client.post("http://127.0.0.1:8000/", data=data, files=files)Don't check trio import in definition of backend fixture params (#447)Florimond Mancabefore adoptionfc3df51
lines 82–108why, one step back82 assert response.status_code == 2008384 # We're using the cgi module to verify the behavior here, which is a85 # bit grungy, but sufficient just for our testing purposes.86 boundary = response.request.headers["Content-Type"].split("boundary=")[-1]87 content_length = response.request.headers["Content-Length"]88 pdict = {"boundary": boundary.encode("ascii"), "CONTENT-LENGTH": content_length}89 multipart = cgi.parse_multipart(io.BytesIO(response.content), pdict)9091 # Note that the expected return type for text fields92 # appears to differs from 3.6 to 3.7+93 assert multipart["text"] == ["abc"] or multipart["text"] == [b"abc"]94 assert multipart["file"] == [b"<file content>"]959697def test_multipart_encode():98 data = {99 "a": "1",100 "b": b"C",101 "c": ["11", "22", "33"],102 "d": {"ff": ["1", b"2", "3"], "fff": ["11", b"22", "33"]},103 "f": "",104 }105 files = {"file": ("name.txt", io.BytesIO(b"<file content>"))}106107 with mock.patch("os.urandom", return_value=os.urandom(16)):108 boundary = binascii.hexlify(os.urandom(16)).decode("ascii")Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
lines 109–111why, one step back109 stream = encode(data=data, files=files)110 assert stream.content_type == f"multipart/form-data; boundary={boundary}"111 assert stream.body == (Don't check trio import in definition of backend fixture params (#447)Florimond Mancabefore adoptionfc3df51
lines 112–127why, one step back112 '--{0}\r\nContent-Disposition: form-data; name="a"\r\n\r\n1\r\n'113 '--{0}\r\nContent-Disposition: form-data; name="b"\r\n\r\nC\r\n'114 '--{0}\r\nContent-Disposition: form-data; name="c"\r\n\r\n11\r\n'115 '--{0}\r\nContent-Disposition: form-data; name="c"\r\n\r\n22\r\n'116 '--{0}\r\nContent-Disposition: form-data; name="c"\r\n\r\n33\r\n'117 '--{0}\r\nContent-Disposition: form-data; name="d"\r\n\r\nff\r\n'118 '--{0}\r\nContent-Disposition: form-data; name="d"\r\n\r\nfff\r\n'119 '--{0}\r\nContent-Disposition: form-data; name="f"\r\n\r\n\r\n'120 '--{0}\r\nContent-Disposition: form-data; name="file";'121 ' filename="name.txt"\r\n'122 "Content-Type: text/plain\r\n\r\n<file content>\r\n"123 "--{0}--\r\n"124 "".format(boundary).encode("ascii")125 )126127Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
lines 128–132why, one step back128def test_multipart_encode_files_allows_filenames_as_none():129 files = {"file": (None, io.BytesIO(b"<file content>"))}130 with mock.patch("os.urandom", return_value=os.urandom(16)):131 boundary = binascii.hexlify(os.urandom(16)).decode("ascii")132Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
line 133why, one step back133 stream = encode(data={}, files=files)Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
line 134why, one step back134Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
lines 135–136why, one step back135 assert stream.content_type == f"multipart/form-data; boundary={boundary}"136 assert stream.body == (Files without a filename should not set a Content-Type in multipart data. (#520)Mattwmaster58before adoptioned94950
lines 137–138why, one step back137 '--{0}\r\nContent-Disposition: form-data; name="file"\r\n\r\n'138 "<file content>\r\n--{0}--\r\n"Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
line 139why, one step back139 "".format(boundary).encode("ascii")Files without a filename should not set a Content-Type in multipart data. (#520)Mattwmaster58before adoptioned94950
lines 140–144why, one step back140 )141142143@pytest.mark.parametrize(144 "file_name,expected_content_type",Minor test fixes (#583)Tom Christiebefore adoption12d00b2
lines 145–149why, one step back145 [146 ("example.json", "application/json"),147 ("example.txt", "text/plain"),148 ("no-extension", "application/octet-stream"),149 ],Files without a filename should not set a Content-Type in multipart data. (#520)Mattwmaster58before adoptioned94950
lines 150–157why, one step back150)151def test_multipart_encode_files_guesses_correct_content_type(152 file_name, expected_content_type153):154 files = {"file": (file_name, io.BytesIO(b"<file content>"))}155 with mock.patch("os.urandom", return_value=os.urandom(16)):156 boundary = binascii.hexlify(os.urandom(16)).decode("ascii")157Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
line 158why, one step back158 stream = encode(data={}, files=files)Files without a filename should not set a Content-Type in multipart data. (#520)Mattwmaster58before adoptioned94950
line 159why, one step back159Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
lines 160–161why, one step back160 assert stream.content_type == f"multipart/form-data; boundary={boundary}"161 assert stream.body == (Files without a filename should not set a Content-Type in multipart data. (#520)Mattwmaster58before adoptioned94950
lines 162–165why, one step back162 f'--{boundary}\r\nContent-Disposition: form-data; name="file"; '163 f'filename="{file_name}"\r\nContent-Type: '164 f"{expected_content_type}\r\n\r\n<file content>\r\n--{boundary}--\r\n"165 "".encode("ascii")Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
lines 166–173why, one step back166 )167168169def test_multipart_encode_files_allows_str_content():170 files = {"file": ("test.txt", "<string content>", "text/plain")}171 with mock.patch("os.urandom", return_value=os.urandom(16)):172 boundary = binascii.hexlify(os.urandom(16)).decode("ascii")173Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
line 174why, one step back174 stream = encode(data={}, files=files)Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
line 175why, one step back175Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
lines 176–177why, one step back176 assert stream.content_type == f"multipart/form-data; boundary={boundary}"177 assert stream.body == (Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
lines 178–185why, one step back178 '--{0}\r\nContent-Disposition: form-data; name="file"; '179 'filename="test.txt"\r\n'180 "Content-Type: text/plain\r\n\r\n<string content>\r\n"181 "--{0}--\r\n"182 "".format(boundary).encode("ascii")183 )184185Don't check trio import in definition of backend fixture params (#447)Florimond Mancabefore adoptionfc3df51
lines 186–187why, one step back186class TestHeaderParamHTML5Formatting:187 def test_unicode(self):Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
line 188why, one step back188 param = format_form_param("filename", "n\u00e4me")Don't check trio import in definition of backend fixture params (#447)Florimond Mancabefore adoptionfc3df51
lines 189–191why, one step back189 assert param == b'filename="n\xc3\xa4me"'190191 def test_ascii(self):Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
line 192why, one step back192 param = format_form_param("filename", b"name")Don't check trio import in definition of backend fixture params (#447)Florimond Mancabefore adoptionfc3df51
lines 193–195why, one step back193 assert param == b'filename="name"'194195 def test_unicode_escape(self):Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
line 196why, one step back196 param = format_form_param("filename", "hello\\world\u0022")Don't check trio import in definition of backend fixture params (#447)Florimond Mancabefore adoptionfc3df51
lines 197–199why, one step back197 assert param == b'filename="hello\\\\world%22"'198199 def test_unicode_with_control_character(self):Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
line 200why, one step back200 param = format_form_param("filename", "hello\x1A\x1B\x1C")Don't check trio import in definition of backend fixture params (#447)Florimond Mancabefore adoptionfc3df51
line 201why, one step back201 assert param == b'filename="hello%1A\x1B%1C"'