test_multipart.py
This file
Path
tests/test_multipart.py
145 lines · 5.5 KiB · Python
Last changed by
Merge pull request #241 from encode/protocol-supportTom Christiebefore adoptione0b523d
History of this fileTake it
Multipart data values encoding (#121)ambrozicbefore adoption0e8dae4
line 1why, one step back1import binasciiMultipart support (#90)Tom Christiebefore adoption6a44b05
lines 2–3why, one step back2import cgi3import ioMultipart data values encoding (#121)ambrozicbefore adoption0e8dae4
lines 4–5why, one step back4import os5from unittest import mockMultipart support (#90)Tom Christiebefore adoption6a44b05
line 6why, one step back6Multipart data values encoding (#121)ambrozicbefore adoption0e8dae4
lines 7–8why, one step back7import pytest8Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 9why, one step back9from httpx import (Multipart support (#90)Tom Christiebefore adoption6a44b05
lines 10–12why, one step back10 CertTypes,11 Client,12 Dispatcher,Merge pull request #241 from encode/protocol-supportTom Christiebefore adoptione0b523d
line 13why, one step back13 HTTPVersionTypes,Multipart support (#90)Tom Christiebefore adoption6a44b05
lines 14–17why, one step back14 Request,15 Response,16 TimeoutTypes,17 VerifyTypes,Check disconnections on connection reacquiry (#145)Tom Christiebefore adoptionec365c0
line 18why, one step back18 multipart,Multipart support (#90)Tom Christiebefore adoption6a44b05
lines 19–28why, one step back19)202122class MockDispatch(Dispatcher):23 def send(24 self,25 request: Request,26 verify: VerifyTypes = None,27 cert: CertTypes = None,28 timeout: TimeoutTypes = None,Merge pull request #241 from encode/protocol-supportTom Christiebefore adoptione0b523d
line 29why, one step back29 http_versions: HTTPVersionTypes = None,Multipart support (#90)Tom Christiebefore adoption6a44b05
lines 30–33why, one step back30 ) -> Response:31 return Response(200, content=request.read())3233Multipart data values encoding (#121)ambrozicbefore adoption0e8dae4
lines 34–35why, one step back34@pytest.mark.parametrize(("value,output"), (("abc", b"abc"), (b"abc", b"abc")))35def test_multipart(value, output):Multipart support (#90)Tom Christiebefore adoption6a44b05
lines 36–38why, one step back36 client = Client(dispatch=MockDispatch())3738 # Test with a single-value 'data' argument, and a plain file 'files' argument.Multipart data values encoding (#121)ambrozicbefore adoption0e8dae4
line 39why, one step back39 data = {"text": value}Multipart support (#90)Tom Christiebefore adoption6a44b05
lines 40–50why, one step back40 files = {"file": io.BytesIO(b"<file content>")}41 response = client.post("http://127.0.0.1:8000/", data=data, files=files)42 assert response.status_code == 2004344 # We're using the cgi module to verify the behavior here, which is a45 # bit grungy, but sufficient just for our testing purposes.46 boundary = response.request.headers["Content-Type"].split("boundary=")[-1]47 content_length = response.request.headers["Content-Length"]48 pdict = {"boundary": boundary.encode("ascii"), "CONTENT-LENGTH": content_length}49 multipart = cgi.parse_multipart(io.BytesIO(response.content), pdict)50Start using flake8, fix lint issues (#126)Seth Michael Larsonbefore adoptionc1044ca
lines 51–52why, one step back51 # Note that the expected return type for text fields52 # appears to differs from 3.6 to 3.7+Multipart data values encoding (#121)ambrozicbefore adoption0e8dae4
line 53why, one step back53 assert multipart["text"] == [output.decode()] or multipart["text"] == [output]Multipart support (#90)Tom Christiebefore adoption6a44b05
lines 54–56why, one step back54 assert multipart["file"] == [b"<file content>"]5556Multipart data values encoding (#121)ambrozicbefore adoption0e8dae4
lines 57–76why, one step back57@pytest.mark.parametrize(("key"), (b"abc", 1, 2.3, None))58def test_multipart_invalid_key(key):59 client = Client(dispatch=MockDispatch())60 data = {key: "abc"}61 files = {"file": io.BytesIO(b"<file content>")}62 with pytest.raises(TypeError) as e:63 client.post("http://127.0.0.1:8000/", data=data, files=files)64 assert "Invalid type for name" in str(e.value)656667@pytest.mark.parametrize(("value"), (1, 2.3, None, [None, "abc"], {None: "abc"}))68def test_multipart_invalid_value(value):69 client = Client(dispatch=MockDispatch())70 data = {"text": value}71 files = {"file": io.BytesIO(b"<file content>")}72 with pytest.raises(TypeError) as e:73 client.post("http://127.0.0.1:8000/", data=data, files=files)74 assert "Invalid type for value" in str(e.value)7576Multipart support (#90)Tom Christiebefore adoption6a44b05
lines 77–92why, one step back77def test_multipart_file_tuple():78 client = Client(dispatch=MockDispatch())7980 # Test with a list of values 'data' argument, and a tuple style 'files' argument.81 data = {"text": ["abc"]}82 files = {"file": ("name.txt", io.BytesIO(b"<file content>"))}83 response = client.post("http://127.0.0.1:8000/", data=data, files=files)84 assert response.status_code == 2008586 # We're using the cgi module to verify the behavior here, which is a87 # bit grungy, but sufficient just for our testing purposes.88 boundary = response.request.headers["Content-Type"].split("boundary=")[-1]89 content_length = response.request.headers["Content-Length"]90 pdict = {"boundary": boundary.encode("ascii"), "CONTENT-LENGTH": content_length}91 multipart = cgi.parse_multipart(io.BytesIO(response.content), pdict)92Start using flake8, fix lint issues (#126)Seth Michael Larsonbefore adoptionc1044ca
lines 93–94why, one step back93 # Note that the expected return type for text fields94 # appears to differs from 3.6 to 3.7+Multipart support (#90)Tom Christiebefore adoption6a44b05
lines 95–96why, one step back95 assert multipart["text"] == ["abc"] or multipart["text"] == [b"abc"]96 assert multipart["file"] == [b"<file content>"]Multipart data values encoding (#121)ambrozicbefore adoption0e8dae4
lines 97–121why, one step back979899def test_multipart_encode():100 data = {101 "a": "1",102 "b": b"C",103 "c": ["11", "22", "33"],104 "d": {"ff": ["1", b"2", "3"], "fff": ["11", b"22", "33"]},105 "f": "",106 }107 files = {"file": ("name.txt", io.BytesIO(b"<file content>"))}108109 with mock.patch("os.urandom", return_value=os.urandom(16)):110 boundary = binascii.hexlify(os.urandom(16)).decode("ascii")111 body, content_type = multipart.multipart_encode(data=data, files=files)112 assert content_type == f"multipart/form-data; boundary={boundary}"113 assert body == (114 '--{0}\r\nContent-Disposition: form-data; name="a"\r\n\r\n1\r\n'115 '--{0}\r\nContent-Disposition: form-data; name="b"\r\n\r\nC\r\n'116 '--{0}\r\nContent-Disposition: form-data; name="c"\r\n\r\n11\r\n'117 '--{0}\r\nContent-Disposition: form-data; name="c"\r\n\r\n22\r\n'118 '--{0}\r\nContent-Disposition: form-data; name="c"\r\n\r\n33\r\n'119 '--{0}\r\nContent-Disposition: form-data; name="d"\r\n\r\nff\r\n'120 '--{0}\r\nContent-Disposition: form-data; name="d"\r\n\r\nfff\r\n'121 '--{0}\r\nContent-Disposition: form-data; name="f"\r\n\r\n\r\n'Add additional flake8 plugins (#157)Can Sarıgölbefore adoption7e9110b
lines 122–123why, one step back122 '--{0}\r\nContent-Disposition: form-data; name="file";'123 ' filename="name.txt"\r\n'Multipart data values encoding (#121)ambrozicbefore adoption0e8dae4
lines 124–127why, one step back124 "Content-Type: text/plain\r\n\r\n<file content>\r\n"125 "--{0}--\r\n"126 "".format(boundary).encode("ascii")127 )Fixed multipart header params encoding (#167)Nikbefore adoptionfb17459
lines 128–145why, one step back128129130class TestHeaderParamHTML5Formatting:131 def test_unicode(self):132 param = multipart._format_param("filename", "n\u00e4me")133 assert param == b'filename="n\xc3\xa4me"'134135 def test_ascii(self):136 param = multipart._format_param("filename", b"name")137 assert param == b'filename="name"'138139 def test_unicode_escape(self):140 param = multipart._format_param("filename", "hello\\world\u0022")141 assert param == b'filename="hello\\\\world%22"'142143 def test_unicode_with_control_character(self):144 param = multipart._format_param("filename", "hello\x1A\x1B\x1C")145 assert param == b'filename="hello%1A\x1B%1C"'