[Zope3-checkins] SVN: Zope3/trunk/src/zope/testbrowser/testing.py
Add in virtual host testing configuration.
Gary Poster
gary at zope.com
Thu Jan 19 23:14:39 EST 2006
Log message for revision 41373:
Add in virtual host testing configuration.
Changed:
U Zope3/trunk/src/zope/testbrowser/testing.py
-=-
Modified: Zope3/trunk/src/zope/testbrowser/testing.py
===================================================================
--- Zope3/trunk/src/zope/testbrowser/testing.py 2006-01-20 04:13:21 UTC (rev 41372)
+++ Zope3/trunk/src/zope/testbrowser/testing.py 2006-01-20 04:14:38 UTC (rev 41373)
@@ -11,10 +11,12 @@
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
-"""Publisher Hook to ``mechanize``
+"""Zope 3-specific testing code
$Id$
"""
+import re
+import unittest
import httplib
import urllib2
from cStringIO import StringIO
@@ -22,16 +24,19 @@
import mechanize
import ClientCookie
+import transaction
from zope.testbrowser import browser
+from zope.testing import renormalizing, doctest
+from zope.app.testing import functional
+from zope.app.folder.folder import Folder
+from zope.app.component.site import LocalSiteManager
class PublisherConnection(object):
"""A ``urllib2`` compatible connection obejct."""
def __init__(self, host):
- # this is imported here to hide the import from non-Zope-based code
- from zope.app.testing.functional import HTTPCaller
- self.caller = HTTPCaller()
+ self.caller = functional.HTTPCaller()
self.host = host
def set_debuglevel(self, level):
@@ -147,8 +152,95 @@
class Browser(browser.Browser):
- """A Zope ``testbrowser` Browser that uses the Zope Publisher."""
+ """A Zope `testbrowser` Browser that uses the Zope Publisher."""
def __init__(self, url=None):
mech_browser = PublisherMechanizeBrowser()
super(Browser, self).__init__(url=url, mech_browser=mech_browser)
+
+#### virtual host test suites ####
+
+example_path_re = re.compile('http://example.com/virtual_path/')
+
+class VirtualHostingPublisherConnection(PublisherConnection):
+ def request(self, method, url, body=None, headers=None):
+ if self.host == 'example.com':
+ assert url.startswith('/virtual_path')
+ url = url[13:]
+ if not url:
+ url = '/'
+ url = '/vh_test_folder/++vh++http:example.com:80/virtual_path/++' + url
+ super(VirtualHostingPublisherConnection, self).request(
+ method, url, body, headers)
+
+class VirtualHostingPublisherHTTPHandler(urllib2.HTTPHandler):
+ """Special HTTP handler to use the Zope Publisher."""
+
+ http_request = urllib2.AbstractHTTPHandler.do_request_
+
+ def http_open(self, req):
+ """Open an HTTP connection having a ``urllib2`` request."""
+ # Here we connect to the publisher.
+ return self.do_open(VirtualHostingPublisherConnection, req)
+
+class VirtualHostingPublisherMechanizeBrowser(PublisherMechanizeBrowser):
+ handler_classes = PublisherMechanizeBrowser.handler_classes.copy()
+ handler_classes['http'] = VirtualHostingPublisherHTTPHandler
+
+class VirtualHostingBrowser(browser.Browser):
+ """A Zope ``testbrowser` Browser that inserts ."""
+
+ def __init__(self, url=None):
+ mech_browser = VirtualHostingPublisherMechanizeBrowser()
+ super(VirtualHostingBrowser, self).__init__(
+ url=url, mech_browser=mech_browser)
+
+def virtualHostingSetUp(test):
+ # need to create a folder named vh_test_folder in root
+ root = test.globs['getRootFolder']()
+ f = Folder()
+ root['vh_test_folder'] = f
+ f.setSiteManager(LocalSiteManager(f))
+ transaction.commit()
+
+def VirtualHostTestBrowserSuite(*paths, **kw):
+# layer=None,
+# globs=None, setUp=None, normalizers=None, **kw):
+
+ if 'checker' in kw:
+ raise ValueError(
+ 'Must not supply custom checker. To provide values for '
+ 'zope.testing.renormalizing.RENormalizing checkers, include a '
+ '"normalizers" argument that is a list of (compiled regex, '
+ 'replacement) pairs.')
+ kw['package'] = doctest._normalize_module(kw.get('package'))
+ layer = kw.pop('layer', None)
+ normalizers = kw.pop('normalizers', None)
+ vh_kw = kw.copy()
+ globs = kw.setdefault('globs', {})
+ if 'Browser' in globs:
+ raise ValueError('"Browser" must not be defined in globs')
+ vh_kw['globs'] = globs.copy()
+ globs['Browser'] = Browser
+ vh_kw['globs']['Browser'] = VirtualHostingBrowser
+ def vh_setUp(test):
+ virtualHostingSetUp(test)
+ if 'setUp' in kw:
+ kw['setUp'](test)
+ vh_kw['setUp'] = vh_setUp
+ if normalizers is not None:
+ kw['checker'] = renormalizing.RENormalizing(normalizers)
+ vh_normalizers = normalizers[:]
+ else:
+ vh_normalizers = []
+ vh_normalizers.append((example_path_re, 'http://localhost/'))
+ vh_kw['checker'] = renormalizing.RENormalizing(vh_normalizers)
+ suite = unittest.TestSuite()
+ test = functional.FunctionalDocFileSuite(*paths, **kw)
+ vh_test = functional.FunctionalDocFileSuite(*paths, **vh_kw)
+ if layer is not None:
+ test.layer = layer
+ vh_test.layer = layer
+ suite.addTest(test)
+ suite.addTest(vh_test)
+ return suite
More information about the Zope3-Checkins
mailing list