Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix several ResourceWarnings in test_requests.py #4766

Merged
merged 1 commit into from May 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
82 changes: 44 additions & 38 deletions tests/test_requests.py
Expand Up @@ -1022,12 +1022,13 @@ def test_form_encoded_post_query_multivalued_element(self, httpbin):
assert prep.body == "test=foo&test=baz"

def test_different_encodings_dont_break_post(self, httpbin):
r = requests.post(
httpbin("post"),
data={"stuff": json.dumps({"a": 123})},
params={"blah": "asdf1234"},
files={"file": ("test_requests.py", open(__file__, "rb"))},
)
with open(__file__, "rb") as f:
r = requests.post(
httpbin("post"),
data={"stuff": json.dumps({"a": 123})},
params={"blah": "asdf1234"},
files={"file": ("test_requests.py", f)},
)
assert r.status_code == 200

@pytest.mark.parametrize(
Expand All @@ -1040,35 +1041,44 @@ def test_different_encodings_dont_break_post(self, httpbin):
),
)
def test_unicode_multipart_post(self, httpbin, data):
r = requests.post(
httpbin("post"),
data=data,
files={"file": ("test_requests.py", open(__file__, "rb"))},
)
with open(__file__, "rb") as f:
r = requests.post(
httpbin("post"),
data=data,
files={"file": ("test_requests.py", f)},
)
assert r.status_code == 200

def test_unicode_multipart_post_fieldnames(self, httpbin):
filename = os.path.splitext(__file__)[0] + ".py"
r = requests.Request(
method="POST",
url=httpbin("post"),
data={b"stuff": "elixr"},
files={"file": ("test_requests.py", open(filename, "rb"))},
)
prep = r.prepare()
with open(filename, "rb") as f:
r = requests.Request(
method="POST",
url=httpbin("post"),
data={b"stuff": "elixr"},
files={"file": ("test_requests.py", f)},
)
prep = r.prepare()

assert b'name="stuff"' in prep.body
assert b"name=\"b'stuff'\"" not in prep.body

def test_unicode_method_name(self, httpbin):
files = {"file": open(__file__, "rb")}
r = requests.request(method="POST", url=httpbin("post"), files=files)
with open(__file__, "rb") as f:
files = {"file": f}
r = requests.request(
method="POST",
url=httpbin("post"),
files=files,
)
assert r.status_code == 200

def test_unicode_method_name_with_request_object(self, httpbin):
files = {"file": open(__file__, "rb")}
s = requests.Session()
req = requests.Request("POST", httpbin("post"), files=files)
prep = s.prepare_request(req)
with open(__file__, "rb") as f:
files = {"file": f}
req = requests.Request("POST", httpbin("post"), files=files)
prep = s.prepare_request(req)
assert isinstance(prep.method, builtin_str)
assert prep.method == "POST"

Expand All @@ -1084,18 +1094,14 @@ def test_non_prepared_request_error(self):
assert str(e.value) == "You can only send PreparedRequests."

def test_custom_content_type(self, httpbin):
r = requests.post(
httpbin("post"),
data={"stuff": json.dumps({"a": 123})},
files={
"file1": ("test_requests.py", open(__file__, "rb")),
"file2": (
"test_requests",
open(__file__, "rb"),
"text/py-content-type",
),
},
)
with open(__file__, "rb") as f1:
with open(__file__, "rb") as f2:
data={"stuff": json.dumps({"a": 123})}
files = {
"file1": ("test_requests.py", f1),
"file2": ("test_requests", f2, "text/py-content-type"),
}
r = requests.post(httpbin("post"), data=data, files=files)
assert r.status_code == 200
assert b"text/py-content-type" in r.request.body

Expand Down Expand Up @@ -1484,9 +1490,9 @@ def test_prepared_request_is_pickleable(self, httpbin):
assert resp.status_code == 200

def test_prepared_request_with_file_is_pickleable(self, httpbin):
files = {"file": open(__file__, "rb")}
r = requests.Request("POST", httpbin("post"), files=files)
p = r.prepare()
with open(__file__, "rb") as f:
r = requests.Request("POST", httpbin("post"), files={"file": f})
p = r.prepare()

# Verify PreparedRequest can be pickled and unpickled
r = pickle.loads(pickle.dumps(p))
Expand Down