Skip to content

Commit

Permalink
Use multipart instead of cgi for multipart tests
Browse files Browse the repository at this point in the history
The cgi module has been deprecated as of python 3.11.

Signed-off-by: Vincent Fazio <vfazio@gmail.com>
  • Loading branch information
vfazio committed Oct 26, 2022
1 parent 9e97d7d commit c710c86
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 25 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ 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.971
types-certifi==2021.10.8.2
pytest==7.1.2
Expand Down
47 changes: 22 additions & 25 deletions tests/test_multipart.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import cgi
import io
import os
import tempfile
import typing
from unittest import mock

import pytest
from multipart import MultipartParser

import httpx
from httpx._content import encode_request
Expand All @@ -26,15 +26,14 @@ def test_multipart(value, output):
response = client.post("http://127.0.0.1:8000/", data=data, files=files)
assert response.status_code == 200

# We're using the cgi module to verify the behavior here, which is a
# bit grungy, but sufficient just for our testing purposes.
boundary = response.request.headers["Content-Type"].split("boundary=")[-1]
content_length = response.request.headers["Content-Length"]
pdict: dict = {
"boundary": boundary.encode("ascii"),
"CONTENT-LENGTH": content_length,
}
multipart = cgi.parse_multipart(io.BytesIO(response.content), pdict)
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+
Expand Down Expand Up @@ -63,15 +62,14 @@ def test_multipart_explicit_boundary(header: str) -> None:
response = client.post("http://127.0.0.1:8000/", files=files, headers=headers)
assert response.status_code == 200

# We're using the cgi module to verify the behavior here, which is a
# bit grungy, but sufficient just for our testing purposes.
assert response.request.headers["Content-Type"] == header
content_length = response.request.headers["Content-Length"]
pdict: dict = {
"boundary": b"+++",
"CONTENT-LENGTH": content_length,
}
multipart = cgi.parse_multipart(io.BytesIO(response.content), pdict)
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>"]

Expand Down Expand Up @@ -131,15 +129,14 @@ def test_multipart_file_tuple():
response = client.post("http://127.0.0.1:8000/", data=data, files=files)
assert response.status_code == 200

# We're using the cgi module to verify the behavior here, which is a
# bit grungy, but sufficient just for our testing purposes.
boundary = response.request.headers["Content-Type"].split("boundary=")[-1]
content_length = response.request.headers["Content-Length"]
pdict: dict = {
"boundary": boundary.encode("ascii"),
"CONTENT-LENGTH": content_length,
}
multipart = cgi.parse_multipart(io.BytesIO(response.content), pdict)
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+
Expand Down

0 comments on commit c710c86

Please sign in to comment.