Skip to content
Myr

Merge pull request #580 from slingamn/keepalive

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fix and test for #458

authored by Kenneth Reitz · committed by Kenneth Reitz · parents 317f64a, 35ec6bb · Browse files · 2 files+17 added−2 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+17 added−2 removed

  • tests:+16 added−2 removed
  • other:+1 added−0 removed

2 files+17 added−2 removed

modified:requests/models.py+1 added
524 unmodified lines
525 if self.config.get('keep_alive'):526 conn = self._poolmanager.connection_from_url(url)527 else:528 conn = connectionpool.connection_from_url(url)529 self.headers['Connection'] = 'close'530 except LocationParseError as e:531 raise InvalidURL(e)532533 if url.startswith('https') and self.verify:
303 unmodified lines
modified:tests/test_requests.py+16 added-2 removed
846 unmodified lines
847 r = requests.get(httpbin('status', '404'))848 r.text849850 def test_max_redirects(self):851 """Test the max_redirects config variable, normally and under safe_mode."""852 def unsafe_callable():852 requests.get("http://httpbin.org/redirect/3", config=dict(max_redirects=2))853 requests.get(httpbin('redirect', '3'), config=dict(max_redirects=2))854 self.assertRaises(requests.exceptions.TooManyRedirects, unsafe_callable)855856 # add safe mode856 response = requests.get("http://httpbin.org/redirect/3", config=dict(safe_mode=True, max_redirects=2))857 response = requests.get(httpbin('redirect', '3'), config=dict(safe_mode=True, max_redirects=2))858 self.assertTrue(response.content is None)859 self.assertTrue(isinstance(response.error, requests.exceptions.TooManyRedirects))860861 def test_connection_keepalive_and_close(self):862 """Test that we send 'Connection: close' when keep_alive is disabled."""863 # keep-alive should be on by default864 r1 = requests.get(httpbin('get'))865 # XXX due to proxying issues, test the header sent back by httpbin, rather than866 # the header reported in its message body. See kennethreitz/httpbin#46867 self.assertEqual(r1.headers['Connection'].lower(), 'keep-alive')868869 # but when we disable it, we should send a 'Connection: close'870 # and get the same back:871 r2 = requests.get(httpbin('get'), config=dict(keep_alive=False))872 self.assertEqual(r2.headers['Connection'].lower(), 'close')873874if __name__ == '__main__':875 unittest.main()