Skip to content

Commit

Permalink
Fix several ResourceWarnings in test_requests.py
Browse files Browse the repository at this point in the history
  • Loading branch information
BoboTiG committed Oct 1, 2018
1 parent a6cd380 commit 2aced7e
Showing 1 changed file with 45 additions and 42 deletions.
87 changes: 45 additions & 42 deletions tests/test_requests.py
Expand Up @@ -875,10 +875,11 @@ 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 @@ -889,37 +890,39 @@ def test_different_encodings_dont_break_post(self, httpbin):
{'stuff': 'elixr'.encode('utf-8')},
))
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={'stuff'.encode('utf-8'): '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={'stuff'.encode('utf-8'): '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=u('POST'), url=httpbin('post'), files=files)
with open(__file__, 'rb') as f:
r = requests.request(
method=u('POST'), url=httpbin('post'), files={'file': f})
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(u('POST'), httpbin('post'), files=files)
prep = s.prepare_request(req)
assert isinstance(prep.method, builtin_str)
assert prep.method == 'POST'
with open(__file__, 'rb') as f:
s = requests.Session()
req = requests.Request(u('POST'), httpbin('post'), files={'file': f})
prep = s.prepare_request(req)
assert isinstance(prep.method, builtin_str)
assert prep.method == 'POST'

resp = s.send(prep)
assert resp.status_code == 200
resp = s.send(prep)
assert resp.status_code == 200

def test_non_prepared_request_error(self):
s = requests.Session()
Expand All @@ -930,13 +933,13 @@ 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, open(__file__, 'rb') as f2:
r = requests.post(
httpbin('post'),
data={'stuff': json.dumps({'a': 123})},
files={
'file1': ('test_requests.py', f1),
'file2': ('test_requests', f2, 'text/py-content-type')})
assert r.status_code == 200
assert b"text/py-content-type" in r.request.body

Expand Down Expand Up @@ -1294,20 +1297,20 @@ 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))
assert r.url == p.url
assert r.headers == p.headers
assert r.body == p.body
# Verify PreparedRequest can be pickled and unpickled
r = pickle.loads(pickle.dumps(p))
assert r.url == p.url
assert r.headers == p.headers
assert r.body == p.body

# Verify unpickled PreparedRequest sends properly
s = requests.Session()
resp = s.send(r)
assert resp.status_code == 200
# Verify unpickled PreparedRequest sends properly
s = requests.Session()
resp = s.send(r)
assert resp.status_code == 200

def test_prepared_request_with_hook_is_pickleable(self, httpbin):
r = requests.Request('GET', httpbin('get'), hooks=default_hooks())
Expand Down

0 comments on commit 2aced7e

Please sign in to comment.