Skip to content

Commit

Permalink
Merge pull request #85 from cjwatson/follow-redirects-option
Browse files Browse the repository at this point in the history
Add Browser.followRedirects option
  • Loading branch information
cjwatson committed Nov 10, 2019
2 parents 784e0c6 + 43d9134 commit 7448fa3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ CHANGES
subsequent queries could use data from the wrong response. See `issue 83
<https://github.com/zopefoundation/zope.testbrowser/issues/83>`_.

- Support telling the browser not to follow redirects by setting
``Browser.follow_redirects`` to False. See `issue 79
<https://github.com/zopefoundation/zope.testbrowser/issues/79>`_.


5.4.0 (2019-11-01)
------------------
Expand Down
17 changes: 10 additions & 7 deletions src/zope/testbrowser/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ def __init__(self, url=None, wsgi_app=None):
self.timer = Timer()
self.raiseHttpErrors = True
self.handleErrors = True
self.followRedirects = True

if wsgi_app is None:
self.testapp = TestbrowserApp(TransparentProxy())
Expand Down Expand Up @@ -273,13 +274,15 @@ def _processRequest(self, url, make_request):
with self._preparedRequest(url) as reqargs:
self._history.add(self._response)
resp = make_request(reqargs)
remaining_redirects = 100 # infinite loops protection
while resp.status_int in REDIRECTS and remaining_redirects:
remaining_redirects -= 1
url = urlparse.urljoin(url, resp.headers['location'])
with self._preparedRequest(url) as reqargs:
resp = self.testapp.get(url, **reqargs)
assert remaining_redirects > 0, "redirects chain looks infinite"
if self.followRedirects:
remaining_redirects = 100 # infinite loops protection
while resp.status_int in REDIRECTS and remaining_redirects:
remaining_redirects -= 1
url = urlparse.urljoin(url, resp.headers['location'])
with self._preparedRequest(url) as reqargs:
resp = self.testapp.get(url, **reqargs)
assert remaining_redirects > 0, (
"redirects chain looks infinite")
self._setResponse(resp)
self._checkStatus()

Expand Down
19 changes: 19 additions & 0 deletions src/zope/testbrowser/tests/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,25 @@ def test_relative_redirect(self):
"""


def test_disable_following_redirects(self):
"""
If followRedirects is False, the browser does not follow redirects.
>>> app = YetAnotherTestApp()
>>> browser = Browser(wsgi_app=app)
>>> redirect = ('Location', 'http://localhost/the_thing')
>>> app.add_response(b"Moved", headers=[redirect],
... status=302, reason='Found')
>>> browser.followRedirects = False
>>> browser.open('http://localhost/')
>>> browser.headers['Status']
'302 Found'
>>> browser.headers['Location']
'http://localhost/the_thing'
"""


def test_relative_open_allowed_after_non_html_page(self):
"""
>>> app = YetAnotherTestApp()
Expand Down

0 comments on commit 7448fa3

Please sign in to comment.