Commit 2bf6e0dd authored by 's avatar

- added some functional tests for exception handling

parent 4ab174db
Exception handling
------------------
These tests capture the current behavior. Maybe some of that behavior should
be changed. The behavior caused by handleErrors=False shows only up in tests.
Create the browser object we'll be using.
>>> from Testing.testbrowser import Browser
>>> browser = Browser()
>>> # XXX: browser has no API for disabling redirects
>>> browser.mech_browser.set_handle_redirect(False)
Create the objects that are raising exceptions.
>>> dummy = app.test_folder_1_._setObject('foo', ExceptionRaiser1())
>>> dummy = app.test_folder_1_._setObject('bar', ExceptionRaiser2())
Handle AttributeError.
>>> app.test_folder_1_.foo.exception = AttributeError('ERROR VALUE')
>>> browser.handleErrors = True
>>> browser.open('http://localhost/test_folder_1_/foo')
Traceback (most recent call last):
...
HTTPError: HTTP Error 500: Internal Server Error
>>> 'Error Type: AttributeError' in browser.contents
True
>>> 'Error Value: ERROR VALUE' in browser.contents
True
>>> browser.handleErrors = False
>>> browser.open('http://localhost/test_folder_1_/foo')
Traceback (most recent call last):
...
AttributeError: ERROR VALUE
>>> browser.contents
Handle ImportError.
>>> app.test_folder_1_.foo.exception = ImportError('ERROR VALUE')
>>> browser.handleErrors = True
>>> browser.open('http://localhost/test_folder_1_/foo')
Traceback (most recent call last):
...
HTTPError: HTTP Error 500: Internal Server Error
>>> 'Error Type: ImportError' in browser.contents
True
>>> 'Error Value: ERROR VALUE' in browser.contents
True
>>> browser.handleErrors = False
>>> browser.open('http://localhost/test_folder_1_/foo')
Traceback (most recent call last):
...
ImportError: ERROR VALUE
>>> browser.contents
Handle zope.publisher.interfaces.NotFound.
>>> from zope.publisher.interfaces import NotFound
>>> app.test_folder_1_.foo.exception = NotFound('OBJECT','NAME')
>>> browser.handleErrors = True
>>> browser.open('http://localhost/test_folder_1_/foo')
Traceback (most recent call last):
...
HTTPError: HTTP Error 404: Not Found
>>> 'Error Type: NotFound' in browser.contents
True
>>> "Error Value: Object: 'OBJECT', name: 'NAME'" in browser.contents
True
>>> browser.handleErrors = False
>>> browser.open('http://localhost/test_folder_1_/foo')
Traceback (most recent call last):
...
NotFound: Object: 'OBJECT', name: 'NAME'
>>> browser.contents
Don't handle SystemExit, even if handleErrors is True.
>>> app.test_folder_1_.foo.exception = SystemExit('ERROR VALUE')
>>> browser.handleErrors = True
>>> browser.open('http://localhost/test_folder_1_/foo')
Traceback (most recent call last):
...
SystemExit: ERROR VALUE
>>> browser.contents
>>> browser.handleErrors = False
>>> browser.open('http://localhost/test_folder_1_/foo')
Traceback (most recent call last):
...
SystemExit: ERROR VALUE
>>> browser.contents
Handle zExceptions.Redirect.
>>> from zExceptions import Redirect
>>> app.test_folder_1_.foo.exception = Redirect('LOCATION')
>>> browser.handleErrors = True
>>> browser.open('http://localhost/test_folder_1_/foo')
Traceback (most recent call last):
...
HTTPError: HTTP Error 302: Moved Temporarily
>>> browser.contents
''
>>> browser.headers['Location']
'LOCATION'
>>> browser.handleErrors = False
>>> browser.open('http://localhost/test_folder_1_/foo')
Traceback (most recent call last):
...
Redirect: LOCATION
>>> browser.contents
Handle zExceptions.Unauthorized.
>>> from zExceptions import Unauthorized
>>> app.test_folder_1_.foo.exception = Unauthorized('ERROR VALUE')
>>> browser.handleErrors = True
>>> browser.open('http://localhost/test_folder_1_/foo')
Traceback (most recent call last):
...
HTTPError: HTTP Error 401: Unauthorized
>>> 'Error Type: Unauthorized' in browser.contents
True
>>> 'Error Value: ERROR VALUE' in browser.contents
True
>>> browser.handleErrors = False
>>> browser.open('http://localhost/test_folder_1_/foo')
Traceback (most recent call last):
...
Unauthorized: ERROR VALUE
>>> browser.contents
Handle zExceptions.Forbidden in BaseRequest.traverse. 'traverse' converts it
into zExceptions.NotFound if we are not in debug mode.
>>> browser.handleErrors = True
>>> browser.open('http://localhost/test_folder_1_/bar')
Traceback (most recent call last):
...
HTTPError: HTTP Error 404: Not Found
>>> '<p><strong>Resource not found</strong></p>' in browser.contents
True
>>> '<p><b>Resource:</b> index_html</p>' in browser.contents
True
>>> browser.handleErrors = False
>>> browser.open('http://localhost/test_folder_1_/bar')
Traceback (most recent call last):
...
NotFound: <h2>Site Error</h2>
...<p><strong>Resource not found</strong></p>...
...<p><b>Resource:</b> index_html</p>...
>>> browser.contents
##############################################################################
#
# Copyright (c) 2010 Zope Foundation and Contributors.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
""" Functional tests for exception handling.
$Id$
"""
import unittest
from Testing.ZopeTestCase import FunctionalDocFileSuite
from OFS.SimpleItem import SimpleItem
class ExceptionRaiser1(SimpleItem):
def index_html(self):
"""DOCSTRING
"""
raise self.exception
class ExceptionRaiser2(SimpleItem):
def index_html(self):
return 'NO DOCSTRING'
def test_suite():
return unittest.TestSuite([
FunctionalDocFileSuite('exception_handling.txt',
globs={'ExceptionRaiser1': ExceptionRaiser1,
'ExceptionRaiser2': ExceptionRaiser2,}),
])
if __name__ == '__main__':
unittest.main(defaultTest='test_suite')
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment