Skip to content

Commit

Permalink
Fix HTTPExceptionHandler to be usable in WSGI pipeline of testbrowser…
Browse files Browse the repository at this point in the history
… tests.

When using a programmatically defined WSGI pipeline which contains `HTTPExceptionHandler` as one of its steps setting `browser.handleErrors = False` was igrnored.
  • Loading branch information
Michael Howitz committed Oct 9, 2017
1 parent c7cbb74 commit e64db69
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.rst
Expand Up @@ -23,6 +23,9 @@ Bugfixes

- Add missing version pin for `Zope2` in `versions-prod.cfg`.

- Fix ``HTTPExceptionHandler`` to be usable as part of the WSGI pipeline in
testbrowser tests.

Other changes
+++++++++++++

Expand Down
27 changes: 27 additions & 0 deletions src/Testing/tests/test_testbrowser.py
Expand Up @@ -15,11 +15,15 @@
"""

from AccessControl.Permissions import view
from ZPublisher.WSGIPublisher import publish_module
from ZPublisher.httpexceptions import HTTPExceptionHandler
from six.moves.urllib.error import HTTPError
from zExceptions import NotFound
import transaction

from OFS.SimpleItem import Item
from Testing.testbrowser import Browser
from Testing.testbrowser import WSGITestApp
from Testing.ZopeTestCase import (
FunctionalTestCase,
user_name,
Expand Down Expand Up @@ -133,6 +137,29 @@ def test_handle_errors_false_redirect(self):
browser.open('http://localhost/test_folder_1_/redirect')
self.assertTrue(browser.contents is None)

def test_handle_errors_false_HTTPExceptionHandler_in_app(self):
"""HTTPExceptionHandler does not handle errors if requested via WSGI.
This is needed when HTTPExceptionHandler is part of the WSGI pipeline.
"""
class WSGITestAppWithHTTPExceptionHandler(object):
"""Minimized testbrowser.WSGITestApp with HTTPExceptionHandler."""

def __call__(self, environ, start_response):
publish = HTTPExceptionHandler(publish_module)
wsgi_result = publish(environ, start_response)

return wsgi_result

self.folder._setObject('stub', ExceptionStub())
transaction.commit()
browser = Browser(wsgi_app=WSGITestAppWithHTTPExceptionHandler())
browser.handleErrors = False

with self.assertRaises(ValueError):
browser.open('http://localhost/test_folder_1_/stub')
self.assertIsNone(browser.contents)

def test_raise_http_errors_false(self):
self.folder._setObject('stub', ExceptionStub())
browser = Browser()
Expand Down
2 changes: 1 addition & 1 deletion src/ZPublisher/httpexceptions.py
Expand Up @@ -34,7 +34,7 @@ def __call__(self, environ, start_response):
except HTTPException as exc:
return exc(environ, start_response)
except Exception as exc:
if self.debug_mode:
if self.debug_mode or environ.get('x-wsgiorg.throw_errors', False):
# In debug mode, let the web server log a real
# traceback
raise
Expand Down

0 comments on commit e64db69

Please sign in to comment.