Skip to content
Myr

test_multipart.py

This file

Path

tests/test_multipart.py

469 lines · 16.7 KiB · Python

Last changed by

format (#3131)T-256before adoption0006ed0

History of this file

Take it

The walk stopped after 200 commits: lines older than a6af45e point at it.

Use `__future__.annotations` (#3068)Kar Petrosyanbefore adoption371b6e9

lines 1–2why, one step back
1from __future__ import annotations
2

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 3–151why, one step back
3import io
4import tempfile5import typing67import pytest89import httpx101112def echo_request_content(request: httpx.Request) -> httpx.Response:13 return httpx.Response(200, content=request.content)141516@pytest.mark.parametrize(("value,output"), (("abc", b"abc"), (b"abc", b"abc")))17def test_multipart(value, output):18 client = httpx.Client(transport=httpx.MockTransport(echo_request_content))1920 # Test with a single-value 'data' argument, and a plain file 'files' argument.21 data = {"text": value}22 files = {"file": io.BytesIO(b"<file content>")}23 response = client.post("http://127.0.0.1:8000/", data=data, files=files)24 boundary = response.request.headers["Content-Type"].split("boundary=")[-1]25 boundary_bytes = boundary.encode("ascii")2627 assert response.status_code == 20028 assert response.content == b"".join(29 [30 b"--" + boundary_bytes + b"\r\n",31 b'Content-Disposition: form-data; name="text"\r\n',32 b"\r\n",33 b"abc\r\n",34 b"--" + boundary_bytes + b"\r\n",35 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',36 b"Content-Type: application/octet-stream\r\n",37 b"\r\n",38 b"<file content>\r\n",39 b"--" + boundary_bytes + b"--\r\n",40 ]41 )424344@pytest.mark.parametrize(45 "header",46 [47 "multipart/form-data; boundary=+++; charset=utf-8",48 "multipart/form-data; charset=utf-8; boundary=+++",49 "multipart/form-data; boundary=+++",50 "multipart/form-data; boundary=+++ ;",51 'multipart/form-data; boundary="+++"; charset=utf-8',52 'multipart/form-data; charset=utf-8; boundary="+++"',53 'multipart/form-data; boundary="+++"',54 'multipart/form-data; boundary="+++" ;',55 ],56)57def test_multipart_explicit_boundary(header: str) -> None:58 client = httpx.Client(transport=httpx.MockTransport(echo_request_content))5960 files = {"file": io.BytesIO(b"<file content>")}61 headers = {"content-type": header}62 response = client.post("http://127.0.0.1:8000/", files=files, headers=headers)63 boundary_bytes = b"+++"6465 assert response.status_code == 20066 assert response.request.headers["Content-Type"] == header67 assert response.content == b"".join(68 [69 b"--" + boundary_bytes + b"\r\n",70 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',71 b"Content-Type: application/octet-stream\r\n",72 b"\r\n",73 b"<file content>\r\n",74 b"--" + boundary_bytes + b"--\r\n",75 ]76 )777879@pytest.mark.parametrize(80 "header",81 [82 "multipart/form-data; charset=utf-8",83 "multipart/form-data; charset=utf-8; ",84 ],85)86def test_multipart_header_without_boundary(header: str) -> None:87 client = httpx.Client(transport=httpx.MockTransport(echo_request_content))8889 files = {"file": io.BytesIO(b"<file content>")}90 headers = {"content-type": header}91 response = client.post("http://127.0.0.1:8000/", files=files, headers=headers)9293 assert response.status_code == 20094 assert response.request.headers["Content-Type"] == header959697@pytest.mark.parametrize(("key"), (b"abc", 1, 2.3, None))98def test_multipart_invalid_key(key):99 client = httpx.Client(transport=httpx.MockTransport(echo_request_content))100101 data = {key: "abc"}102 files = {"file": io.BytesIO(b"<file content>")}103 with pytest.raises(TypeError) as e:104 client.post(105 "http://127.0.0.1:8000/",106 data=data,107 files=files,108 )109 assert "Invalid type for name" in str(e.value)110 assert repr(key) in str(e.value)111112113@pytest.mark.parametrize(("value"), (object(), {"key": "value"}))114def test_multipart_invalid_value(value):115 client = httpx.Client(transport=httpx.MockTransport(echo_request_content))116117 data = {"text": value}118 files = {"file": io.BytesIO(b"<file content>")}119 with pytest.raises(TypeError) as e:120 client.post("http://127.0.0.1:8000/", data=data, files=files)121 assert "Invalid type for value" in str(e.value)122123124def test_multipart_file_tuple():125 client = httpx.Client(transport=httpx.MockTransport(echo_request_content))126127 # Test with a list of values 'data' argument,128 # and a tuple style 'files' argument.129 data = {"text": ["abc"]}130 files = {"file": ("name.txt", io.BytesIO(b"<file content>"))}131 response = client.post("http://127.0.0.1:8000/", data=data, files=files)132 boundary = response.request.headers["Content-Type"].split("boundary=")[-1]133 boundary_bytes = boundary.encode("ascii")134135 assert response.status_code == 200136 assert response.content == b"".join(137 [138 b"--" + boundary_bytes + b"\r\n",139 b'Content-Disposition: form-data; name="text"\r\n',140 b"\r\n",141 b"abc\r\n",142 b"--" + boundary_bytes + b"\r\n",143 b'Content-Disposition: form-data; name="file"; filename="name.txt"\r\n',144 b"Content-Type: text/plain\r\n",145 b"\r\n",146 b"<file content>\r\n",147 b"--" + boundary_bytes + b"--\r\n",148 ]149 )150151

Drop private import of 'encode_request' in test_multipart (#2525)Tom Christiebefore adoption7947b56

line 152why, one step back
152@pytest.mark.parametrize("file_content_type", [None, "text/plain"])

Use `__future__.annotations` (#3068)Kar Petrosyanbefore adoption371b6e9

line 153why, one step back
153def test_multipart_file_tuple_headers(file_content_type: str | None) -> None:

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

line 154why, one step back
154 file_name = "test.txt"

Drop private import of 'encode_request' in test_multipart (#2525)Tom Christiebefore adoption7947b56

lines 155–156why, one step back
155 file_content = io.BytesIO(b"<file content>")
156 file_headers = {"Expires": "0"}

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

line 157why, one step back
157

Drop private import of 'encode_request' in test_multipart (#2525)Tom Christiebefore adoption7947b56

lines 158–160why, one step back
158 url = "https://www.example.com/"
159 headers = {"Content-Type": "multipart/form-data; boundary=BOUNDARY"}160 files = {"file": (file_name, file_content, file_content_type, file_headers)}

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

line 161why, one step back
161

Drop private import of 'encode_request' in test_multipart (#2525)Tom Christiebefore adoption7947b56

lines 162–163why, one step back
162 request = httpx.Request("POST", url, headers=headers, files=files)
163 request.read()

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

line 164why, one step back
164

Drop private import of 'encode_request' in test_multipart (#2525)Tom Christiebefore adoption7947b56

lines 165–175why, one step back
165 assert request.headers == {
166 "Host": "www.example.com",167 "Content-Type": "multipart/form-data; boundary=BOUNDARY",168 "Content-Length": str(len(request.content)),169 }170 assert request.content == (171 f'--BOUNDARY\r\nContent-Disposition: form-data; name="file"; '172 f'filename="{file_name}"\r\nExpires: 0\r\nContent-Type: '173 f"text/plain\r\n\r\n<file content>\r\n--BOUNDARY--\r\n"174 "".encode("ascii")175 )

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 176–178why, one step back
176
177178def test_multipart_headers_include_content_type() -> None:

Ruff linter: Use the default line-length (#2922)T-256before adoptionfd60b18

lines 179–182why, one step back
179 """
180 Content-Type from 4th tuple parameter (headers) should181 override the 3rd parameter (content_type)182 """

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

line 183why, one step back
183 file_name = "test.txt"

Drop private import of 'encode_request' in test_multipart (#2525)Tom Christiebefore adoption7947b56

lines 184–186why, one step back
184 file_content = io.BytesIO(b"<file content>")
185 file_content_type = "text/plain"186 file_headers = {"Content-Type": "image/png"}

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

line 187why, one step back
187

Drop private import of 'encode_request' in test_multipart (#2525)Tom Christiebefore adoption7947b56

lines 188–190why, one step back
188 url = "https://www.example.com/"
189 headers = {"Content-Type": "multipart/form-data; boundary=BOUNDARY"}190 files = {"file": (file_name, file_content, file_content_type, file_headers)}

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

line 191why, one step back
191

Drop private import of 'encode_request' in test_multipart (#2525)Tom Christiebefore adoption7947b56

lines 192–193why, one step back
192 request = httpx.Request("POST", url, headers=headers, files=files)
193 request.read()

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

line 194why, one step back
194

Drop private import of 'encode_request' in test_multipart (#2525)Tom Christiebefore adoption7947b56

lines 195–205why, one step back
195 assert request.headers == {
196 "Host": "www.example.com",197 "Content-Type": "multipart/form-data; boundary=BOUNDARY",198 "Content-Length": str(len(request.content)),199 }200 assert request.content == (201 f'--BOUNDARY\r\nContent-Disposition: form-data; name="file"; '202 f'filename="{file_name}"\r\nContent-Type: '203 f"image/png\r\n\r\n<file content>\r\n--BOUNDARY--\r\n"204 "".encode("ascii")205 )

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 206–212why, one step back
206
207208def test_multipart_encode(tmp_path: typing.Any) -> None:209 path = str(tmp_path / "name.txt")210 with open(path, "wb") as f:211 f.write(b"<file content>")212

Drop private import of 'encode_request' in test_multipart (#2525)Tom Christiebefore adoption7947b56

lines 213–214why, one step back
213 url = "https://www.example.com/"
214 headers = {"Content-Type": "multipart/form-data; boundary=BOUNDARY"}

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 215–225why, one step back
215 data = {
216 "a": "1",217 "b": b"C",218 "c": ["11", "22", "33"],219 "d": "",220 "e": True,221 "f": "",222 }223 with open(path, "rb") as input_file:224 files = {"file": ("name.txt", input_file)}225

Drop private import of 'encode_request' in test_multipart (#2525)Tom Christiebefore adoption7947b56

lines 226–227why, one step back
226 request = httpx.Request("POST", url, headers=headers, data=data, files=files)
227 request.read()

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

line 228why, one step back
228

Drop private import of 'encode_request' in test_multipart (#2525)Tom Christiebefore adoption7947b56

lines 229–248why, one step back
229 assert request.headers == {
230 "Host": "www.example.com",231 "Content-Type": "multipart/form-data; boundary=BOUNDARY",232 "Content-Length": str(len(request.content)),233 }234 assert request.content == (235 '--BOUNDARY\r\nContent-Disposition: form-data; name="a"\r\n\r\n1\r\n'236 '--BOUNDARY\r\nContent-Disposition: form-data; name="b"\r\n\r\nC\r\n'237 '--BOUNDARY\r\nContent-Disposition: form-data; name="c"\r\n\r\n11\r\n'238 '--BOUNDARY\r\nContent-Disposition: form-data; name="c"\r\n\r\n22\r\n'239 '--BOUNDARY\r\nContent-Disposition: form-data; name="c"\r\n\r\n33\r\n'240 '--BOUNDARY\r\nContent-Disposition: form-data; name="d"\r\n\r\n\r\n'241 '--BOUNDARY\r\nContent-Disposition: form-data; name="e"\r\n\r\ntrue\r\n'242 '--BOUNDARY\r\nContent-Disposition: form-data; name="f"\r\n\r\n\r\n'243 '--BOUNDARY\r\nContent-Disposition: form-data; name="file";'244 ' filename="name.txt"\r\n'245 "Content-Type: text/plain\r\n\r\n<file content>\r\n"246 "--BOUNDARY--\r\n"247 "".encode("ascii")248 )

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 249–251why, one step back
249
250251def test_multipart_encode_unicode_file_contents() -> None:

Drop private import of 'encode_request' in test_multipart (#2525)Tom Christiebefore adoption7947b56

lines 252–253why, one step back
252 url = "https://www.example.com/"
253 headers = {"Content-Type": "multipart/form-data; boundary=BOUNDARY"}

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 254–255why, one step back
254 files = {"file": ("name.txt", b"<bytes content>")}
255

Drop private import of 'encode_request' in test_multipart (#2525)Tom Christiebefore adoption7947b56

lines 256–257why, one step back
256 request = httpx.Request("POST", url, headers=headers, files=files)
257 request.read()

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

line 258why, one step back
258

Drop private import of 'encode_request' in test_multipart (#2525)Tom Christiebefore adoption7947b56

lines 259–269why, one step back
259 assert request.headers == {
260 "Host": "www.example.com",261 "Content-Type": "multipart/form-data; boundary=BOUNDARY",262 "Content-Length": str(len(request.content)),263 }264 assert request.content == (265 b'--BOUNDARY\r\nContent-Disposition: form-data; name="file";'266 b' filename="name.txt"\r\n'267 b"Content-Type: text/plain\r\n\r\n<bytes content>\r\n"268 b"--BOUNDARY--\r\n"269 )

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 270–272why, one step back
270
271272def test_multipart_encode_files_allows_filenames_as_none() -> None:

Drop private import of 'encode_request' in test_multipart (#2525)Tom Christiebefore adoption7947b56

lines 273–274why, one step back
273 url = "https://www.example.com/"
274 headers = {"Content-Type": "multipart/form-data; boundary=BOUNDARY"}

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 275–276why, one step back
275 files = {"file": (None, io.BytesIO(b"<file content>"))}
276

Drop private import of 'encode_request' in test_multipart (#2525)Tom Christiebefore adoption7947b56

lines 277–278why, one step back
277 request = httpx.Request("POST", url, headers=headers, data={}, files=files)
278 request.read()

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

line 279why, one step back
279

Drop private import of 'encode_request' in test_multipart (#2525)Tom Christiebefore adoption7947b56

lines 280–289why, one step back
280 assert request.headers == {
281 "Host": "www.example.com",282 "Content-Type": "multipart/form-data; boundary=BOUNDARY",283 "Content-Length": str(len(request.content)),284 }285 assert request.content == (286 '--BOUNDARY\r\nContent-Disposition: form-data; name="file"\r\n\r\n'287 "<file content>\r\n--BOUNDARY--\r\n"288 "".encode("ascii")289 )

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 290–302why, one step back
290
291292@pytest.mark.parametrize(293 "file_name,expected_content_type",294 [295 ("example.json", "application/json"),296 ("example.txt", "text/plain"),297 ("no-extension", "application/octet-stream"),298 ],299)300def test_multipart_encode_files_guesses_correct_content_type(301 file_name: str, expected_content_type: str302) -> None:

Drop private import of 'encode_request' in test_multipart (#2525)Tom Christiebefore adoption7947b56

lines 303–304why, one step back
303 url = "https://www.example.com/"
304 headers = {"Content-Type": "multipart/form-data; boundary=BOUNDARY"}

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 305–306why, one step back
305 files = {"file": (file_name, io.BytesIO(b"<file content>"))}
306

Drop private import of 'encode_request' in test_multipart (#2525)Tom Christiebefore adoption7947b56

lines 307–308why, one step back
307 request = httpx.Request("POST", url, headers=headers, data={}, files=files)
308 request.read()

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

line 309why, one step back
309

Drop private import of 'encode_request' in test_multipart (#2525)Tom Christiebefore adoption7947b56

lines 310–320why, one step back
310 assert request.headers == {
311 "Host": "www.example.com",312 "Content-Type": "multipart/form-data; boundary=BOUNDARY",313 "Content-Length": str(len(request.content)),314 }315 assert request.content == (316 f'--BOUNDARY\r\nContent-Disposition: form-data; name="file"; '317 f'filename="{file_name}"\r\nContent-Type: '318 f"{expected_content_type}\r\n\r\n<file content>\r\n--BOUNDARY--\r\n"319 "".encode("ascii")320 )

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 321–323why, one step back
321
322323def test_multipart_encode_files_allows_bytes_content() -> None:

Drop private import of 'encode_request' in test_multipart (#2525)Tom Christiebefore adoption7947b56

lines 324–325why, one step back
324 url = "https://www.example.com/"
325 headers = {"Content-Type": "multipart/form-data; boundary=BOUNDARY"}

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 326–327why, one step back
326 files = {"file": ("test.txt", b"<bytes content>", "text/plain")}
327

Drop private import of 'encode_request' in test_multipart (#2525)Tom Christiebefore adoption7947b56

lines 328–329why, one step back
328 request = httpx.Request("POST", url, headers=headers, data={}, files=files)
329 request.read()

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

line 330why, one step back
330

Drop private import of 'encode_request' in test_multipart (#2525)Tom Christiebefore adoption7947b56

lines 331–342why, one step back
331 assert request.headers == {
332 "Host": "www.example.com",333 "Content-Type": "multipart/form-data; boundary=BOUNDARY",334 "Content-Length": str(len(request.content)),335 }336 assert request.content == (337 '--BOUNDARY\r\nContent-Disposition: form-data; name="file"; '338 'filename="test.txt"\r\n'339 "Content-Type: text/plain\r\n\r\n<bytes content>\r\n"340 "--BOUNDARY--\r\n"341 "".encode("ascii")342 )

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 343–345why, one step back
343
344345def test_multipart_encode_files_allows_str_content() -> None:

Drop private import of 'encode_request' in test_multipart (#2525)Tom Christiebefore adoption7947b56

lines 346–347why, one step back
346 url = "https://www.example.com/"
347 headers = {"Content-Type": "multipart/form-data; boundary=BOUNDARY"}

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 348–349why, one step back
348 files = {"file": ("test.txt", "<str content>", "text/plain")}
349

Drop private import of 'encode_request' in test_multipart (#2525)Tom Christiebefore adoption7947b56

lines 350–351why, one step back
350 request = httpx.Request("POST", url, headers=headers, data={}, files=files)
351 request.read()

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

line 352why, one step back
352

Drop private import of 'encode_request' in test_multipart (#2525)Tom Christiebefore adoption7947b56

lines 353–364why, one step back
353 assert request.headers == {
354 "Host": "www.example.com",355 "Content-Type": "multipart/form-data; boundary=BOUNDARY",356 "Content-Length": str(len(request.content)),357 }358 assert request.content == (359 '--BOUNDARY\r\nContent-Disposition: form-data; name="file"; '360 'filename="test.txt"\r\n'361 "Content-Type: text/plain\r\n\r\n<str content>\r\n"362 "--BOUNDARY--\r\n"363 "".encode("ascii")364 )

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 365–367why, one step back
365
366367def test_multipart_encode_files_raises_exception_with_StringIO_content() -> None:

Drop private import of 'encode_request' in test_multipart (#2525)Tom Christiebefore adoption7947b56

line 368why, one step back
368 url = "https://www.example.com"

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 369–370why, one step back
369 files = {"file": ("test.txt", io.StringIO("content"), "text/plain")}
370 with pytest.raises(TypeError):

Drop private import of 'encode_request' in test_multipart (#2525)Tom Christiebefore adoption7947b56

line 371why, one step back
371 httpx.Request("POST", url, data={}, files=files) # type: ignore

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 372–374why, one step back
372
373374def test_multipart_encode_files_raises_exception_with_text_mode_file() -> None:

Drop private import of 'encode_request' in test_multipart (#2525)Tom Christiebefore adoption7947b56

line 375why, one step back
375 url = "https://www.example.com"

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 376–378why, one step back
376 with tempfile.TemporaryFile(mode="w") as upload:
377 files = {"file": ("test.txt", upload, "text/plain")}378 with pytest.raises(TypeError):

Drop private import of 'encode_request' in test_multipart (#2525)Tom Christiebefore adoption7947b56

line 379why, one step back
379 httpx.Request("POST", url, data={}, files=files) # type: ignore

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 380–399why, one step back
380
381382def test_multipart_encode_non_seekable_filelike() -> None:383 """384 Test that special readable but non-seekable filelike objects are supported.385 In this case uploads with use 'Transfer-Encoding: chunked', instead of386 a 'Content-Length' header.387 """388389 class IteratorIO(io.IOBase):390 def __init__(self, iterator: typing.Iterator[bytes]) -> None:391 self._iterator = iterator392393 def read(self, *args: typing.Any) -> bytes:394 return b"".join(self._iterator)395396 def data() -> typing.Iterator[bytes]:397 yield b"Hello"398 yield b"World"399

Drop private import of 'encode_request' in test_multipart (#2525)Tom Christiebefore adoption7947b56

lines 400–401why, one step back
400 url = "https://www.example.com/"
401 headers = {"Content-Type": "multipart/form-data; boundary=BOUNDARY"}

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 402–404why, one step back
402 fileobj: typing.Any = IteratorIO(data())
403 files = {"file": fileobj}404

Drop private import of 'encode_request' in test_multipart (#2525)Tom Christiebefore adoption7947b56

lines 405–414why, one step back
405 request = httpx.Request("POST", url, headers=headers, files=files)
406 request.read()407408 assert request.headers == {409 "Host": "www.example.com",410 "Content-Type": "multipart/form-data; boundary=BOUNDARY",411 "Transfer-Encoding": "chunked",412 }413 assert request.content == (414 b"--BOUNDARY\r\n"

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 415–418why, one step back
415 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n'
416 b"Content-Type: application/octet-stream\r\n"417 b"\r\n"418 b"HelloWorld\r\n"

Drop private import of 'encode_request' in test_multipart (#2525)Tom Christiebefore adoption7947b56

line 419why, one step back
419 b"--BOUNDARY--\r\n"

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 420–464why, one step back
420 )
421422423def test_multipart_rewinds_files():424 with tempfile.TemporaryFile() as upload:425 upload.write(b"Hello, world!")426427 transport = httpx.MockTransport(echo_request_content)428 client = httpx.Client(transport=transport)429430 files = {"file": upload}431 response = client.post("http://127.0.0.1:8000/", files=files)432 assert response.status_code == 200433 assert b"\r\nHello, world!\r\n" in response.content434435 # POSTing the same file instance a second time should have the same content.436 files = {"file": upload}437 response = client.post("http://127.0.0.1:8000/", files=files)438 assert response.status_code == 200439 assert b"\r\nHello, world!\r\n" in response.content440441442class TestHeaderParamHTML5Formatting:443 def test_unicode(self):444 filename = "n\u00e4me"445 expected = b'filename="n\xc3\xa4me"'446 files = {"upload": (filename, b"<file content>")}447 request = httpx.Request("GET", "https://www.example.com", files=files)448 assert expected in request.read()449450 def test_ascii(self):451 filename = "name"452 expected = b'filename="name"'453 files = {"upload": (filename, b"<file content>")}454 request = httpx.Request("GET", "https://www.example.com", files=files)455 assert expected in request.read()456457 def test_unicode_escape(self):458 filename = "hello\\world\u0022"459 expected = b'filename="hello\\\\world%22"'460 files = {"upload": (filename, b"<file content>")}461 request = httpx.Request("GET", "https://www.example.com", files=files)462 assert expected in request.read()463464 def test_unicode_with_control_character(self):

format (#3131)T-256before adoption0006ed0

lines 465–466why, one step back
465 filename = "hello\x1a\x1b\x1c"
466 expected = b'filename="hello%1A\x1b%1C"'

Use '%20' for encoding spaces in query parameters. (#2543)Tom Christiebefore adoptiona6af45e

lines 467–469why, one step back
467 files = {"upload": (filename, b"<file content>")}
468 request = httpx.Request("GET", "https://www.example.com", files=files)469 assert expected in request.read()