Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #53 from dreid/add-response-history

Add response history
  • Loading branch information...
commit 3ae51c7c85472414f886480907c11a7d57cdbbfa 2 parents c9187e5 + aeea5eb
@dreid dreid authored
View
12 docs/examples/disable_redirects.py
@@ -0,0 +1,12 @@
+from twisted.internet.task import react
+from _utils import print_response
+
+import treq
+
+
+def main(reactor, *args):
+ d = treq.get('http://httpbin.org/redirect/1', allow_redirects=False)
+ d.addCallback(print_response)
+ return d
+
+react(main, [])
View
12 docs/examples/redirects.py
@@ -0,0 +1,12 @@
+from twisted.internet.task import react
+from _utils import print_response
+
+import treq
+
+
+def main(reactor, *args):
+ d = treq.get('http://httpbin.org/redirect/1')
+ d.addCallback(print_response)
+ return d
+
+react(main, [])
View
17 docs/examples/response_history.py
@@ -0,0 +1,17 @@
+from twisted.internet.task import react
+from _utils import print_response
+
+import treq
+
+
+def main(reactor, *args):
+ d = treq.get('http://httpbin.org/redirect/1')
+
+ def cb(response):
+ print 'Response history:', response.history()
+ return print_response(response)
+
+ d.addCallback(cb)
+ return d
+
+react(main, [])
View
32 docs/howto.rst
@@ -38,7 +38,6 @@ or a ``list`` of ``str`` values.
Full example: :download:`download_file.py <examples/download_file.py>`
-
Auth
----
@@ -54,3 +53,34 @@ The ``auth`` argument should be a tuple of the form ``('username', 'password')``
Full example: :download:`download_file.py <examples/download_file.py>`
.. _RFC 2617: http://www.ietf.org/rfc/rfc2617.txt
+
+Redirects
+---------
+
+treq handles redirects by default.
+
+The following will print a 200 OK response.
+
+.. literalinclude:: examples/redirects.py
+ :linenos:
+ :lines: 7-13
+
+Full example: :download:`redirects.py <examples/redirects.py>`
+
+You can easily disable redirects by simply passing `allow_redirects=False` to
+any of the request methods.
+
+.. literalinclude:: examples/disable_redirects.py
+ :linenos:
+ :lines: 7-13
+
+Full example: :download:`disable_redirects.py <examples/disable_redirects.py>`
+
+You can even access the complete history of treq response objects by calling
+the `history()` method on the the response.
+
+.. literalinclude:: examples/response_history.py
+ :linenos:
+ :lines: 7-15
+
+Full example: :download:`response_history.py <examples/response_history.py>`
View
15 treq/response.py
@@ -13,3 +13,18 @@ def json(self, *args, **kwargs):
def text(self, *args, **kwargs):
return text_content(self.original, *args, **kwargs)
+
+ def history(self):
+ if not hasattr(self, "previousResponse"):
+ raise NotImplementedError(
+ "Twisted < 13.1.0 does not support response history.")
+
+ response = self
+ history = []
+
+ while response.previousResponse is not None:
+ history.append(_Response(response.previousResponse))
+ response = response.previousResponse
+
+ history.reverse()
+ return history
View
64 treq/test/test_response.py
@@ -0,0 +1,64 @@
+from twisted.trial.unittest import TestCase
+
+from twisted import version
+from twisted.python.versions import Version
+
+from twisted.web.http_headers import Headers
+
+from treq.response import _Response
+
+
+skip_history = None
+
+if version < Version("twisted", 13, 1, 0):
+ skip_history = "Response history not supported on Twisted < 13.1.0."
+
+
+class FakeResponse(object):
+ def __init__(self, code, headers):
+ self.code = code
+ self.headers = headers
+ self.previousResponse = None
+
+ def setPreviousResponse(self, response):
+ self.previousResponse = response
+
+
+class ResponseTests(TestCase):
+ def test_history(self):
+ redirect1 = FakeResponse(
+ 301,
+ Headers({'location': ['http://example.com/']})
+ )
+
+ redirect2 = FakeResponse(
+ 302,
+ Headers({'location': ['https://example.com/']})
+ )
+ redirect2.setPreviousResponse(redirect1)
+
+ final = FakeResponse(200, Headers({}))
+ final.setPreviousResponse(redirect2)
+
+ wrapper = _Response(final)
+
+ history = wrapper.history()
+
+ self.assertEqual(wrapper.code, 200)
+ self.assertEqual(history[0].code, 301)
+ self.assertEqual(history[1].code, 302)
+
+ def test_no_history(self):
+ wrapper = _Response(FakeResponse(200, Headers({})))
+ self.assertEqual(wrapper.history(), [])
+
+ if skip_history:
+ test_history.skip = skip_history
+ test_no_history.skip = skip_history
+
+ def test_history_notimplemented(self):
+ wrapper = _Response(FakeResponse(200, Headers({})))
+ self.assertRaises(NotImplementedError, wrapper.history)
+
+ if not skip_history:
+ test_history_notimplemented.skip = "History supported."
Please sign in to comment.
Something went wrong with that request. Please try again.