Merge pull request #764 from slingamn/regressions
Before adoption: this commit came in with the history the project adopted, so no landing records it.
test and fix regressions from #760
authored by Kenneth Reitz · committed by Kenneth Reitz · parents a94fcb8, 291859c · Browse files · 2 files·+25 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+25 added−3 removed
- tests:+16 added−0 removed
- other:+9 added−3 removed
Provenance
- Commit
bc7b2f9- Tree
41107e0- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent 1
a94fcb8- Parent 2
291859c
2 files changed+25 added−3 removed
modified:requests/models.py+9 added-3 removed
289 unmodified lines290 _poolmanager=self._poolmanager,291 proxies=self.proxies,292 verify=self.verify,293 session=self.session,294- cert=self.cert294+ cert=self.cert,295+ prefetch=self.prefetch,296 )297 298 request.send()299 r = request.response158 unmodified lines458 return True459 except ValueError:460 return False461 461- def send(self, anyway=False, prefetch=True):462+ def send(self, anyway=False, prefetch=None):463 """Sends the request. Returns True if successful, False if not.464 If there was an HTTPError during transmission,465 self.response.status_code will contain the HTTPError code.466 467 Once a request is successfully sent, `sent` will equal True.468 469 :param anyway: If True, request will be sent, even if it has470 already been sent.471+472+ :param prefetch: If not None, will override the request's own setting473+ for prefetch.474 """475 476 # Build the URL477 url = self.full_url151 unmodified lines629 r = dispatch_hook('post_request', self.hooks, self)630 self.__dict__.update(r.__dict__)631 632 # If prefetch is True, mark content as consumed.629- if prefetch or self.prefetch:633+ if prefetch is None:634+ prefetch = self.prefetch635+ if prefetch:636 # Save the response.637 self.response.content638 639 if self.config.get('danger_mode'):212 unmodified linesmodified:tests/test_requests.py+16 added
977 unmodified lines978 r = post(httpbin('post'), data=data)979 t = json.loads(r.text)980 self.assertEqual(t.get('headers').get('Content-Type'), '')981 982+ def test_prefetch_redirect_bug(self):983+ """Test that prefetch persists across redirections."""984+ res = get(httpbin('redirect/2'), prefetch=False)985+ # prefetch should persist across the redirect; if it doesn't,986+ # this attempt to iterate will crash because the content has already987+ # been read.988+ first_line = next(res.iter_lines())989+ self.assertTrue(first_line.strip().decode('utf-8').startswith('{'))990 991+ def test_prefetch_return_response_interaction(self):992+ """Test that prefetch can be overridden as a kwarg to `send`."""993+ req = requests.get(httpbin('get'), return_response=False)994+ req.send(prefetch=False)995+ # content should not have been prefetched, and iter_lines should succeed996+ first_line = next(req.response.iter_lines())997+ self.assertTrue(first_line.strip().decode('utf-8').startswith('{'))998+999 if __name__ == '__main__':1000 unittest.main()