Skip to content

Commit cb233a1

Browse files
authored
Fix several ResourceWarnings in test_requests.py (psf#4766)
1 parent 4095672 commit cb233a1

File tree

1 file changed

+44
-38
lines changed

1 file changed

+44
-38
lines changed

tests/test_requests.py

+44-38
Original file line numberDiff line numberDiff line change
@@ -1022,12 +1022,13 @@ def test_form_encoded_post_query_multivalued_element(self, httpbin):
10221022
assert prep.body == "test=foo&test=baz"
10231023

10241024
def test_different_encodings_dont_break_post(self, httpbin):
1025-
r = requests.post(
1026-
httpbin("post"),
1027-
data={"stuff": json.dumps({"a": 123})},
1028-
params={"blah": "asdf1234"},
1029-
files={"file": ("test_requests.py", open(__file__, "rb"))},
1030-
)
1025+
with open(__file__, "rb") as f:
1026+
r = requests.post(
1027+
httpbin("post"),
1028+
data={"stuff": json.dumps({"a": 123})},
1029+
params={"blah": "asdf1234"},
1030+
files={"file": ("test_requests.py", f)},
1031+
)
10311032
assert r.status_code == 200
10321033

10331034
@pytest.mark.parametrize(
@@ -1040,35 +1041,44 @@ def test_different_encodings_dont_break_post(self, httpbin):
10401041
),
10411042
)
10421043
def test_unicode_multipart_post(self, httpbin, data):
1043-
r = requests.post(
1044-
httpbin("post"),
1045-
data=data,
1046-
files={"file": ("test_requests.py", open(__file__, "rb"))},
1047-
)
1044+
with open(__file__, "rb") as f:
1045+
r = requests.post(
1046+
httpbin("post"),
1047+
data=data,
1048+
files={"file": ("test_requests.py", f)},
1049+
)
10481050
assert r.status_code == 200
10491051

10501052
def test_unicode_multipart_post_fieldnames(self, httpbin):
10511053
filename = os.path.splitext(__file__)[0] + ".py"
1052-
r = requests.Request(
1053-
method="POST",
1054-
url=httpbin("post"),
1055-
data={b"stuff": "elixr"},
1056-
files={"file": ("test_requests.py", open(filename, "rb"))},
1057-
)
1058-
prep = r.prepare()
1054+
with open(filename, "rb") as f:
1055+
r = requests.Request(
1056+
method="POST",
1057+
url=httpbin("post"),
1058+
data={b"stuff": "elixr"},
1059+
files={"file": ("test_requests.py", f)},
1060+
)
1061+
prep = r.prepare()
1062+
10591063
assert b'name="stuff"' in prep.body
10601064
assert b"name=\"b'stuff'\"" not in prep.body
10611065

10621066
def test_unicode_method_name(self, httpbin):
1063-
files = {"file": open(__file__, "rb")}
1064-
r = requests.request(method="POST", url=httpbin("post"), files=files)
1067+
with open(__file__, "rb") as f:
1068+
files = {"file": f}
1069+
r = requests.request(
1070+
method="POST",
1071+
url=httpbin("post"),
1072+
files=files,
1073+
)
10651074
assert r.status_code == 200
10661075

10671076
def test_unicode_method_name_with_request_object(self, httpbin):
1068-
files = {"file": open(__file__, "rb")}
10691077
s = requests.Session()
1070-
req = requests.Request("POST", httpbin("post"), files=files)
1071-
prep = s.prepare_request(req)
1078+
with open(__file__, "rb") as f:
1079+
files = {"file": f}
1080+
req = requests.Request("POST", httpbin("post"), files=files)
1081+
prep = s.prepare_request(req)
10721082
assert isinstance(prep.method, builtin_str)
10731083
assert prep.method == "POST"
10741084

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

10861096
def test_custom_content_type(self, httpbin):
1087-
r = requests.post(
1088-
httpbin("post"),
1089-
data={"stuff": json.dumps({"a": 123})},
1090-
files={
1091-
"file1": ("test_requests.py", open(__file__, "rb")),
1092-
"file2": (
1093-
"test_requests",
1094-
open(__file__, "rb"),
1095-
"text/py-content-type",
1096-
),
1097-
},
1098-
)
1097+
with open(__file__, "rb") as f1:
1098+
with open(__file__, "rb") as f2:
1099+
data={"stuff": json.dumps({"a": 123})}
1100+
files = {
1101+
"file1": ("test_requests.py", f1),
1102+
"file2": ("test_requests", f2, "text/py-content-type"),
1103+
}
1104+
r = requests.post(httpbin("post"), data=data, files=files)
10991105
assert r.status_code == 200
11001106
assert b"text/py-content-type" in r.request.body
11011107

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

14861492
def test_prepared_request_with_file_is_pickleable(self, httpbin):
1487-
files = {"file": open(__file__, "rb")}
1488-
r = requests.Request("POST", httpbin("post"), files=files)
1489-
p = r.prepare()
1493+
with open(__file__, "rb") as f:
1494+
r = requests.Request("POST", httpbin("post"), files={"file": f})
1495+
p = r.prepare()
14901496

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

0 commit comments

Comments
 (0)