Skip to content
Myr

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

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tom Christie · committed by GitHub · parent a6af45e · Browse files · 1 file+156 added−156 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+156 added−156 removed

  • tests:+156 added−156 removed

1 file+156 added−156 removed

modified:tests/test_multipart.py+156 added-156 removed
1import io2import os2import tempfile3import typing5from unittest import mock45import pytest67import httpx10from httpx._content import encode_request8910def echo_request_content(request: httpx.Request) -> httpx.Response:11 return httpx.Response(200, content=request.content)
134 unmodified lines
146 ]147 )148149153@pytest.mark.parametrize("content_type", [None, "text/plain"])154def test_multipart_file_tuple_headers(content_type: typing.Optional[str]) -> None:150@pytest.mark.parametrize("file_content_type", [None, "text/plain"])151def test_multipart_file_tuple_headers(file_content_type: typing.Optional[str]) -> None:152 file_name = "test.txt"156 expected_content_type = "text/plain"157 headers = {"Expires": "0"}153 file_content = io.BytesIO(b"<file content>")154 file_headers = {"Expires": "0"}155159 files = {"file": (file_name, io.BytesIO(b"<file content>"), content_type, headers)}160 with mock.patch("os.urandom", return_value=os.urandom(16)):161 boundary = os.urandom(16).hex()156 url = "https://www.example.com/"157 headers = {"Content-Type": "multipart/form-data; boundary=BOUNDARY"}158 files = {"file": (file_name, file_content, file_content_type, file_headers)}159163 headers, stream = encode_request(data={}, files=files)164 assert isinstance(stream, typing.Iterable)160 request = httpx.Request("POST", url, headers=headers, files=files)161 request.read()162166 content = (167 f'--{boundary}\r\nContent-Disposition: form-data; name="file"; '168 f'filename="{file_name}"\r\nExpires: 0\r\nContent-Type: '169 f"{expected_content_type}\r\n\r\n<file content>\r\n--{boundary}--\r\n"170 "".encode("ascii")171 )172 assert headers == {173 "Content-Type": f"multipart/form-data; boundary={boundary}",174 "Content-Length": str(len(content)),175 }176 assert content == b"".join(stream)163 assert request.headers == {164 "Host": "www.example.com",165 "Content-Type": "multipart/form-data; boundary=BOUNDARY",166 "Content-Length": str(len(request.content)),167 }168 assert request.content == (169 f'--BOUNDARY\r\nContent-Disposition: form-data; name="file"; '170 f'filename="{file_name}"\r\nExpires: 0\r\nContent-Type: '171 f"text/plain\r\n\r\n<file content>\r\n--BOUNDARY--\r\n"172 "".encode("ascii")173 )174175176def test_multipart_headers_include_content_type() -> None:177 """Content-Type from 4th tuple parameter (headers) should override the 3rd parameter (content_type)"""178 file_name = "test.txt"182 expected_content_type = "image/png"183 headers = {"Content-Type": "image/png"}179 file_content = io.BytesIO(b"<file content>")180 file_content_type = "text/plain"181 file_headers = {"Content-Type": "image/png"}182185 files = {"file": (file_name, io.BytesIO(b"<file content>"), "text_plain", headers)}186 with mock.patch("os.urandom", return_value=os.urandom(16)):187 boundary = os.urandom(16).hex()183 url = "https://www.example.com/"184 headers = {"Content-Type": "multipart/form-data; boundary=BOUNDARY"}185 files = {"file": (file_name, file_content, file_content_type, file_headers)}186189 headers, stream = encode_request(data={}, files=files)190 assert isinstance(stream, typing.Iterable)187 request = httpx.Request("POST", url, headers=headers, files=files)188 request.read()189192 content = (193 f'--{boundary}\r\nContent-Disposition: form-data; name="file"; '194 f'filename="{file_name}"\r\nContent-Type: '195 f"{expected_content_type}\r\n\r\n<file content>\r\n--{boundary}--\r\n"196 "".encode("ascii")197 )198 assert headers == {199 "Content-Type": f"multipart/form-data; boundary={boundary}",200 "Content-Length": str(len(content)),201 }202 assert content == b"".join(stream)190 assert request.headers == {191 "Host": "www.example.com",192 "Content-Type": "multipart/form-data; boundary=BOUNDARY",193 "Content-Length": str(len(request.content)),194 }195 assert request.content == (196 f'--BOUNDARY\r\nContent-Disposition: form-data; name="file"; '197 f'filename="{file_name}"\r\nContent-Type: '198 f"image/png\r\n\r\n<file content>\r\n--BOUNDARY--\r\n"199 "".encode("ascii")200 )201202203def test_multipart_encode(tmp_path: typing.Any) -> None:204 path = str(tmp_path / "name.txt")205 with open(path, "wb") as f:206 f.write(b"<file content>")207208 url = "https://www.example.com/"209 headers = {"Content-Type": "multipart/form-data; boundary=BOUNDARY"}210 data = {211 "a": "1",212 "b": b"C",213 "c": ["11", "22", "33"],
3 unmodified lines
217 }218 with open(path, "rb") as input_file:219 files = {"file": ("name.txt", input_file)}220221 with mock.patch("os.urandom", return_value=os.urandom(16)):222 boundary = os.urandom(16).hex()221 request = httpx.Request("POST", url, headers=headers, data=data, files=files)222 request.read()223224 headers, stream = encode_request(data=data, files=files)225 assert isinstance(stream, typing.Iterable)224 assert request.headers == {225 "Host": "www.example.com",226 "Content-Type": "multipart/form-data; boundary=BOUNDARY",227 "Content-Length": str(len(request.content)),228 }229 assert request.content == (230 '--BOUNDARY\r\nContent-Disposition: form-data; name="a"\r\n\r\n1\r\n'231 '--BOUNDARY\r\nContent-Disposition: form-data; name="b"\r\n\r\nC\r\n'232 '--BOUNDARY\r\nContent-Disposition: form-data; name="c"\r\n\r\n11\r\n'233 '--BOUNDARY\r\nContent-Disposition: form-data; name="c"\r\n\r\n22\r\n'234 '--BOUNDARY\r\nContent-Disposition: form-data; name="c"\r\n\r\n33\r\n'235 '--BOUNDARY\r\nContent-Disposition: form-data; name="d"\r\n\r\n\r\n'236 '--BOUNDARY\r\nContent-Disposition: form-data; name="e"\r\n\r\ntrue\r\n'237 '--BOUNDARY\r\nContent-Disposition: form-data; name="f"\r\n\r\n\r\n'238 '--BOUNDARY\r\nContent-Disposition: form-data; name="file";'239 ' filename="name.txt"\r\n'240 "Content-Type: text/plain\r\n\r\n<file content>\r\n"241 "--BOUNDARY--\r\n"242 "".encode("ascii")243 )244227 content = (228 '--{0}\r\nContent-Disposition: form-data; name="a"\r\n\r\n1\r\n'229 '--{0}\r\nContent-Disposition: form-data; name="b"\r\n\r\nC\r\n'230 '--{0}\r\nContent-Disposition: form-data; name="c"\r\n\r\n11\r\n'231 '--{0}\r\nContent-Disposition: form-data; name="c"\r\n\r\n22\r\n'232 '--{0}\r\nContent-Disposition: form-data; name="c"\r\n\r\n33\r\n'233 '--{0}\r\nContent-Disposition: form-data; name="d"\r\n\r\n\r\n'234 '--{0}\r\nContent-Disposition: form-data; name="e"\r\n\r\ntrue\r\n'235 '--{0}\r\nContent-Disposition: form-data; name="f"\r\n\r\n\r\n'236 '--{0}\r\nContent-Disposition: form-data; name="file";'237 ' filename="name.txt"\r\n'238 "Content-Type: text/plain\r\n\r\n<file content>\r\n"239 "--{0}--\r\n"240 "".format(boundary).encode("ascii")241 )242 assert headers == {243 "Content-Type": f"multipart/form-data; boundary={boundary}",244 "Content-Length": str(len(content)),245 }246 assert content == b"".join(stream)245248246def test_multipart_encode_unicode_file_contents() -> None:247 url = "https://www.example.com/"248 headers = {"Content-Type": "multipart/form-data; boundary=BOUNDARY"}249 files = {"file": ("name.txt", b"<bytes content>")}250252 with mock.patch("os.urandom", return_value=os.urandom(16)):253 boundary = os.urandom(16).hex()251 request = httpx.Request("POST", url, headers=headers, files=files)252 request.read()253255 headers, stream = encode_request(files=files)256 assert isinstance(stream, typing.Iterable)254 assert request.headers == {255 "Host": "www.example.com",256 "Content-Type": "multipart/form-data; boundary=BOUNDARY",257 "Content-Length": str(len(request.content)),258 }259 assert request.content == (260 b'--BOUNDARY\r\nContent-Disposition: form-data; name="file";'261 b' filename="name.txt"\r\n'262 b"Content-Type: text/plain\r\n\r\n<bytes content>\r\n"263 b"--BOUNDARY--\r\n"264 )265258 content = (259 '--{0}\r\nContent-Disposition: form-data; name="file";'260 ' filename="name.txt"\r\n'261 "Content-Type: text/plain\r\n\r\n<bytes content>\r\n"262 "--{0}--\r\n"263 "".format(boundary).encode("utf-8")264 )265 assert headers == {266 "Content-Type": f"multipart/form-data; boundary={boundary}",267 "Content-Length": str(len(content)),268 }269 assert content == b"".join(stream)266271267def test_multipart_encode_files_allows_filenames_as_none() -> None:268 url = "https://www.example.com/"269 headers = {"Content-Type": "multipart/form-data; boundary=BOUNDARY"}270 files = {"file": (None, io.BytesIO(b"<file content>"))}274 with mock.patch("os.urandom", return_value=os.urandom(16)):275 boundary = os.urandom(16).hex()271277 headers, stream = encode_request(data={}, files=files)278 assert isinstance(stream, typing.Iterable)272 request = httpx.Request("POST", url, headers=headers, data={}, files=files)273 request.read()274280 content = (281 '--{0}\r\nContent-Disposition: form-data; name="file"\r\n\r\n'282 "<file content>\r\n--{0}--\r\n"283 "".format(boundary).encode("ascii")284 )285 assert headers == {286 "Content-Type": f"multipart/form-data; boundary={boundary}",287 "Content-Length": str(len(content)),288 }289 assert content == b"".join(stream)275 assert request.headers == {276 "Host": "www.example.com",277 "Content-Type": "multipart/form-data; boundary=BOUNDARY",278 "Content-Length": str(len(request.content)),279 }280 assert request.content == (281 '--BOUNDARY\r\nContent-Disposition: form-data; name="file"\r\n\r\n'282 "<file content>\r\n--BOUNDARY--\r\n"283 "".encode("ascii")284 )285286287@pytest.mark.parametrize(288 "file_name,expected_content_type",
5 unmodified lines
294)295def test_multipart_encode_files_guesses_correct_content_type(296 file_name: str, expected_content_type: str297) -> None:298 url = "https://www.example.com/"299 headers = {"Content-Type": "multipart/form-data; boundary=BOUNDARY"}300 files = {"file": (file_name, io.BytesIO(b"<file content>"))}304 with mock.patch("os.urandom", return_value=os.urandom(16)):305 boundary = os.urandom(16).hex()301307 headers, stream = encode_request(data={}, files=files)308 assert isinstance(stream, typing.Iterable)302 request = httpx.Request("POST", url, headers=headers, data={}, files=files)303 request.read()304310 content = (311 f'--{boundary}\r\nContent-Disposition: form-data; name="file"; '312 f'filename="{file_name}"\r\nContent-Type: '313 f"{expected_content_type}\r\n\r\n<file content>\r\n--{boundary}--\r\n"314 "".encode("ascii")315 )316 assert headers == {317 "Content-Type": f"multipart/form-data; boundary={boundary}",318 "Content-Length": str(len(content)),319 }320 assert content == b"".join(stream)305 assert request.headers == {306 "Host": "www.example.com",307 "Content-Type": "multipart/form-data; boundary=BOUNDARY",308 "Content-Length": str(len(request.content)),309 }310 assert request.content == (311 f'--BOUNDARY\r\nContent-Disposition: form-data; name="file"; '312 f'filename="{file_name}"\r\nContent-Type: '313 f"{expected_content_type}\r\n\r\n<file content>\r\n--BOUNDARY--\r\n"314 "".encode("ascii")315 )316317318def test_multipart_encode_files_allows_bytes_content() -> None:319 url = "https://www.example.com/"320 headers = {"Content-Type": "multipart/form-data; boundary=BOUNDARY"}321 files = {"file": ("test.txt", b"<bytes content>", "text/plain")}325 with mock.patch("os.urandom", return_value=os.urandom(16)):326 boundary = os.urandom(16).hex()322328 headers, stream = encode_request(data={}, files=files)329 assert isinstance(stream, typing.Iterable)323 request = httpx.Request("POST", url, headers=headers, data={}, files=files)324 request.read()325331 content = (332 '--{0}\r\nContent-Disposition: form-data; name="file"; '333 'filename="test.txt"\r\n'334 "Content-Type: text/plain\r\n\r\n<bytes content>\r\n"335 "--{0}--\r\n"336 "".format(boundary).encode("ascii")337 )338 assert headers == {339 "Content-Type": f"multipart/form-data; boundary={boundary}",340 "Content-Length": str(len(content)),341 }342 assert content == b"".join(stream)326 assert request.headers == {327 "Host": "www.example.com",328 "Content-Type": "multipart/form-data; boundary=BOUNDARY",329 "Content-Length": str(len(request.content)),330 }331 assert request.content == (332 '--BOUNDARY\r\nContent-Disposition: form-data; name="file"; '333 'filename="test.txt"\r\n'334 "Content-Type: text/plain\r\n\r\n<bytes content>\r\n"335 "--BOUNDARY--\r\n"336 "".encode("ascii")337 )338339340def test_multipart_encode_files_allows_str_content() -> None:341 url = "https://www.example.com/"342 headers = {"Content-Type": "multipart/form-data; boundary=BOUNDARY"}343 files = {"file": ("test.txt", "<str content>", "text/plain")}347 with mock.patch("os.urandom", return_value=os.urandom(16)):348 boundary = os.urandom(16).hex()344350 headers, stream = encode_request(data={}, files=files)351 assert isinstance(stream, typing.Iterable)345 request = httpx.Request("POST", url, headers=headers, data={}, files=files)346 request.read()347353 content = (354 '--{0}\r\nContent-Disposition: form-data; name="file"; '355 'filename="test.txt"\r\n'356 "Content-Type: text/plain\r\n\r\n<str content>\r\n"357 "--{0}--\r\n"358 "".format(boundary).encode("ascii")359 )360 assert headers == {361 "Content-Type": f"multipart/form-data; boundary={boundary}",362 "Content-Length": str(len(content)),363 }364 assert content == b"".join(stream)348 assert request.headers == {349 "Host": "www.example.com",350 "Content-Type": "multipart/form-data; boundary=BOUNDARY",351 "Content-Length": str(len(request.content)),352 }353 assert request.content == (354 '--BOUNDARY\r\nContent-Disposition: form-data; name="file"; '355 'filename="test.txt"\r\n'356 "Content-Type: text/plain\r\n\r\n<str content>\r\n"357 "--BOUNDARY--\r\n"358 "".encode("ascii")359 )360361362def test_multipart_encode_files_raises_exception_with_StringIO_content() -> None:363 url = "https://www.example.com"364 files = {"file": ("test.txt", io.StringIO("content"), "text/plain")}365 with pytest.raises(TypeError):370 encode_request(data={}, files=files) # type: ignore366 httpx.Request("POST", url, data={}, files=files) # type: ignore367368369def test_multipart_encode_files_raises_exception_with_text_mode_file() -> None:370 url = "https://www.example.com"371 with tempfile.TemporaryFile(mode="w") as upload:372 files = {"file": ("test.txt", upload, "text/plain")}373 with pytest.raises(TypeError):377 encode_request(data={}, files=files) # type: ignore374 httpx.Request("POST", url, data={}, files=files) # type: ignore375376377def test_multipart_encode_non_seekable_filelike() -> None:378 """
12 unmodified lines
391 def data() -> typing.Iterator[bytes]:392 yield b"Hello"393 yield b"World"394395 url = "https://www.example.com/"396 headers = {"Content-Type": "multipart/form-data; boundary=BOUNDARY"}397 fileobj: typing.Any = IteratorIO(data())398 files = {"file": fileobj}400 headers, stream = encode_request(files=files, boundary=b"+++")401 assert isinstance(stream, typing.Iterable)399403 content = (404 b"--+++\r\n"400 request = httpx.Request("POST", url, headers=headers, files=files)401 request.read()402403 assert request.headers == {404 "Host": "www.example.com",405 "Content-Type": "multipart/form-data; boundary=BOUNDARY",406 "Transfer-Encoding": "chunked",407 }408 assert request.content == (409 b"--BOUNDARY\r\n"410 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n'411 b"Content-Type: application/octet-stream\r\n"412 b"\r\n"413 b"HelloWorld\r\n"409 b"--+++--\r\n"414 b"--BOUNDARY--\r\n"415 )411 assert headers == {412 "Content-Type": "multipart/form-data; boundary=+++",413 "Transfer-Encoding": "chunked",414 }415 assert content == b"".join(stream)416417418def test_multipart_rewinds_files():419 with tempfile.TemporaryFile() as upload:
45 unmodified lines