Fix several ResourceWarnings in test_requests.py (#4766)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Mickaël Schoentgen · committed by GitHub · parent 4095672 · Browse files · 1 file·+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
1 file+44 added−38 removed
- tests:+44 added−38 removed
Provenance
- Commit
cb233a1- Tree
f4c2016- Author
Mickaël Schoentgen <contact@tiger-222.fr>- Committer
GitHub <noreply@github.com>- Parent
4095672
1 file changed+44 added−38 removed
modified:tests/test_requests.py+44 added-38 removed
1020 unmodified lines1021 prep = r.prepare()1022 assert prep.body == "test=foo&test=baz"1023 1024 def test_different_encodings_dont_break_post(self, httpbin):1025+ with open(__file__, "rb") as f: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- )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+ )1032 assert r.status_code == 2001033 1034 @pytest.mark.parametrize(1035 "data",4 unmodified lines1040 {"stuff": b"elixr"},1041 ),1042 )1043 def test_unicode_multipart_post(self, httpbin, data):1044+ with open(__file__, "rb") as f:1043- r = requests.post(1044- httpbin("post"),1045- data=data,1046- files={"file": ("test_requests.py", open(__file__, "rb"))},1047- )1045+ r = requests.post(1046+ httpbin("post"),1047+ data=data,1048+ files={"file": ("test_requests.py", f)},1049+ )1050 assert r.status_code == 2001051 1052 def test_unicode_multipart_post_fieldnames(self, httpbin):1053 filename = os.path.splitext(__file__)[0] + ".py"1054+ with open(filename, "rb") as f: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()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+1063 assert b'name="stuff"' in prep.body1064 assert b"name=\"b'stuff'\"" not in prep.body1065 1066 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+ )1074 assert r.status_code == 2001075 1076 def test_unicode_method_name_with_request_object(self, httpbin):1068- files = {"file": open(__file__, "rb")}1077 s = requests.Session()1078+ with open(__file__, "rb") as f:1079+ files = {"file": f}1070- req = requests.Request("POST", httpbin("post"), files=files)1071- prep = s.prepare_request(req)1080+ req = requests.Request("POST", httpbin("post"), files=files)1081+ prep = s.prepare_request(req)1082 assert isinstance(prep.method, builtin_str)1083 assert prep.method == "POST"1084 1085 resp = s.send(prep)7 unmodified lines1093 s.send(req)1094 assert str(e.value) == "You can only send PreparedRequests."1095 1096 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)1105 assert r.status_code == 2001106 assert b"text/py-content-type" in r.request.body1107 1108 def test_hook_receives_request_arguments(self, httpbin):380 unmodified lines1489 resp = s.send(r)1490 assert resp.status_code == 2001491 1492 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()1496 1497 # Verify PreparedRequest can be pickled and unpickled1498 r = pickle.loads(pickle.dumps(p))1499 assert r.url == p.url1259 unmodified lines