[Zope-Checkins] SVN: Zope/trunk/ Moved ``testbrowser`` module into the Testing package.
Hanno Schlichting
hannosch at hannosch.eu
Tue Mar 30 19:05:20 EDT 2010
Log message for revision 110345:
Moved ``testbrowser`` module into the Testing package.
Changed:
U Zope/trunk/doc/CHANGES.rst
U Zope/trunk/src/Products/Five/browser/tests/aqlegacy_ftest.txt
U Zope/trunk/src/Products/Five/browser/tests/test_zope3security.py
U Zope/trunk/src/Products/Five/component/makesite.txt
U Zope/trunk/src/Products/Five/testbrowser.py
D Zope/trunk/src/Products/Five/tests/test_testbrowser.py
U Zope/trunk/src/Testing/ZopeTestCase/testPlaceless.py
A Zope/trunk/src/Testing/testbrowser.py
A Zope/trunk/src/Testing/tests/test_testbrowser.py
-=-
Modified: Zope/trunk/doc/CHANGES.rst
===================================================================
--- Zope/trunk/doc/CHANGES.rst 2010-03-30 22:40:52 UTC (rev 110344)
+++ Zope/trunk/doc/CHANGES.rst 2010-03-30 23:05:20 UTC (rev 110345)
@@ -11,6 +11,8 @@
Restructuring
+++++++++++++
+- Moved ``testbrowser`` module into the Testing package.
+
- Moved the code handling ZCML loading into the ``Zope2.App`` package. The
component architecture is now setup before the application object is created
or any database connections are opened. So far the CA was setup somewhat
Modified: Zope/trunk/src/Products/Five/browser/tests/aqlegacy_ftest.txt
===================================================================
--- Zope/trunk/src/Products/Five/browser/tests/aqlegacy_ftest.txt 2010-03-30 22:40:52 UTC (rev 110344)
+++ Zope/trunk/src/Products/Five/browser/tests/aqlegacy_ftest.txt 2010-03-30 23:05:20 UTC (rev 110345)
@@ -9,7 +9,7 @@
>>> zcml.load_config("configure.zcml", Products.Five)
>>> zcml.load_config('aqlegacy.zcml', package=Products.Five.browser.tests)
- >>> from Products.Five.testbrowser import Browser
+ >>> from Testing.testbrowser import Browser
>>> browser = Browser()
>>> browser.handleErrors = False
Modified: Zope/trunk/src/Products/Five/browser/tests/test_zope3security.py
===================================================================
--- Zope/trunk/src/Products/Five/browser/tests/test_zope3security.py 2010-03-30 22:40:52 UTC (rev 110344)
+++ Zope/trunk/src/Products/Five/browser/tests/test_zope3security.py 2010-03-30 23:05:20 UTC (rev 110345)
@@ -37,7 +37,7 @@
zope.security.management.checkPermission(). We see it works as
expected:
- >>> from Products.Five.testbrowser import Browser
+ >>> from Testing.testbrowser import Browser
>>> browser = Browser()
>>> browser.open('http://localhost/test_folder_1_/testoid/@@zope3security.html?permission=zope2.View')
>>> print browser.contents
Modified: Zope/trunk/src/Products/Five/component/makesite.txt
===================================================================
--- Zope/trunk/src/Products/Five/component/makesite.txt 2010-03-30 22:40:52 UTC (rev 110344)
+++ Zope/trunk/src/Products/Five/component/makesite.txt 2010-03-30 23:05:20 UTC (rev 110345)
@@ -30,7 +30,7 @@
Create the test browser we'll be using:
- >>> from Products.Five.testbrowser import Browser
+ >>> from Testing.testbrowser import Browser
>>> browser = Browser()
>>> browser.addHeader('Authorization', 'Basic manager:r00t')
Modified: Zope/trunk/src/Products/Five/testbrowser.py
===================================================================
--- Zope/trunk/src/Products/Five/testbrowser.py 2010-03-30 22:40:52 UTC (rev 110344)
+++ Zope/trunk/src/Products/Five/testbrowser.py 2010-03-30 23:05:20 UTC (rev 110345)
@@ -1,118 +1,10 @@
-##############################################################################
-#
-# Copyright (c) 2006 Zope Corporation and Contributors.
-# All Rights Reserved.
-#
-# 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.
-#
-##############################################################################
-"""Support for using zope.testbrowser from Zope2.
+# BBB
-Mostly just copy and paste from zope.testbrowser.testing.
+from zope.deferredimport import deprecated
-$Id$
-"""
-
-import sys
-import socket
-import urllib2
-
-import mechanize
-
-from zope.testbrowser import testing
-from zope.testbrowser import browser
-import zope.publisher.http
-
-
-class PublisherConnection(testing.PublisherConnection):
-
- def __init__(self, host, timeout=None):
- from Testing.ZopeTestCase.zopedoctest.functional import http
- self.caller = http
- self.host = host
-
- def getresponse(self):
- """Return a ``urllib2`` compatible response.
-
- The goal of ths method is to convert the Zope Publisher's reseponse to
- a ``urllib2`` compatible response, which is also understood by
- mechanize.
- """
- real_response = self.response._response
- status = real_response.getStatus()
- reason = zope.publisher.http.status_reasons[real_response.status]
- headers = []
- # Convert header keys to camel case. This is basically a copy
- # paste from ZPublisher.HTTPResponse
- for key, val in real_response.headers.items():
- if key.lower() == key:
- # only change non-literal header names
- key = "%s%s" % (key[:1].upper(), key[1:])
- start = 0
- l = key.find('-',start)
- while l >= start:
- key = "%s-%s%s" % (key[:l],key[l+1:l+2].upper(),key[l+2:])
- start = l + 1
- l = key.find('-', start)
- headers.append((key, val))
- # get the cookies, breaking them into tuples for sorting
- cookies = [(c[:10], c[12:]) for c in real_response._cookie_list()]
- headers.extend(cookies)
- headers.sort()
- headers.insert(0, ('Status', "%s %s" % (status, reason)))
- headers = '\r\n'.join('%s: %s' % h for h in headers)
- content = real_response.body
- return testing.PublisherResponse(content, headers, status, reason)
-
-
-class PublisherHTTPHandler(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.
- 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(PublisherConnection, req)
-
-
-class PublisherMechanizeBrowser(mechanize.Browser):
- """Special ``mechanize`` browser using the Zope Publisher HTTP handler."""
-
- default_schemes = ['http']
- default_others = ['_http_error', '_http_request_upgrade',
- '_http_default_error']
- default_features = ['_redirect', '_cookies', '_referer', '_refresh',
- '_equiv', '_basicauth', '_digestauth' ]
-
- def __init__(self, *args, **kws):
- inherited_handlers = ['_unknown', '_http_error',
- '_http_request_upgrade', '_http_default_error', '_basicauth',
- '_digestauth', '_redirect', '_cookies', '_referer',
- '_refresh', '_equiv', '_gzip']
-
- self.handler_classes = {"http": PublisherHTTPHandler}
- for name in inherited_handlers:
- self.handler_classes[name] = mechanize.Browser.handler_classes[name]
-
- mechanize.Browser.__init__(self, *args, **kws)
-
-
-class Browser(browser.Browser):
- """A Zope ``testbrowser` Browser that uses the Zope Publisher."""
-
- def __init__(self, url=None):
- mech_browser = PublisherMechanizeBrowser()
- # override the http handler class
- mech_browser.handler_classes["http"] = PublisherHTTPHandler
- super(Browser, self).__init__(url=url, mech_browser=mech_browser)
-
+deprecated("Please import from Testing.testbrowser",
+ PublisherConnection = 'Testing.testbrowser:PublisherConnection',
+ PublisherHTTPHandler = 'Testing.testbrowser:PublisherHTTPHandler',
+ PublisherMechanizeBrowser = 'Testing.testbrowser:PublisherMechanizeBrowser',
+ Browser = 'Testing.testbrowser:Browser',
+)
Deleted: Zope/trunk/src/Products/Five/tests/test_testbrowser.py
===================================================================
--- Zope/trunk/src/Products/Five/tests/test_testbrowser.py 2010-03-30 22:40:52 UTC (rev 110344)
+++ Zope/trunk/src/Products/Five/tests/test_testbrowser.py 2010-03-30 23:05:20 UTC (rev 110345)
@@ -1,83 +0,0 @@
-##############################################################################
-#
-# Copyright (c) 2004, 2005 Zope Corporation and Contributors.
-# All Rights Reserved.
-#
-# 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.
-#
-##############################################################################
-"""Unit tests for the testbrowser module.
-
-$Id$
-"""
-
-import unittest
-from Testing.ZopeTestCase import FunctionalDocTestSuite
-from OFS.SimpleItem import Item
-
-class CookieStub(Item):
- """This is a cookie stub."""
-
- def __call__(self, REQUEST):
- REQUEST.RESPONSE.setCookie('evil', 'cookie')
- return 'Stub'
-
-def doctest_cookies():
- """
- We want to make sure that our testbrowser correctly understands
- cookies. We'll add a stub to ``self.folder`` that sets a cookie.
-
- >>> from Products.Five.tests.test_testbrowser import CookieStub
- >>> self.folder._setObject('stub', CookieStub())
- 'stub'
-
- This response looks alright:
-
- >>> response = self.publish('/test_folder_1_/stub')
- >>> print str(response) #doctest: +ELLIPSIS
- Status: 200 OK
- ...
- Set-Cookie: evil="cookie"
- ...
-
- Let's try to look at the same folder with testbrowser:
-
- >>> from Products.Five.testbrowser import Browser
- >>> browser = Browser()
- >>> browser.open('http://localhost/test_folder_1_/stub')
- >>> 'Set-Cookie: evil="cookie"' in str(browser.headers)
- True
- """
-
-def doctest_camel_case_headers():
- """Make sure that the headers come out in camel case.
-
- Some setup:
-
- >>> from Products.Five.tests.test_testbrowser import CookieStub
- >>> self.folder._setObject('stub', CookieStub())
- 'stub'
-
- The Zope2 response mungs headers so they come out in camel case we should
- do the same. This is also more consistent with the Zope3 testbrowser tests.
- We will test a few:
-
- >>> from Products.Five.testbrowser import Browser
- >>> browser = Browser()
- >>> browser.open('http://localhost/test_folder_1_/stub')
- >>> 'Content-Length: ' in str(browser.headers)
- True
- >>> 'Content-Type: ' in str(browser.headers)
- True
- """
-
-
-def test_suite():
- return unittest.TestSuite((
- FunctionalDocTestSuite(),
- ))
Modified: Zope/trunk/src/Testing/ZopeTestCase/testPlaceless.py
===================================================================
--- Zope/trunk/src/Testing/ZopeTestCase/testPlaceless.py 2010-03-30 22:40:52 UTC (rev 110344)
+++ Zope/trunk/src/Testing/ZopeTestCase/testPlaceless.py 2010-03-30 23:05:20 UTC (rev 110345)
@@ -76,7 +76,6 @@
tearDown()
def testSimple(self):
- # SetUp according to Five's adapter test
setUp()
setupZCML()
# Now we have a fixture that should work for adaptation
Copied: Zope/trunk/src/Testing/testbrowser.py (from rev 110303, Zope/trunk/src/Products/Five/testbrowser.py)
===================================================================
--- Zope/trunk/src/Testing/testbrowser.py (rev 0)
+++ Zope/trunk/src/Testing/testbrowser.py 2010-03-30 23:05:20 UTC (rev 110345)
@@ -0,0 +1,115 @@
+##############################################################################
+#
+# Copyright (c) 2006 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# 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.
+#
+##############################################################################
+"""Support for using zope.testbrowser from Zope2.
+
+Mostly just copy and paste from zope.testbrowser.testing.
+"""
+
+import sys
+import socket
+import urllib2
+
+import mechanize
+
+from zope.testbrowser import testing
+from zope.testbrowser import browser
+import zope.publisher.http
+
+
+class PublisherConnection(testing.PublisherConnection):
+
+ def __init__(self, host, timeout=None):
+ from Testing.ZopeTestCase.zopedoctest.functional import http
+ self.caller = http
+ self.host = host
+
+ def getresponse(self):
+ """Return a ``urllib2`` compatible response.
+
+ The goal of ths method is to convert the Zope Publisher's reseponse to
+ a ``urllib2`` compatible response, which is also understood by
+ mechanize.
+ """
+ real_response = self.response._response
+ status = real_response.getStatus()
+ reason = zope.publisher.http.status_reasons[real_response.status]
+ headers = []
+ # Convert header keys to camel case. This is basically a copy
+ # paste from ZPublisher.HTTPResponse
+ for key, val in real_response.headers.items():
+ if key.lower() == key:
+ # only change non-literal header names
+ key = "%s%s" % (key[:1].upper(), key[1:])
+ start = 0
+ l = key.find('-',start)
+ while l >= start:
+ key = "%s-%s%s" % (key[:l],key[l+1:l+2].upper(),key[l+2:])
+ start = l + 1
+ l = key.find('-', start)
+ headers.append((key, val))
+ # get the cookies, breaking them into tuples for sorting
+ cookies = [(c[:10], c[12:]) for c in real_response._cookie_list()]
+ headers.extend(cookies)
+ headers.sort()
+ headers.insert(0, ('Status', "%s %s" % (status, reason)))
+ headers = '\r\n'.join('%s: %s' % h for h in headers)
+ content = real_response.body
+ return testing.PublisherResponse(content, headers, status, reason)
+
+
+class PublisherHTTPHandler(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.
+ 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(PublisherConnection, req)
+
+
+class PublisherMechanizeBrowser(mechanize.Browser):
+ """Special ``mechanize`` browser using the Zope Publisher HTTP handler."""
+
+ default_schemes = ['http']
+ default_others = ['_http_error', '_http_request_upgrade',
+ '_http_default_error']
+ default_features = ['_redirect', '_cookies', '_referer', '_refresh',
+ '_equiv', '_basicauth', '_digestauth' ]
+
+ def __init__(self, *args, **kws):
+ inherited_handlers = ['_unknown', '_http_error',
+ '_http_request_upgrade', '_http_default_error', '_basicauth',
+ '_digestauth', '_redirect', '_cookies', '_referer',
+ '_refresh', '_equiv', '_gzip']
+
+ self.handler_classes = {"http": PublisherHTTPHandler}
+ for name in inherited_handlers:
+ self.handler_classes[name] = mechanize.Browser.handler_classes[name]
+
+ mechanize.Browser.__init__(self, *args, **kws)
+
+
+class Browser(browser.Browser):
+ """A Zope ``testbrowser` Browser that uses the Zope Publisher."""
+
+ def __init__(self, url=None):
+ mech_browser = PublisherMechanizeBrowser()
+ # override the http handler class
+ mech_browser.handler_classes["http"] = PublisherHTTPHandler
+ super(Browser, self).__init__(url=url, mech_browser=mech_browser)
Copied: Zope/trunk/src/Testing/tests/test_testbrowser.py (from rev 110303, Zope/trunk/src/Products/Five/tests/test_testbrowser.py)
===================================================================
--- Zope/trunk/src/Testing/tests/test_testbrowser.py (rev 0)
+++ Zope/trunk/src/Testing/tests/test_testbrowser.py 2010-03-30 23:05:20 UTC (rev 110345)
@@ -0,0 +1,82 @@
+##############################################################################
+#
+# Copyright (c) 2004, 2005 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# 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.
+#
+##############################################################################
+"""Unit tests for the testbrowser module.
+
+$Id$
+"""
+
+import unittest
+from Testing.ZopeTestCase import FunctionalDocTestSuite
+from OFS.SimpleItem import Item
+
+class CookieStub(Item):
+ """This is a cookie stub."""
+
+ def __call__(self, REQUEST):
+ REQUEST.RESPONSE.setCookie('evil', 'cookie')
+ return 'Stub'
+
+def doctest_cookies():
+ """
+ We want to make sure that our testbrowser correctly understands
+ cookies. We'll add a stub to ``self.folder`` that sets a cookie.
+
+ >>> from Testing.tests.test_testbrowser import CookieStub
+ >>> self.folder._setObject('stub', CookieStub())
+ 'stub'
+
+ This response looks alright:
+
+ >>> response = self.publish('/test_folder_1_/stub')
+ >>> print str(response) #doctest: +ELLIPSIS
+ Status: 200 OK
+ ...
+ Set-Cookie: evil="cookie"
+ ...
+
+ Let's try to look at the same folder with testbrowser:
+
+ >>> from Testing.testbrowser import Browser
+ >>> browser = Browser()
+ >>> browser.open('http://localhost/test_folder_1_/stub')
+ >>> 'Set-Cookie: evil="cookie"' in str(browser.headers)
+ True
+ """
+
+def doctest_camel_case_headers():
+ """Make sure that the headers come out in camel case.
+
+ Some setup:
+
+ >>> from Testing.tests.test_testbrowser import CookieStub
+ >>> self.folder._setObject('stub', CookieStub())
+ 'stub'
+
+ The Zope2 response mungs headers so they come out in camel case we should
+ do the same. We will test a few:
+
+ >>> from Testing.testbrowser import Browser
+ >>> browser = Browser()
+ >>> browser.open('http://localhost/test_folder_1_/stub')
+ >>> 'Content-Length: ' in str(browser.headers)
+ True
+ >>> 'Content-Type: ' in str(browser.headers)
+ True
+ """
+
+
+def test_suite():
+ return unittest.TestSuite((
+ FunctionalDocTestSuite(),
+ ))
More information about the Zope-Checkins
mailing list