From df641e74ffa294d30f0cc9bf951b7f9614aac18d Mon Sep 17 00:00:00 2001 From: ContinuousFunction Date: Sun, 17 Aug 2014 19:13:26 -0700 Subject: [PATCH 1/2] Tracking Previous Requests Addresses the issue brought up here: https://github.com/kennethreitz/requests/issues/1929 --- requests/sessions.py | 7 +++++++ test_requests.py | 8 ++++++++ 2 files changed, 15 insertions(+) diff --git a/requests/sessions.py b/requests/sessions.py index 4c6fa2f2..a46957ff 100644 --- a/requests/sessions.py +++ b/requests/sessions.py @@ -91,10 +91,17 @@ class SessionRedirectMixin(object): """Receives a Response. Returns a generator of Responses.""" i = 0 + hist = [] #keep track of history while resp.is_redirect: prepared_request = req.copy() + if i > 0: + #create deep copy of the history and keep track of redirects + hist.append(resp) + new_hist = list(hist) + resp.history = new_hist + try: resp.content # Consume socket so it can be released except (ChunkedEncodingError, ContentDecodingError, RuntimeError): diff --git a/test_requests.py b/test_requests.py index c9804517..aa4a2745 100755 --- a/test_requests.py +++ b/test_requests.py @@ -974,6 +974,13 @@ class RequestsTestCase(unittest.TestCase): assert isinstance(s, builtin_str) assert s == "Basic dGVzdDp0ZXN0" + def test_requests_history_is_saved(self): + r = requests.get('https://httpbin.org/redirect/5') + count = 0 + for item in r.history: + assert len(item.history) == count + count = count + 1 + class TestContentEncodingDetection(unittest.TestCase): @@ -1364,6 +1371,7 @@ class TestRedirects: assert session.calls[-1] == send_call + @pytest.fixture def list_of_tuples(): return [ From 2fc6e8a894a5378dd16b2653df9ccf875d0bddf8 Mon Sep 17 00:00:00 2001 From: ContinuousFunction Date: Tue, 19 Aug 2014 10:51:26 -0700 Subject: [PATCH 2/2] Update on Tracking Previous Requests Modified the comment in sessions.py and rewrote the test in test_requests.py --- requests/sessions.py | 2 +- test_requests.py | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/requests/sessions.py b/requests/sessions.py index a46957ff..758e6eab 100644 --- a/requests/sessions.py +++ b/requests/sessions.py @@ -97,7 +97,7 @@ class SessionRedirectMixin(object): prepared_request = req.copy() if i > 0: - #create deep copy of the history and keep track of redirects + #update history and keep track of redirects hist.append(resp) new_hist = list(hist) resp.history = new_hist diff --git a/test_requests.py b/test_requests.py index aa4a2745..34ebd8ca 100755 --- a/test_requests.py +++ b/test_requests.py @@ -976,10 +976,11 @@ class RequestsTestCase(unittest.TestCase): def test_requests_history_is_saved(self): r = requests.get('https://httpbin.org/redirect/5') - count = 0 + total = r.history[-1].history + i = 0 for item in r.history: - assert len(item.history) == count - count = count + 1 + assert item.history == total[0:i] + i=i+1 class TestContentEncodingDetection(unittest.TestCase):