From ca28329c7fbe01d184a152e3a7ff92c041355cda Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 12 Dec 2022 16:13:44 +0000 Subject: [PATCH 1/2] Drop private import of 'format_form_param' from tests --- tests/test_multipart.py | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/tests/test_multipart.py b/tests/test_multipart.py index e9ce928a16..cf5716542f 100644 --- a/tests/test_multipart.py +++ b/tests/test_multipart.py @@ -8,7 +8,6 @@ import httpx from httpx._content import encode_request -from httpx._utils import format_form_param def echo_request_content(request: httpx.Request) -> httpx.Response: @@ -436,17 +435,29 @@ def test_multipart_rewinds_files(): class TestHeaderParamHTML5Formatting: def test_unicode(self): - param = format_form_param("filename", "n\u00e4me") - assert param == b'filename="n\xc3\xa4me"' + filename = "n\u00e4me" + expected = b'filename="n\xc3\xa4me"' + files = {"upload": (filename, b"")} + request = httpx.Request("GET", "https://www.example.com", files=files) + assert expected in request.read() def test_ascii(self): - param = format_form_param("filename", b"name") - assert param == b'filename="name"' + filename = "name" + expected = b'filename="name"' + files = {"upload": (filename, b"")} + request = httpx.Request("GET", "https://www.example.com", files=files) + assert expected in request.read() def test_unicode_escape(self): - param = format_form_param("filename", "hello\\world\u0022") - assert param == b'filename="hello\\\\world%22"' + filename = "hello\\world\u0022" + expected = b'filename="hello\\\\world%22"' + files = {"upload": (filename, b"")} + request = httpx.Request("GET", "https://www.example.com", files=files) + assert expected in request.read() def test_unicode_with_control_character(self): - param = format_form_param("filename", "hello\x1A\x1B\x1C") - assert param == b'filename="hello%1A\x1B%1C"' + filename = "hello\x1A\x1B\x1C" + expected = b'filename="hello%1A\x1B%1C"' + files = {"upload": (filename, b"")} + request = httpx.Request("GET", "https://www.example.com", files=files) + assert expected in request.read() From 4456cd01642c543942f78cd5a9f88e609d0ce530 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 12 Dec 2022 16:21:35 +0000 Subject: [PATCH 2/2] Drop unused code path --- httpx/_utils.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/httpx/_utils.py b/httpx/_utils.py index 126936db15..1f64deedcd 100644 --- a/httpx/_utils.py +++ b/httpx/_utils.py @@ -81,12 +81,10 @@ def is_known_encoding(encoding: str) -> bool: return True -def format_form_param(name: str, value: typing.Union[str, bytes]) -> bytes: +def format_form_param(name: str, value: str) -> bytes: """ Encode a name/value pair within a multipart form. """ - if isinstance(value, bytes): - value = value.decode() def replacer(match: typing.Match[str]) -> str: return _HTML5_FORM_ENCODING_REPLACEMENTS[match.group(0)]