[Zope-Checkins] CVS: Zope3/lib/python/Zope/Publisher/HTTP/tests - TestRequest.py:1.2 __init__.py:1.2 testHTTP.py:1.2
Jim Fulton
jim@zope.com
Mon, 10 Jun 2002 19:30:03 -0400
Update of /cvs-repository/Zope3/lib/python/Zope/Publisher/HTTP/tests
In directory cvs.zope.org:/tmp/cvs-serv20468/lib/python/Zope/Publisher/HTTP/tests
Added Files:
TestRequest.py __init__.py testHTTP.py
Log Message:
Merged Zope-3x-branch into newly forked Zope3 CVS Tree.
=== Zope3/lib/python/Zope/Publisher/HTTP/tests/TestRequest.py 1.1 => 1.2 ===
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (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.
+#
+##############################################################################
+"""
+Test request for writing tests that need HTTP requests.
+
+Note that this is used by tests in other packages.
+
+$Id$
+"""
+
+from StringIO import StringIO
+
+from Zope.Publisher.HTTP.HTTPRequest import HTTPRequest
+
+_testEnv = {
+ 'SERVER_URL': 'http://foobar.com',
+ 'HTTP_HOST': 'foobar.com',
+ 'CONTENT_LENGTH': '0',
+ 'GATEWAY_INTERFACE': 'Test/1.0',
+}
+
+class TestRequest(HTTPRequest):
+
+ def __init__(self, body_instream=None, outstream=None, environ=None, **kw):
+ if body_instream is None:
+ body_instream = StringIO('')
+ if outstream is None:
+ outstream = StringIO()
+
+
+ env = {}
+ env.update(_testEnv)
+ if environ: env.update(environ)
+ env.update(kw)
+
+ super(TestRequest, self).__init__(body_instream, outstream, env)
+
=== Zope3/lib/python/Zope/Publisher/HTTP/tests/__init__.py 1.1 => 1.2 ===
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (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.
+#
+##############################################################################
+""" Zope.Publisher.HTTP unit tests. """
=== Zope3/lib/python/Zope/Publisher/HTTP/tests/testHTTP.py 1.1 => 1.2 ===
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (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.
+#
+##############################################################################
+import unittest
+
+from Zope.Publisher.HTTP.HTTPRequest import HTTPRequest
+from Zope.Publisher.HTTP.HTTPResponse import HTTPResponse
+
+from Zope.Publisher.Publish import publish
+from Zope.Publisher.DefaultPublication import DefaultPublication
+
+from Interface.Verify import verifyObject
+from Interface.Implements import instancesOfObjectImplements
+
+from StringIO import StringIO
+
+
+class HTTPTests(unittest.TestCase):
+
+ _testEnv = {
+ 'PATH_INFO': '/folder/item',
+ 'a': '5',
+ 'b': 6,
+ 'SERVER_URL': 'http://foobar.com',
+ 'HTTP_HOST': 'foobar.com',
+ 'CONTENT_LENGTH': '0',
+ 'HTTP_AUTHORIZATION': 'Should be in accessible',
+ 'GATEWAY_INTERFACE': 'TestFooInterface/1.0',
+ 'HTTP_OFF_THE_WALL': "Spam 'n eggs",
+ }
+
+ def setUp(self):
+ class AppRoot:
+ " "
+
+ class Folder:
+ " "
+
+ class Item:
+ " "
+ def __call__(self, a, b):
+ return "%s, %s" % (`a`, `b`)
+
+ self.app = AppRoot()
+ self.app.folder = Folder()
+ self.app.folder.item = Item()
+
+ def _createRequest(self, extra_env={}, body="", outstream=None):
+ env = self._testEnv.copy()
+ env.update(extra_env)
+ if len(body):
+ env['CONTENT_LENGTH'] = str(len(body))
+
+ publication = DefaultPublication(self.app)
+ if outstream is None:
+ outstream = StringIO()
+ instream = StringIO(body)
+ request = HTTPRequest(instream, outstream, env)
+ request.setPublication(publication)
+ return request
+
+ def _publisherResults(self, extra_env={}, body=""):
+ outstream = StringIO()
+ request = self._createRequest(extra_env, body, outstream=outstream)
+ publish(request)
+ return outstream.getvalue()
+
+ def testTraversalToItem(self):
+ res = self._publisherResults()
+ self.failUnlessEqual(
+ res,
+ "Status: 200 Ok\r\n"
+ "Content-Length: 6\r\n"
+ "X-Powered-By: Zope (www.zope.org), Python (www.python.org)\r\n"
+ "\r\n"
+ "'5', 6")
+
+ def testRequestEnvironment(self):
+ req = self._createRequest()
+ publish(req) # Force expansion of URL variables
+
+ self.assertEquals(str(req.URL), 'http://foobar.com/folder/item')
+ self.assertEquals(req.URL['-1'], 'http://foobar.com/folder')
+ self.assertEquals(req.URL['-2'], 'http://foobar.com')
+ self.assertRaises(KeyError, req.URL.__getitem__, '-3')
+
+ self.assertEquals(req.URL['0'], 'http://foobar.com')
+ self.assertEquals(req.URL['1'], 'http://foobar.com/folder')
+ self.assertEquals(req.URL['2'], 'http://foobar.com/folder/item')
+ self.assertRaises(KeyError, req.URL.__getitem__, '3')
+
+ self.assertEquals(req['SERVER_URL'], 'http://foobar.com')
+ self.assertEquals(req['HTTP_HOST'], 'foobar.com')
+ self.assertEquals(req['PATH_INFO'], '/folder/item')
+ self.assertEquals(req['CONTENT_LENGTH'], '0')
+ self.assertRaises(KeyError, req.__getitem__, 'HTTP_AUTHORIZATION')
+ self.assertEquals(req['GATEWAY_INTERFACE'], 'TestFooInterface/1.0')
+ self.assertEquals(req['HTTP_OFF_THE_WALL'], "Spam 'n eggs")
+
+ self.assertRaises(KeyError, req.__getitem__,
+ 'HTTP_WE_DID_NOT_PROVIDE_THIS')
+
+ def testCookies(self):
+ cookies = {
+ 'HTTP_COOKIE': 'foo=bar; spam="eggs", this="Should be accepted"'
+ }
+ req = self._createRequest(extra_env=cookies)
+
+ self.assertEquals(req.cookies['foo'], 'bar')
+ self.assertEquals(req['foo'], 'bar')
+
+ self.assertEquals(req.cookies['spam'], 'eggs')
+ self.assertEquals(req['spam'], 'eggs')
+
+ self.assertEquals(req.cookies['this'], 'Should be accepted')
+ self.assertEquals(req['this'], 'Should be accepted')
+
+ def testBasicAuth(self):
+ from Zope.Publisher.HTTP.IHTTPCredentials import IHTTPCredentials
+ import base64
+ req = self._createRequest()
+ verifyObject(IHTTPCredentials, req)
+ lpq = req._authUserPW()
+ self.assertEquals(lpq, None)
+ env = {}
+ login, password = ("tim", "123")
+ s = base64.encodestring("%s:%s" % (login, password)).rstrip()
+ env['HTTP_AUTHORIZATION'] = "Basic %s" % s
+ req = self._createRequest(env)
+ lpw = req._authUserPW()
+ self.assertEquals(lpw, (login, password))
+
+
+
+ def testIPresentationRequest(self):
+ ''' test the IView request'''
+
+ r = self._createRequest()
+
+ self.failUnless( r.getPresentationType() is None)
+ self.assertEqual( r.getPresentationSkin(), '')
+ r.setViewSkin( 'morefoo' )
+ self.assertEqual( r.getPresentationSkin(), 'morefoo')
+
+
+
+def test_suite():
+ loader = unittest.TestLoader()
+ return loader.loadTestsFromTestCase(HTTPTests)
+
+if __name__=='__main__':
+ unittest.TextTestRunner().run( test_suite() )
+