]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Drop multipart requirement from tests (#2456)
authorTom Christie <tom@tomchristie.com>
Mon, 21 Nov 2022 10:29:31 +0000 (10:29 +0000)
committerGitHub <noreply@github.com>
Mon, 21 Nov 2022 10:29:31 +0000 (10:29 +0000)
requirements.txt
tests/test_multipart.py

index dc87c7dd51667405a5e700324eec2f9f3e7080c2..95e17ed4588df6b6fe4964f80c5debb511ff4a06 100644 (file)
@@ -28,7 +28,6 @@ flake8-bugbear==22.7.1
 flake8-pie==0.16.0; python_version>='3.7'
 importlib-metadata==4.13.0; python_version>='3.7'
 isort==5.10.1
-multipart==0.2.4
 mypy==0.982
 types-certifi==2021.10.8.2
 pytest==7.1.2
index 1d9c12feeaea325af12ac13cca0f9ee4be529093..80b4588c69179e4a7c90c2317269a33fdfa40312 100644 (file)
@@ -5,7 +5,6 @@ import typing
 from unittest import mock
 
 import pytest
-from multipart import MultipartParser
 
 import httpx
 from httpx._content import encode_request
@@ -24,21 +23,24 @@ def test_multipart(value, output):
     data = {"text": value}
     files = {"file": io.BytesIO(b"<file content>")}
     response = client.post("http://127.0.0.1:8000/", data=data, files=files)
-    assert response.status_code == 200
-
     boundary = response.request.headers["Content-Type"].split("boundary=")[-1]
-    content_length = int(response.request.headers["Content-Length"])
+    boundary_bytes = boundary.encode("ascii")
 
-    multipart = {}
-    for part in MultipartParser(
-        io.BytesIO(response.content), boundary=boundary, content_length=content_length
-    ):
-        multipart[part.name] = [part.raw]
-
-    # Note that the expected return type for text fields
-    # appears to differs from 3.6 to 3.7+
-    assert multipart["text"] == [output.decode()] or multipart["text"] == [output]
-    assert multipart["file"] == [b"<file content>"]
+    assert response.status_code == 200
+    assert response.content == b"".join(
+        [
+            b"--" + boundary_bytes + b"\r\n",
+            b'Content-Disposition: form-data; name="text"\r\n',
+            b"\r\n",
+            b"abc\r\n",
+            b"--" + boundary_bytes + b"\r\n",
+            b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',
+            b"Content-Type: application/octet-stream\r\n",
+            b"\r\n",
+            b"<file content>\r\n",
+            b"--" + boundary_bytes + b"--\r\n",
+        ]
+    )
 
 
 @pytest.mark.parametrize(
@@ -60,18 +62,20 @@ def test_multipart_explicit_boundary(header: str) -> None:
     files = {"file": io.BytesIO(b"<file content>")}
     headers = {"content-type": header}
     response = client.post("http://127.0.0.1:8000/", files=files, headers=headers)
-    assert response.status_code == 200
+    boundary_bytes = b"+++"
 
+    assert response.status_code == 200
     assert response.request.headers["Content-Type"] == header
-    content_length = int(response.request.headers["Content-Length"])
-
-    multipart = {}
-    for part in MultipartParser(
-        io.BytesIO(response.content), boundary="+++", content_length=content_length
-    ):
-        multipart[part.name] = [part.raw]
-
-    assert multipart["file"] == [b"<file content>"]
+    assert response.content == b"".join(
+        [
+            b"--" + boundary_bytes + b"\r\n",
+            b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',
+            b"Content-Type: application/octet-stream\r\n",
+            b"\r\n",
+            b"<file content>\r\n",
+            b"--" + boundary_bytes + b"--\r\n",
+        ]
+    )
 
 
 @pytest.mark.parametrize(
@@ -127,21 +131,24 @@ def test_multipart_file_tuple():
     data = {"text": ["abc"]}
     files = {"file": ("name.txt", io.BytesIO(b"<file content>"))}
     response = client.post("http://127.0.0.1:8000/", data=data, files=files)
-    assert response.status_code == 200
-
     boundary = response.request.headers["Content-Type"].split("boundary=")[-1]
-    content_length = int(response.request.headers["Content-Length"])
-
-    multipart = {}
-    for part in MultipartParser(
-        io.BytesIO(response.content), boundary=boundary, content_length=content_length
-    ):
-        multipart[part.name] = [part.raw]
-
-    # Note that the expected return type for text fields
-    # appears to differs from 3.6 to 3.7+
-    assert multipart["text"] == ["abc"] or multipart["text"] == [b"abc"]
-    assert multipart["file"] == [b"<file content>"]
+    boundary_bytes = boundary.encode("ascii")
+
+    assert response.status_code == 200
+    assert response.content == b"".join(
+        [
+            b"--" + boundary_bytes + b"\r\n",
+            b'Content-Disposition: form-data; name="text"\r\n',
+            b"\r\n",
+            b"abc\r\n",
+            b"--" + boundary_bytes + b"\r\n",
+            b'Content-Disposition: form-data; name="file"; filename="name.txt"\r\n',
+            b"Content-Type: text/plain\r\n",
+            b"\r\n",
+            b"<file content>\r\n",
+            b"--" + boundary_bytes + b"--\r\n",
+        ]
+    )
 
 
 @pytest.mark.parametrize("content_type", [None, "text/plain"])