[Zope-Checkins] SVN: Zope/trunk/ Fixed collector 2057:
Testing.makequest broke getPhysicalPath()
Paul Winkler
pw_lists at slinkp.com
Wed Apr 5 16:51:22 EDT 2006
Log message for revision 66581:
Fixed collector 2057: Testing.makequest broke getPhysicalPath()
when used on anything other than an app object.
Also added an 'environ' argument if you want to use something
other than os.environ, and added a couple trivial tweaks from
ZopeTestCase.makereqeust.
Added unit tests for this stuff.
Changed:
U Zope/trunk/doc/CHANGES.txt
U Zope/trunk/lib/python/Testing/makerequest.py
A Zope/trunk/lib/python/Testing/tests/
A Zope/trunk/lib/python/Testing/tests/__init__.py
A Zope/trunk/lib/python/Testing/tests/test_makerequest.py
-=-
Modified: Zope/trunk/doc/CHANGES.txt
===================================================================
--- Zope/trunk/doc/CHANGES.txt 2006-04-05 20:04:59 UTC (rev 66580)
+++ Zope/trunk/doc/CHANGES.txt 2006-04-05 20:51:21 UTC (rev 66581)
@@ -46,6 +46,9 @@
Features added
+ - Testing.makerequest: Added an 'environ' argument so
+ clients can use mappings other than os.environ.
+
- Updated to Docutils 0.4.0
- reStructuredText: The default value for the 'stylesheet'
@@ -202,6 +205,10 @@
Bugs Fixed
+ - Collector #2057: Allow Testing.makerequest to work with
+ any acquisition-supporting root object, not just Zope2.app.
+ Formerly, if you did that, getPhysicalPath() was broken.
+
- Collector #2051: Applied patch by Yoshinori Okuji to fix some
XML export/import problems, including tests for that feature.
Modified: Zope/trunk/lib/python/Testing/makerequest.py
===================================================================
--- Zope/trunk/lib/python/Testing/makerequest.py 2006-04-05 20:04:59 UTC (rev 66580)
+++ Zope/trunk/lib/python/Testing/makerequest.py 2006-04-05 20:51:21 UTC (rev 66581)
@@ -19,27 +19,50 @@
import makerequest
app = makerequest.makerequest(Zope2.app())
+You can optionally pass stdout to be used by the response,
+and an environ mapping to be used in the request.
+Defaults are sys.stdout and os.environ.
+
+If you don't want to start a zope app in your test, you can wrap other
+objects, but they must support acquisition and you should only wrap
+your root object.
+
+
$Id$
"""
import os
-from os import environ
from sys import stdin, stdout
from ZPublisher.HTTPRequest import HTTPRequest
from ZPublisher.HTTPResponse import HTTPResponse
from ZPublisher.BaseRequest import RequestContainer
-def makerequest(app, stdout=stdout):
+def makerequest(app, stdout=stdout, environ=None):
resp = HTTPResponse(stdout=stdout)
- environ['SERVER_NAME']='foo'
- environ['SERVER_PORT']='80'
- environ['REQUEST_METHOD'] = 'GET'
+ if environ is None:
+ environ = os.environ
+ environ.setdefault('SERVER_NAME', 'foo')
+ environ.setdefault('SERVER_PORT', '80')
+ environ.setdefault('REQUEST_METHOD', 'GET')
req = HTTPRequest(stdin, environ, resp)
-
+ req._steps = ['noobject'] # Fake a published object.
+ req['ACTUAL_URL'] = req.get('URL') # Zope 2.7.4
+
# set Zope3-style default skin so that the request is usable for
- # Zope3-style view look-ups
+ # Zope3-style view look-ups.
from zope.app.publication.browser import setDefaultSkin
setDefaultSkin(req)
- return app.__of__(RequestContainer(REQUEST = req))
+ requestcontainer = RequestContainer(REQUEST = req)
+ # Workaround for collector 2057: ensure that we don't break
+ # getPhysicalPath if app has that method.
+ # We could instead fix Traversable.getPhysicalPath() to check for
+ # existence of p.getPhysicalPath before calling it; but it's such
+ # a commonly called method that I don't want to impact performance
+ # for something that AFAICT only affects makerequest() in
+ # practice.
+ if getattr(app, 'getPhysicalPath', None) is not None:
+ requestcontainer.getPhysicalPath = lambda: ()
+
+ return app.__of__(requestcontainer)
Added: Zope/trunk/lib/python/Testing/tests/__init__.py
===================================================================
--- Zope/trunk/lib/python/Testing/tests/__init__.py 2006-04-05 20:04:59 UTC (rev 66580)
+++ Zope/trunk/lib/python/Testing/tests/__init__.py 2006-04-05 20:51:21 UTC (rev 66581)
@@ -0,0 +1 @@
+#
Property changes on: Zope/trunk/lib/python/Testing/tests/__init__.py
___________________________________________________________________
Name: svn:keywords
+ "Author Date Revision"
Added: Zope/trunk/lib/python/Testing/tests/test_makerequest.py
===================================================================
--- Zope/trunk/lib/python/Testing/tests/test_makerequest.py 2006-04-05 20:04:59 UTC (rev 66580)
+++ Zope/trunk/lib/python/Testing/tests/test_makerequest.py 2006-04-05 20:51:21 UTC (rev 66581)
@@ -0,0 +1,65 @@
+##############################################################################
+#
+# 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.
+#
+##############################################################################
+"""Unit tests of makequest.
+
+$Id$
+"""
+
+import unittest
+
+from Acquisition import Implicit
+from Testing.makerequest import makerequest
+from OFS.SimpleItem import SimpleItem
+
+class MakerequestTests(unittest.TestCase):
+
+ def test_makerequest(self):
+ # The argument must support acquisition.
+ self.assertRaises(AttributeError, makerequest, object())
+ # After the call, it will have a REQUEST attribute.
+ item = Implicit()
+ self.failIf(hasattr(item, 'REQUEST'))
+ item = makerequest(item)
+ self.failUnless(hasattr(item, 'REQUEST'))
+
+ def test_dont_break_getPhysicalPath(self):
+ # see http://www.zope.org/Collectors/Zope/2057
+ item = SimpleItem()
+ self.assertEqual(item.getPhysicalPath(), ('',))
+ self.assertEqual(item.getPhysicalPath(),
+ makerequest(item).getPhysicalPath())
+
+ def test_stdout(self):
+ # You can pass a stdout arg and it's used by the response.
+ import cStringIO
+ out = cStringIO.StringIO()
+ item = makerequest(SimpleItem(), stdout=out)
+ item.REQUEST.RESPONSE.write('aaa')
+ out.seek(0)
+ written = out.read()
+ self.failUnless(written.startswith('Status: 200 OK\n'))
+ self.failUnless(written.endswith('\naaa'))
+
+ def test_environ(self):
+ # You can pass an environ argument to use in the request.
+ environ = {'foofoo': 'barbar'}
+ item = makerequest(SimpleItem(), environ=environ)
+ self.assertEqual(item.REQUEST.environ['foofoo'], 'barbar')
+
+def test_suite():
+ suite = unittest.TestSuite()
+ suite.addTest(unittest.makeSuite(MakerequestTests))
+ return suite
+
+if __name__=='__main__':
+ unittest.TextTestRunner().run(test_suite())
Property changes on: Zope/trunk/lib/python/Testing/tests/test_makerequest.py
___________________________________________________________________
Name: svn:keywords
+ "Author Date Revision"
More information about the Zope-Checkins
mailing list