Skip to content

Commit

Permalink
Preserve request headers across multiple requests
Browse files Browse the repository at this point in the history
It is customary in many real-world test suites to do something like

   browser.addHeader('Authorization', 'mgr:mgrpw')

and then expect that to take effect for all subsequent browser.open(...),
browser.getLink(...).click(), browser.getControl(...).submit() calls.
  • Loading branch information
mgedmin committed Mar 27, 2013
1 parent 3af9455 commit 56a8ce2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/zope/testbrowser/browser.py
Expand Up @@ -446,7 +446,6 @@ def _findAllControls(self, forms, include_subcontrols=False):
def _changed(self):
self._counter += 1
self._contents = None
self._req_headers = {}
self._controls = {}
self.__html = None

Expand All @@ -469,6 +468,7 @@ def _preparedRequest(self, url):
extra_environ['wsgi.handleErrors'] = False
extra_environ['paste.throw_errors'] = True
extra_environ['x-wsgiorg.throw_errors'] = True
self._req_headers.pop('X-zope-handle-errors', None)

kwargs = {'headers': sorted(self._req_headers.items()),
'extra_environ': extra_environ,
Expand Down
9 changes: 8 additions & 1 deletion src/zope/testbrowser/tests/test_wsgi.py
Expand Up @@ -101,13 +101,17 @@ def test_handle_errors(self):
self.assertEqual(browser.contents, 'None')
browser.open('http://localhost/echo_one.html?var=wsgi.handleErrors')
self.assertEqual(browser.contents, 'None')
browser.open('http://localhost/echo_one.html?var=HTTP_X_ZOPE_HANDLE_ERRORS')
self.assertEqual(browser.contents, "'True'")
browser.handleErrors = False
browser.open('http://localhost/echo_one.html?var=x-wsgiorg.throw_errors')
self.assertEqual(browser.contents, 'True')
browser.open('http://localhost/echo_one.html?var=paste.throw_errors')
self.assertEqual(browser.contents, 'True')
browser.open('http://localhost/echo_one.html?var=wsgi.handleErrors')
self.assertEqual(browser.contents, 'False')
browser.open('http://localhost/echo_one.html?var=HTTP_X_ZOPE_HANDLE_ERRORS')
self.assertEqual(browser.contents, 'None')

def test_non_ascii_urls(self):
teststr = u'~ひらがな'
Expand Down Expand Up @@ -173,12 +177,15 @@ def test_unwanted_headers(self):
self.unwrapped_browser.open(url)
self.assertTrue('x-powered-by' in self.unwrapped_browser.headers)
self.assertTrue('x-content-type-warning' in self.unwrapped_browser.headers)

def test_authorization(self):
# Basic authorization headers are encoded in base64
self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
self.browser.open('http://localhost/echo_one.html?var=HTTP_AUTHORIZATION')
self.assertEqual(self.browser.contents, repr('Basic bWdyOm1ncnB3'))
# this header persists over multiple requests
self.browser.open('http://localhost/echo_one.html?var=HTTP_AUTHORIZATION')
self.assertEqual(self.browser.contents, repr('Basic bWdyOm1ncnB3'))

def test_authorization_other(self):
# Non-Basic authorization headers are unmolested
Expand Down

0 comments on commit 56a8ce2

Please sign in to comment.