test_multipart.py
This file
Path
tests/test_multipart.py
200 lines · 7.8 KiB · Python
Last changed by
Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
History of this fileTake it
The walk stopped after 200 commits: lines older than b46d9ea point at it.
Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea
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
lines 9–12why, one step back9import httpx10from httpx.config import CertTypes, TimeoutTypes, VerifyTypes11from httpx.dispatch.base import Dispatcher12from httpx.multipart import _format_param, multipart_encodeInstall package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea
lines 13–15why, one step back131415class MockDispatch(Dispatcher):Drop sync (#544)Tom Christiebefore adoption206c537
line 16why, one step back16 async def send(Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea
line 17why, one step back17 self,Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 18why, one step back18 request: httpx.Request,Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea
lines 19–21why, one step back19 verify: VerifyTypes = None,20 cert: CertTypes = None,21 timeout: TimeoutTypes = None,Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 22why, one step back22 ) -> httpx.Response:Drop sync (#544)Tom Christiebefore adoption206c537
line 23why, one step back23 content = await request.read()Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 24why, one step back24 return httpx.Response(200, content=content)Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea
lines 25–27why, one step back252627@pytest.mark.parametrize(("value,output"), (("abc", b"abc"), (b"abc", b"abc")))Drop sync (#544)Tom Christiebefore adoption206c537
lines 28–29why, one step back28@pytest.mark.asyncio29async def test_multipart(value, output):Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 30why, one step back30 client = httpx.Client(dispatch=MockDispatch())Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea
lines 31–34why, one step back3132 # Test with a single-value 'data' argument, and a plain file 'files' argument.33 data = {"text": value}34 files = {"file": io.BytesIO(b"<file content>")}Drop sync (#544)Tom Christiebefore adoption206c537
line 35why, one step back35 response = await client.post("http://127.0.0.1:8000/", data=data, files=files)Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea
lines 36–51why, one step back36 assert response.status_code == 2003738 # We're using the cgi module to verify the behavior here, which is a39 # bit grungy, but sufficient just for our testing purposes.40 boundary = response.request.headers["Content-Type"].split("boundary=")[-1]41 content_length = response.request.headers["Content-Length"]42 pdict = {"boundary": boundary.encode("ascii"), "CONTENT-LENGTH": content_length}43 multipart = cgi.parse_multipart(io.BytesIO(response.content), pdict)4445 # Note that the expected return type for text fields46 # appears to differs from 3.6 to 3.7+47 assert multipart["text"] == [output.decode()] or multipart["text"] == [output]48 assert multipart["file"] == [b"<file content>"]495051@pytest.mark.parametrize(("key"), (b"abc", 1, 2.3, None))Drop sync (#544)Tom Christiebefore adoption206c537
lines 52–53why, one step back52@pytest.mark.asyncio53async def test_multipart_invalid_key(key):Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 54why, one step back54 client = httpx.Client(dispatch=MockDispatch())Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea
lines 55–57why, one step back55 data = {key: "abc"}56 files = {"file": io.BytesIO(b"<file content>")}57 with pytest.raises(TypeError) as e:Drop sync (#544)Tom Christiebefore adoption206c537
line 58why, one step back58 await client.post("http://127.0.0.1:8000/", data=data, files=files)Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea
lines 59–62why, one step back59 assert "Invalid type for name" in str(e.value)606162@pytest.mark.parametrize(("value"), (1, 2.3, None, [None, "abc"], {None: "abc"}))Drop sync (#544)Tom Christiebefore adoption206c537
lines 63–64why, one step back63@pytest.mark.asyncio64async def test_multipart_invalid_value(value):Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 65why, one step back65 client = httpx.Client(dispatch=MockDispatch())Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea
lines 66–68why, one step back66 data = {"text": value}67 files = {"file": io.BytesIO(b"<file content>")}68 with pytest.raises(TypeError) as e:Drop sync (#544)Tom Christiebefore adoption206c537
line 69why, one step back69 await client.post("http://127.0.0.1:8000/", data=data, files=files)Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea
lines 70–72why, one step back70 assert "Invalid type for value" in str(e.value)7172Drop sync (#544)Tom Christiebefore adoption206c537
lines 73–74why, one step back73@pytest.mark.asyncio74async def test_multipart_file_tuple():Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 75why, one step back75 client = httpx.Client(dispatch=MockDispatch())Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea
lines 76–79why, one step back7677 # Test with a list of values 'data' argument, and a tuple style 'files' argument.78 data = {"text": ["abc"]}79 files = {"file": ("name.txt", io.BytesIO(b"<file content>"))}Drop sync (#544)Tom Christiebefore adoption206c537
line 80why, one step back80 response = await client.post("http://127.0.0.1:8000/", data=data, files=files)Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea
lines 81–107why, one step back81 assert response.status_code == 2008283 # We're using the cgi module to verify the behavior here, which is a84 # bit grungy, but sufficient just for our testing purposes.85 boundary = response.request.headers["Content-Type"].split("boundary=")[-1]86 content_length = response.request.headers["Content-Length"]87 pdict = {"boundary": boundary.encode("ascii"), "CONTENT-LENGTH": content_length}88 multipart = cgi.parse_multipart(io.BytesIO(response.content), pdict)8990 # Note that the expected return type for text fields91 # appears to differs from 3.6 to 3.7+92 assert multipart["text"] == ["abc"] or multipart["text"] == [b"abc"]93 assert multipart["file"] == [b"<file content>"]949596def test_multipart_encode():97 data = {98 "a": "1",99 "b": b"C",100 "c": ["11", "22", "33"],101 "d": {"ff": ["1", b"2", "3"], "fff": ["11", b"22", "33"]},102 "f": "",103 }104 files = {"file": ("name.txt", io.BytesIO(b"<file content>"))}105106 with mock.patch("os.urandom", return_value=os.urandom(16)):107 boundary = binascii.hexlify(os.urandom(16)).decode("ascii")Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 108why, one step back108 body, content_type = multipart_encode(data=data, files=files)Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea
lines 109–126why, one step back109 assert content_type == f"multipart/form-data; boundary={boundary}"110 assert body == (111 '--{0}\r\nContent-Disposition: form-data; name="a"\r\n\r\n1\r\n'112 '--{0}\r\nContent-Disposition: form-data; name="b"\r\n\r\nC\r\n'113 '--{0}\r\nContent-Disposition: form-data; name="c"\r\n\r\n11\r\n'114 '--{0}\r\nContent-Disposition: form-data; name="c"\r\n\r\n22\r\n'115 '--{0}\r\nContent-Disposition: form-data; name="c"\r\n\r\n33\r\n'116 '--{0}\r\nContent-Disposition: form-data; name="d"\r\n\r\nff\r\n'117 '--{0}\r\nContent-Disposition: form-data; name="d"\r\n\r\nfff\r\n'118 '--{0}\r\nContent-Disposition: form-data; name="f"\r\n\r\n\r\n'119 '--{0}\r\nContent-Disposition: form-data; name="file";'120 ' filename="name.txt"\r\n'121 "Content-Type: text/plain\r\n\r\n<file content>\r\n"122 "--{0}--\r\n"123 "".format(boundary).encode("ascii")124 )125126Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
lines 127–131why, one step back127def test_multipart_encode_files_allows_filenames_as_none():128 files = {"file": (None, io.BytesIO(b"<file content>"))}129 with mock.patch("os.urandom", return_value=os.urandom(16)):130 boundary = binascii.hexlify(os.urandom(16)).decode("ascii")131Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 132why, one step back132 body, content_type = multipart_encode(data={}, files=files)Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
lines 133–135why, one step back133134 assert content_type == f"multipart/form-data; boundary={boundary}"135 assert body == (Files without a filename should not set a Content-Type in multipart data. (#520)Mattwmaster58before adoptioned94950
lines 136–137why, one step back136 '--{0}\r\nContent-Disposition: form-data; name="file"\r\n\r\n'137 "<file content>\r\n--{0}--\r\n"Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
line 138why, one step back138 "".format(boundary).encode("ascii")Files without a filename should not set a Content-Type in multipart data. (#520)Mattwmaster58before adoptioned94950
lines 139–143why, one step back139 )140141142@pytest.mark.parametrize(143 "file_name,expected_content_type",Minor test fixes (#583)Tom Christiebefore adoption12d00b2
lines 144–148why, one step back144 [145 ("example.json", "application/json"),146 ("example.txt", "text/plain"),147 ("no-extension", "application/octet-stream"),148 ],Files without a filename should not set a Content-Type in multipart data. (#520)Mattwmaster58before adoptioned94950
lines 149–156why, one step back149)150def test_multipart_encode_files_guesses_correct_content_type(151 file_name, expected_content_type152):153 files = {"file": (file_name, io.BytesIO(b"<file content>"))}154 with mock.patch("os.urandom", return_value=os.urandom(16)):155 boundary = binascii.hexlify(os.urandom(16)).decode("ascii")156Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 157why, one step back157 body, content_type = multipart_encode(data={}, files=files)Files without a filename should not set a Content-Type in multipart data. (#520)Mattwmaster58before adoptioned94950
lines 158–164why, one step back158159 assert content_type == f"multipart/form-data; boundary={boundary}"160 assert body == (161 f'--{boundary}\r\nContent-Disposition: form-data; name="file"; '162 f'filename="{file_name}"\r\nContent-Type: '163 f"{expected_content_type}\r\n\r\n<file content>\r\n--{boundary}--\r\n"164 "".encode("ascii")Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
lines 165–172why, one step back165 )166167168def test_multipart_encode_files_allows_str_content():169 files = {"file": ("test.txt", "<string content>", "text/plain")}170 with mock.patch("os.urandom", return_value=os.urandom(16)):171 boundary = binascii.hexlify(os.urandom(16)).decode("ascii")172Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 173why, one step back173 body, content_type = multipart_encode(data={}, files=files)Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
lines 174–184why, one step back174175 assert content_type == f"multipart/form-data; boundary={boundary}"176 assert body == (177 '--{0}\r\nContent-Disposition: form-data; name="file"; '178 'filename="test.txt"\r\n'179 "Content-Type: text/plain\r\n\r\n<string content>\r\n"180 "--{0}--\r\n"181 "".format(boundary).encode("ascii")182 )183184Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea
lines 185–186why, one step back185class TestHeaderParamHTML5Formatting:186 def test_unicode(self):Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 187why, one step back187 param = _format_param("filename", "n\u00e4me")Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea
lines 188–190why, one step back188 assert param == b'filename="n\xc3\xa4me"'189190 def test_ascii(self):Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 191why, one step back191 param = _format_param("filename", b"name")Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea
lines 192–194why, one step back192 assert param == b'filename="name"'193194 def test_unicode_escape(self):Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 195why, one step back195 param = _format_param("filename", "hello\\world\u0022")Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea
lines 196–198why, one step back196 assert param == b'filename="hello\\\\world%22"'197198 def test_unicode_with_control_character(self):Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 199why, one step back199 param = _format_param("filename", "hello\x1A\x1B\x1C")Install package locally in test-requirements.txt (#231)Florimond Mancabefore adoptionb46d9ea
line 200why, one step back200 assert param == b'filename="hello%1A\x1B%1C"'