From 857d8eda3a4055d92626f71ff5b34ce8cc6ec167 Mon Sep 17 00:00:00 2001 From: Shivaram Lingamneni Date: Mon, 6 Aug 2012 13:57:04 -0700 Subject: [PATCH 1/4] red tests derived from @dhagrow's examples in #760 --- tests/test_requests.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/test_requests.py b/tests/test_requests.py index 83ca69fd..233daed1 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -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().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().startswith('{')) if __name__ == '__main__': unittest.main() From 030ead9c36b59d2e18f6381f3f8c96c6e4de0090 Mon Sep 17 00:00:00 2001 From: Shivaram Lingamneni Date: Mon, 6 Aug 2012 15:37:28 -0700 Subject: [PATCH 2/4] properly handle send(prefetch=False) fixes RequestsTestSuite.test_prefetch_return_response_interaction --- requests/models.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/requests/models.py b/requests/models.py index 5908928a..5556883e 100644 --- a/requests/models.py +++ b/requests/models.py @@ -458,7 +458,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 +467,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 +629,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 From 000c1053035812ee765a4f4a967eb69dba400a04 Mon Sep 17 00:00:00 2001 From: Shivaram Lingamneni Date: Mon, 6 Aug 2012 15:46:47 -0700 Subject: [PATCH 3/4] propagate self.prefetch on redirect fixes RequestsTestSuite.test_prefetch_redirect_bug --- requests/models.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/requests/models.py b/requests/models.py index 5556883e..d8c0f3e8 100644 --- a/requests/models.py +++ b/requests/models.py @@ -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() From 291859c199c481bf1e304e39613ca90b6f55f9f8 Mon Sep 17 00:00:00 2001 From: Shivaram Lingamneni Date: Mon, 6 Aug 2012 16:11:45 -0700 Subject: [PATCH 4/4] fix the tests for python 3 --- tests/test_requests.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_requests.py b/tests/test_requests.py index 233daed1..dd08b2af 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -986,7 +986,7 @@ class RequestsTestSuite(TestSetup, TestBaseMixin, unittest.TestCase): # this attempt to iterate will crash because the content has already # been read. first_line = next(res.iter_lines()) - self.assertTrue(first_line.strip().startswith('{')) + 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`.""" @@ -994,7 +994,7 @@ class RequestsTestSuite(TestSetup, TestBaseMixin, unittest.TestCase): 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().startswith('{')) + self.assertTrue(first_line.strip().decode('utf-8').startswith('{')) if __name__ == '__main__': unittest.main()