Drop multipart requirement from tests (#2456)
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 cd1d51d · Browse files · 2 files·+44 added−38 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+44 added−38 removed
- tests:+44 added−37 removed
- other:+0 added−1 removed
Provenance
- Commit
883cf8c- Tree
1a33271- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
cd1d51d
2 files changed+44 added−38 removed
modified:tests/test_multipart.py+44 added-37 removed
3 unmodified lines4 import typing5 from unittest import mock6 7 import pytest8-from multipart import MultipartParser8 9 import httpx10 from httpx._content import encode_request11 from httpx._utils import format_form_param10 unmodified lines22 # Test with a single-value 'data' argument, and a plain file 'files' argument.23 data = {"text": value}24 files = {"file": io.BytesIO(b"<file content>")}25 response = client.post("http://127.0.0.1:8000/", data=data, files=files)27- assert response.status_code == 20028-26 boundary = response.request.headers["Content-Type"].split("boundary=")[-1]30- content_length = int(response.request.headers["Content-Length"])27+ boundary_bytes = boundary.encode("ascii")28 32- multipart = {}33- for part in MultipartParser(34- io.BytesIO(response.content), boundary=boundary, content_length=content_length35- ):36- multipart[part.name] = [part.raw]29+ assert response.status_code == 20030+ assert response.content == b"".join(31+ [32+ b"--" + boundary_bytes + b"\r\n",33+ b'Content-Disposition: form-data; name="text"\r\n',34+ b"\r\n",35+ b"abc\r\n",36+ b"--" + boundary_bytes + b"\r\n",37+ b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',38+ b"Content-Type: application/octet-stream\r\n",39+ b"\r\n",40+ b"<file content>\r\n",41+ b"--" + boundary_bytes + b"--\r\n",42+ ]43+ )44 38- # Note that the expected return type for text fields39- # appears to differs from 3.6 to 3.7+40- assert multipart["text"] == [output.decode()] or multipart["text"] == [output]41- assert multipart["file"] == [b"<file content>"]45 43-46 @pytest.mark.parametrize(47 "header",48 [49 "multipart/form-data; boundary=+++; charset=utf-8",11 unmodified lines61 62 files = {"file": io.BytesIO(b"<file content>")}63 headers = {"content-type": header}64 response = client.post("http://127.0.0.1:8000/", files=files, headers=headers)63- assert response.status_code == 20065+ boundary_bytes = b"+++"66 67+ assert response.status_code == 20068 assert response.request.headers["Content-Type"] == header66- content_length = int(response.request.headers["Content-Length"])69+ assert response.content == b"".join(70+ [71+ b"--" + boundary_bytes + b"\r\n",72+ b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',73+ b"Content-Type: application/octet-stream\r\n",74+ b"\r\n",75+ b"<file content>\r\n",76+ b"--" + boundary_bytes + b"--\r\n",77+ ]78+ )79 68- multipart = {}69- for part in MultipartParser(70- io.BytesIO(response.content), boundary="+++", content_length=content_length71- ):72- multipart[part.name] = [part.raw]80 74- assert multipart["file"] == [b"<file content>"]75-76-81 @pytest.mark.parametrize(82 "header",83 [84 "multipart/form-data; charset=utf-8",45 unmodified lines130 # and a tuple style 'files' argument.131 data = {"text": ["abc"]}132 files = {"file": ("name.txt", io.BytesIO(b"<file content>"))}133 response = client.post("http://127.0.0.1:8000/", data=data, files=files)130- assert response.status_code == 200131-134 boundary = response.request.headers["Content-Type"].split("boundary=")[-1]133- content_length = int(response.request.headers["Content-Length"])135+ boundary_bytes = boundary.encode("ascii")136 135- multipart = {}136- for part in MultipartParser(137- io.BytesIO(response.content), boundary=boundary, content_length=content_length138- ):139- multipart[part.name] = [part.raw]137+ assert response.status_code == 200138+ assert response.content == b"".join(139+ [140+ b"--" + boundary_bytes + b"\r\n",141+ b'Content-Disposition: form-data; name="text"\r\n',142+ b"\r\n",143+ b"abc\r\n",144+ b"--" + boundary_bytes + b"\r\n",145+ b'Content-Disposition: form-data; name="file"; filename="name.txt"\r\n',146+ b"Content-Type: text/plain\r\n",147+ b"\r\n",148+ b"<file content>\r\n",149+ b"--" + boundary_bytes + b"--\r\n",150+ ]151+ )152 141- # Note that the expected return type for text fields142- # appears to differs from 3.6 to 3.7+143- assert multipart["text"] == ["abc"] or multipart["text"] == [b"abc"]144- assert multipart["file"] == [b"<file content>"]153 146-154 @pytest.mark.parametrize("content_type", [None, "text/plain"])155 def test_multipart_file_tuple_headers(content_type: typing.Optional[str]):156 file_name = "test.txt"157 expected_content_type = "text/plain"295 unmodified linesmodified:requirements.txt-1 removed
26 unmodified lines27 flake8-bugbear==22.7.128 flake8-pie==0.16.0; python_version>='3.7'29 importlib-metadata==4.13.0; python_version>='3.7'30 isort==5.10.131-multipart==0.2.431 mypy==0.98232 types-certifi==2021.10.8.233 pytest==7.1.234 pytest-asyncio==0.19.05 unmodified lines