[Zope3-checkins] SVN: Zope3/branches/testbrowser-integration/src/zope/app/testing/testbrowser/ Added new property ``handleErrors`` to the test browser. When set to

Stephan Richter srichter at cosmos.phy.tufts.edu
Wed Jul 27 11:43:00 EDT 2005


Log message for revision 37491:
  Added new property ``handleErrors`` to the test browser. When set to 
  ``False``, the publisher will not handle application exceptions. As you 
  all know this is very useful for debugging.
  
  

Changed:
  U   Zope3/branches/testbrowser-integration/src/zope/app/testing/testbrowser/README.txt
  U   Zope3/branches/testbrowser-integration/src/zope/app/testing/testbrowser/browser.py
  U   Zope3/branches/testbrowser-integration/src/zope/app/testing/testbrowser/interfaces.py
  U   Zope3/branches/testbrowser-integration/src/zope/app/testing/testbrowser/testing.py

-=-
Modified: Zope3/branches/testbrowser-integration/src/zope/app/testing/testbrowser/README.txt
===================================================================
--- Zope3/branches/testbrowser-integration/src/zope/app/testing/testbrowser/README.txt	2005-07-27 14:51:09 UTC (rev 37490)
+++ Zope3/branches/testbrowser-integration/src/zope/app/testing/testbrowser/README.txt	2005-07-27 15:42:59 UTC (rev 37491)
@@ -194,3 +194,34 @@
     >>> browser.click('Change')
 
 
+Handling Errors
+---------------
+
+A very useful feature of the publisher is the automatic graceful handling of
+application errors, such as invalid URLs:
+
+    >>> browser.open('http://localhost/invalid')
+    Traceback (most recent call last):
+    ...
+    HTTPError: HTTP Error 404: Not Found
+
+Note that the above error was thrown by ``urllib2`` and not by the
+publisher. For debugging purposes, however, it can be very useful to see the
+original exception caused by the application. In those cases you can set the
+``handleErrors`` property of the browser to ``False``. It is defaulted to
+``True``:
+
+    >>> browser.handleErrors
+    True
+
+So when we tell the publisher not to handle the errors,    
+
+    >>> browser.handleErrors = False
+
+we get a different, Zope internal error:
+
+    >>> browser.open('http://localhost/invalid')
+    Traceback (most recent call last):
+    ...
+    NotFound: Object: <zope.app.folder.folder.Folder object at ...>, 
+              name: u'invalid'

Modified: Zope3/branches/testbrowser-integration/src/zope/app/testing/testbrowser/browser.py
===================================================================
--- Zope3/branches/testbrowser-integration/src/zope/app/testing/testbrowser/browser.py	2005-07-27 14:51:09 UTC (rev 37490)
+++ Zope3/branches/testbrowser-integration/src/zope/app/testing/testbrowser/browser.py	2005-07-27 15:42:59 UTC (rev 37491)
@@ -77,8 +77,29 @@
         """See zope.app.testing.testbrowser.interfaces.IBrowser"""
         return self.mech_browser.response().info()
 
+    @apply
+    def handleErrors():
+        """See zope.app.testing.testbrowser.interfaces.IBrowser"""
+        header_key = 'X-zope-handle-errors'
+
+        def get(self):
+            headers = self.mech_browser.addheaders
+            return dict(headers).get(header_key, True)
+
+        def set(self, value):
+            headers = self.mech_browser.addheaders
+            current_value = get(self)
+            if current_value == value:
+                return
+            if header_key in dict(headers):
+                headers.remove((header_key, current_value))
+            headers.append((header_key, value))
+            
+        return property(get, set)
+
     def open(self, url, data=None):
         """See zope.app.testing.testbrowser.interfaces.IBrowser"""
+        
         self.mech_browser.open(url, data)
 
     def reload(self):
@@ -119,8 +140,8 @@
             else:
                 url_regex = None
 
-            self.mech_browser.follow_link(text_regex=text_regex,
-                                          url_regex=url_regex)
+            self.mech_browser.follow_link(
+                text_regex=text_regex, url_regex=url_regex)
         self._changed()
 
     def getControl(self, text):

Modified: Zope3/branches/testbrowser-integration/src/zope/app/testing/testbrowser/interfaces.py
===================================================================
--- Zope3/branches/testbrowser-integration/src/zope/app/testing/testbrowser/interfaces.py	2005-07-27 14:51:09 UTC (rev 37490)
+++ Zope3/branches/testbrowser-integration/src/zope/app/testing/testbrowser/interfaces.py	2005-07-27 15:42:59 UTC (rev 37491)
@@ -180,6 +180,15 @@
         schema=IFormsMapping,
         required=True)
 
+    handleErrors = zope.schema.Bool(
+        title=u"Handle Errors",
+        description=(u"Describes whether server-side errors will be handled "
+                     u"by the publisher. If set to ``False``, the error will "
+                     u"progress all the way to the test, which is good for "
+                     u"debugging."),
+        default=True,
+        required=True)
+
     def addHeader(key, value):
         """Adds a header to each HTTP request.
 

Modified: Zope3/branches/testbrowser-integration/src/zope/app/testing/testbrowser/testing.py
===================================================================
--- Zope3/branches/testbrowser-integration/src/zope/app/testing/testbrowser/testing.py	2005-07-27 14:51:09 UTC (rev 37490)
+++ Zope3/branches/testbrowser-integration/src/zope/app/testing/testbrowser/testing.py	2005-07-27 15:42:59 UTC (rev 37491)
@@ -45,6 +45,12 @@
         if body is None:
             body = ''
 
+        # Extract the handle_error option header
+        handle_errors_key = 'X-zope-handle-errors'
+        handle_errors = headers.get(handle_errors_key, True)
+        if handle_errors_key in headers:
+            del headers[handle_errors_key]
+
         # Construct the headers.
         header_chunks = []
         if headers is not None:
@@ -59,7 +65,7 @@
         request_string = (method + ' ' + url + ' HTTP/1.1\n'
                           + headers + '\n' + body)
 
-        self.response = self.caller(request_string)
+        self.response = self.caller(request_string, handle_errors)
 
     def getresponse(self):
         """Return a ``urllib2`` compatible response.



More information about the Zope3-Checkins mailing list