diff --git a/requests/sessions.py b/requests/sessions.py index b6fb5bb4..8d517ab5 100644 --- a/requests/sessions.py +++ b/requests/sessions.py @@ -229,8 +229,10 @@ class Session(object): r.send(prefetch=prefetch) # Send any cookies back up the to the session. - for cookie in r.response.cookies: - self.cookies.set_cookie(cookie) + # (in safe mode, cookies may be None if the request didn't succeed) + if r.response.cookies is not None: + for cookie in r.response.cookies: + self.cookies.set_cookie(cookie) # Return the response. return r.response diff --git a/tests/test_requests.py b/tests/test_requests.py index fb0d75c0..db176f51 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -847,5 +847,15 @@ class RequestsTestSuite(TestSetup, TestBaseMixin, unittest.TestCase): r = requests.get(httpbin('status', '404')) r.text + def test_max_redirects(self): + def unsafe_callable(): + requests.get("http://httpbin.org/redirect/3", config=dict(max_redirects=2)) + self.assertRaises(requests.exceptions.TooManyRedirects, unsafe_callable) + + # add safe mode + response = requests.get("http://httpbin.org/redirect/3", config=dict(safe_mode=True, max_redirects=2)) + self.assertTrue(response.content is None) + self.assertTrue(isinstance(response.error, requests.exceptions.TooManyRedirects)) + if __name__ == '__main__': unittest.main()