[Checkins] SVN: zope.testbrowser/branches/jinty-webtest2/src/zope/testbrowser/ Move code used in both zope.publisher and WebTest based Browsers into it's own space. Fix some copy/paste documentation errors
Brian Sutherland
jinty at web.de
Tue Jan 18 19:23:13 EST 2011
Log message for revision 119677:
Move code used in both zope.publisher and WebTest based Browsers into it's own space. Fix some copy/paste documentation errors
Changed:
U zope.testbrowser/branches/jinty-webtest2/src/zope/testbrowser/testing.py
U zope.testbrowser/branches/jinty-webtest2/src/zope/testbrowser/wsgi.py
-=-
Modified: zope.testbrowser/branches/jinty-webtest2/src/zope/testbrowser/testing.py
===================================================================
--- zope.testbrowser/branches/jinty-webtest2/src/zope/testbrowser/testing.py 2011-01-18 22:03:30 UTC (rev 119676)
+++ zope.testbrowser/branches/jinty-webtest2/src/zope/testbrowser/testing.py 2011-01-19 00:23:13 UTC (rev 119677)
@@ -20,7 +20,88 @@
import sys
import zope.testbrowser.browser
+#
+# Base classes sometimes useful to implement browsers
+#
+class Response(object):
+ """``mechanize`` compatible response object."""
+
+ def __init__(self, content, headers, status, reason):
+ self.content = content
+ self.status = status
+ self.reason = reason
+ self.msg = httplib.HTTPMessage(cStringIO.StringIO(headers), 0)
+ self.content_as_file = cStringIO.StringIO(self.content)
+
+ def read(self, amt=None):
+ return self.content_as_file.read(amt)
+
+ def close(self):
+ """To overcome changes in mechanize and socket in python2.5"""
+ pass
+
+class HTTPHandler(mechanize.HTTPHandler):
+
+ def _connect(self, *args, **kw):
+ raise NotImplementedError("implement")
+
+ def http_request(self, req):
+ # look at data and set content type
+ if req.has_data():
+ data = req.get_data()
+ if isinstance(data, dict):
+ req.add_data(data['body'])
+ req.add_unredirected_header('Content-type',
+ data['content-type'])
+ return mechanize.HTTPHandler.do_request_(self, req)
+
+ https_request = http_request
+
+ def http_open(self, req):
+ """Open an HTTP connection having a ``mechanize`` request."""
+ # Here we connect to the publisher.
+ if sys.version_info > (2, 6) and not hasattr(req, 'timeout'):
+ # Workaround mechanize incompatibility with Python
+ # 2.6. See: LP #280334
+ req.timeout = socket._GLOBAL_DEFAULT_TIMEOUT
+ return self.do_open(self._connect, req)
+
+ https_open = http_open
+
+class MechanizeBrowser(mechanize.Browser):
+ """Special ``mechanize`` browser using the Zope Publisher HTTP handler."""
+
+ default_schemes = ['http']
+ default_others = ['_http_error', '_http_default_error']
+ default_features = ['_redirect', '_cookies', '_referer', '_refresh',
+ '_equiv', '_basicauth', '_digestauth']
+
+
+ def __init__(self, *args, **kws):
+ inherited_handlers = ['_unknown', '_http_error',
+ '_http_default_error', '_basicauth',
+ '_digestauth', '_redirect', '_cookies', '_referer',
+ '_refresh', '_equiv', '_gzip']
+
+ self.handler_classes = {"http": self._http_handler}
+ for name in inherited_handlers:
+ self.handler_classes[name] = mechanize.Browser.handler_classes[name]
+
+ kws['request_class'] = kws.get('request_class',
+ mechanize._request.Request)
+
+ mechanize.Browser.__init__(self, *args, **kws)
+
+ def _http_handler(self, *args, **kw):
+ return NotImplementedError("Try return a sub-class of PublisherHTTPHandler here")
+
+#
+# Zope Publisher Browser implementation
+#
+
+PublisherResponse = Response # BBB
+
class PublisherConnection(object):
"""A ``mechanize`` compatible connection object."""
@@ -91,84 +172,23 @@
headers.insert(0, ('Status', real_response.getStatusString()))
headers = '\r\n'.join('%s: %s' % h for h in headers)
content = real_response.consumeBody()
- return PublisherResponse(content, headers, status, reason)
+ return Response(content, headers, status, reason)
-class PublisherResponse(object):
- """``mechanize`` compatible response object."""
-
- def __init__(self, content, headers, status, reason):
- self.content = content
- self.status = status
- self.reason = reason
- self.msg = httplib.HTTPMessage(cStringIO.StringIO(headers), 0)
- self.content_as_file = cStringIO.StringIO(self.content)
-
- def read(self, amt=None):
- return self.content_as_file.read(amt)
-
- def close(self):
- """To overcome changes in mechanize and socket in python2.5"""
- pass
-
-
-class PublisherHTTPHandler(mechanize.HTTPHandler):
+class PublisherHTTPHandler(HTTPHandler):
"""Special HTTP handler to use the Zope Publisher."""
def _connect(self, *args, **kw):
return PublisherConnection(*args, **kw)
- def http_request(self, req):
- # look at data and set content type
- if req.has_data():
- data = req.get_data()
- if isinstance(data, dict):
- req.add_data(data['body'])
- req.add_unredirected_header('Content-type',
- data['content-type'])
- return mechanize.HTTPHandler.do_request_(self, req)
- https_request = http_request
-
- def http_open(self, req):
- """Open an HTTP connection having a ``mechanize`` request."""
- # Here we connect to the publisher.
- if sys.version_info > (2, 6) and not hasattr(req, 'timeout'):
- # Workaround mechanize incompatibility with Python
- # 2.6. See: LP #280334
- req.timeout = socket._GLOBAL_DEFAULT_TIMEOUT
- return self.do_open(self._connect, req)
-
- https_open = http_open
-
-
-class PublisherMechanizeBrowser(mechanize.Browser):
+class PublisherMechanizeBrowser(MechanizeBrowser):
"""Special ``mechanize`` browser using the Zope Publisher HTTP handler."""
- default_schemes = ['http']
- default_others = ['_http_error', '_http_default_error']
- default_features = ['_redirect', '_cookies', '_referer', '_refresh',
- '_equiv', '_basicauth', '_digestauth']
-
-
- def __init__(self, *args, **kws):
- inherited_handlers = ['_unknown', '_http_error',
- '_http_default_error', '_basicauth',
- '_digestauth', '_redirect', '_cookies', '_referer',
- '_refresh', '_equiv', '_gzip']
-
- self.handler_classes = {"http": self._http_handler}
- for name in inherited_handlers:
- self.handler_classes[name] = mechanize.Browser.handler_classes[name]
-
- kws['request_class'] = kws.get('request_class',
- mechanize._request.Request)
-
- mechanize.Browser.__init__(self, *args, **kws)
-
def _http_handler(self, *args, **kw):
return PublisherHTTPHandler(*args, **kw)
+
class Browser(zope.testbrowser.browser.Browser):
"""A Zope `testbrowser` Browser that uses the Zope Publisher."""
Modified: zope.testbrowser/branches/jinty-webtest2/src/zope/testbrowser/wsgi.py
===================================================================
--- zope.testbrowser/branches/jinty-webtest2/src/zope/testbrowser/wsgi.py 2011-01-18 22:03:30 UTC (rev 119676)
+++ zope.testbrowser/branches/jinty-webtest2/src/zope/testbrowser/wsgi.py 2011-01-19 00:23:13 UTC (rev 119677)
@@ -11,7 +11,7 @@
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
-"""Zope 3-specific testing code
+"""WSGI-specific testing code
"""
import cStringIO
import Cookie
@@ -35,8 +35,8 @@
pass
def _quote(self, url):
- # the publisher expects to be able to split on whitespace, so we have
- # to make sure there is none in the URL
+ # XXX: is this necessary with WebTest? Was cargeo-culted from the
+ # Zope Publisher Connection
return url.replace(' ', '%20')
def request(self, method, url, body=None, headers=None):
@@ -103,7 +103,7 @@
def getresponse(self):
"""Return a ``mechanize`` compatible response.
- The goal of ths method is to convert the Zope Publisher's reseponse to
+ The goal of ths method is to convert the WebTest's reseponse to
a ``mechanize`` compatible response, which is also understood by
mechanize.
"""
@@ -116,14 +116,14 @@
headers.insert(0, ('Status', response.status))
headers = '\r\n'.join('%s: %s' % h for h in headers)
content = response.body
- return zope.testbrowser.testing.PublisherResponse(content, headers, status, reason)
+ return zope.testbrowser.testing.Response(content, headers, status, reason)
-class WSGIHTTPHandler(zope.testbrowser.testing.PublisherHTTPHandler):
+class WSGIHTTPHandler(zope.testbrowser.testing.HTTPHandler):
def __init__(self, test_app, *args, **kw):
self._test_app = test_app
- zope.testbrowser.testing.PublisherHTTPHandler.__init__(self, *args, **kw)
+ zope.testbrowser.testing.HTTPHandler.__init__(self, *args, **kw)
def _connect(self, *args, **kw):
return WSGIConnection(self._test_app, *args, **kw)
@@ -133,19 +133,19 @@
return self.http_request(req)
-class WSGIMechanizeBrowser(zope.testbrowser.testing.PublisherMechanizeBrowser):
- """Special ``mechanize`` browser using the Zope Publisher HTTP handler."""
+class WSGIMechanizeBrowser(zope.testbrowser.testing.MechanizeBrowser):
+ """Special ``mechanize`` browser using the WSGI HTTP handler."""
def __init__(self, test_app, *args, **kw):
self._test_app = test_app
- zope.testbrowser.testing.PublisherMechanizeBrowser.__init__(self, *args, **kw)
+ zope.testbrowser.testing.MechanizeBrowser.__init__(self, *args, **kw)
def _http_handler(self, *args, **kw):
return WSGIHTTPHandler(self._test_app, *args, **kw)
class Browser(zope.testbrowser.browser.Browser):
- """A Zope `testbrowser` Browser that uses the Zope Publisher."""
+ """A WSGI `testbrowser` Browser that uses a WebTest wrapped WSGI app."""
def __init__(self, test_app, url=None):
mech_browser = WSGIMechanizeBrowser(test_app)
More information about the checkins
mailing list