Skip to content
Myr

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

2 files+44 added−38 removed

modified:tests/test_multipart.py+44 added-37 removed
3 unmodified lines
4import typing5from unittest import mock67import pytest8from multipart import MultipartParser89import httpx10from httpx._content import encode_request11from httpx._utils import format_form_param
10 unmodified lines
22 # 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 == 2002826 boundary = response.request.headers["Content-Type"].split("boundary=")[-1]30 content_length = int(response.request.headers["Content-Length"])27 boundary_bytes = boundary.encode("ascii")2832 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 )4438 # 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>"]454346@pytest.mark.parametrize(47 "header",48 [49 "multipart/form-data; boundary=+++; charset=utf-8",
11 unmodified lines
6162 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"+++"6667 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 )7968 multipart = {}69 for part in MultipartParser(70 io.BytesIO(response.content), boundary="+++", content_length=content_length71 ):72 multipart[part.name] = [part.raw]8074 assert multipart["file"] == [b"<file content>"]757681@pytest.mark.parametrize(82 "header",83 [84 "multipart/form-data; charset=utf-8",
45 unmodified lines
130 # 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 == 200131134 boundary = response.request.headers["Content-Type"].split("boundary=")[-1]133 content_length = int(response.request.headers["Content-Length"])135 boundary_bytes = boundary.encode("ascii")136135 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 )152141 # 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>"]153146154@pytest.mark.parametrize("content_type", [None, "text/plain"])155def test_multipart_file_tuple_headers(content_type: typing.Optional[str]):156 file_name = "test.txt"157 expected_content_type = "text/plain"
295 unmodified lines
modified:requirements.txt-1 removed
26 unmodified lines
27flake8-bugbear==22.7.128flake8-pie==0.16.0; python_version>='3.7'29importlib-metadata==4.13.0; python_version>='3.7'30isort==5.10.131multipart==0.2.431mypy==0.98232types-certifi==2021.10.8.233pytest==7.1.234pytest-asyncio==0.19.0
5 unmodified lines