Merge pull request #764 from slingamn/regressions

test and fix regressions from #760
This commit is contained in:
Kenneth Reitz
2012-08-06 16:13:59 -07:00
2 changed files with 25 additions and 3 deletions
+9 -3
View File
@@ -291,7 +291,8 @@ class Request(object):
proxies=self.proxies,
verify=self.verify,
session=self.session,
cert=self.cert
cert=self.cert,
prefetch=self.prefetch,
)
request.send()
@@ -458,7 +459,7 @@ class Request(object):
except ValueError:
return False
def send(self, anyway=False, prefetch=True):
def send(self, anyway=False, prefetch=None):
"""Sends the request. Returns True if successful, False if not.
If there was an HTTPError during transmission,
self.response.status_code will contain the HTTPError code.
@@ -467,6 +468,9 @@ class Request(object):
:param anyway: If True, request will be sent, even if it has
already been sent.
:param prefetch: If not None, will override the request's own setting
for prefetch.
"""
# Build the URL
@@ -626,7 +630,9 @@ class Request(object):
self.__dict__.update(r.__dict__)
# If prefetch is True, mark content as consumed.
if prefetch or self.prefetch:
if prefetch is None:
prefetch = self.prefetch
if prefetch:
# Save the response.
self.response.content
+16
View File
@@ -979,6 +979,22 @@ class RequestsTestSuite(TestSetup, TestBaseMixin, unittest.TestCase):
t = json.loads(r.text)
self.assertEqual(t.get('headers').get('Content-Type'), '')
def test_prefetch_redirect_bug(self):
"""Test that prefetch persists across redirections."""
res = get(httpbin('redirect/2'), prefetch=False)
# prefetch should persist across the redirect; if it doesn't,
# this attempt to iterate will crash because the content has already
# been read.
first_line = next(res.iter_lines())
self.assertTrue(first_line.strip().decode('utf-8').startswith('{'))
def test_prefetch_return_response_interaction(self):
"""Test that prefetch can be overridden as a kwarg to `send`."""
req = requests.get(httpbin('get'), return_response=False)
req.send(prefetch=False)
# content should not have been prefetched, and iter_lines should succeed
first_line = next(req.response.iter_lines())
self.assertTrue(first_line.strip().decode('utf-8').startswith('{'))
if __name__ == '__main__':
unittest.main()