[Checkins] SVN: Sandbox/philikon/five.publication/trunk/five/publication/ More tests, test documentation, and API compatibility with ZPublisher.

Philipp von Weitershausen philikon at philikon.de
Sun Aug 5 16:29:52 EDT 2007


Log message for revision 78603:
  More tests, test documentation, and API compatibility with ZPublisher.
  

Changed:
  U   Sandbox/philikon/five.publication/trunk/five/publication/request.py
  U   Sandbox/philikon/five.publication/trunk/five/publication/tests/test_environment.py

-=-
Modified: Sandbox/philikon/five.publication/trunk/five/publication/request.py
===================================================================
--- Sandbox/philikon/five.publication/trunk/five/publication/request.py	2007-08-05 19:47:43 UTC (rev 78602)
+++ Sandbox/philikon/five.publication/trunk/five/publication/request.py	2007-08-05 20:29:51 UTC (rev 78603)
@@ -80,6 +80,10 @@
             raise AttributeError(key)
         return value
 
+    @property
+    def URL(self):
+        return self.getURL()
+
 class BrowserResponse(zope.publisher.browser.BrowserResponse):
 
     @property

Modified: Sandbox/philikon/five.publication/trunk/five/publication/tests/test_environment.py
===================================================================
--- Sandbox/philikon/five.publication/trunk/five/publication/tests/test_environment.py	2007-08-05 19:47:43 UTC (rev 78602)
+++ Sandbox/philikon/five.publication/trunk/five/publication/tests/test_environment.py	2007-08-05 20:29:51 UTC (rev 78603)
@@ -4,18 +4,21 @@
 from ZPublisher.HTTPResponse import HTTPResponse
 from five.publication.request import BrowserRequest
 
-environ = {
+test_body = 'gun=reload&terrorist=kill'
+
+test_environ = {
+    # XXX better cookie
     'HTTP_COOKIE': 'tree-s=eJzT0MgpMOQKVneEA1dbda4CI67EkgJjLj0AeGcHew',
     'SCRIPT_NAME': '/john/mc/clane',
-    'REQUEST_METHOD': 'GET',
+    'REQUEST_METHOD': 'POST',
     'PATH_INFO': '/john/mc/clane',
     'SERVER_PROTOCOL': 'HTTP/1.1',
     'QUERY_STRING': '',
-    'CONTENT_LENGTH': '0',
+    'CONTENT_LENGTH': str(len(test_body)),
     'SERVER_NAME': 'diehard.tv',
     'REMOTE_ADDR': '127.0.0.1',
     'SERVER_PORT': '8080',
-    'CONTENT_TYPE': '',
+    'CONTENT_TYPE': 'application/x-www-form-urlencoded',
     'HTTP_ACCEPT_CHARSET': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
     'HTTP_CONNECTION': 'keep-alive',
     'HTTP_HOST': 'diehard.tv:8080',
@@ -26,11 +29,19 @@
     }
 
 class TestEnvironment(unittest.TestCase):
+    """
+    Test various aspects of the request's behaviour regarding the
+    CGI/WSGI environment and other environment variables.
+    """
 
     def setUp(self):
         pass
 
     def test_url(self):
+        # ZPublisher's HTTPRequest allows to access different sub-URLs
+        # of the current URL as URL0, URL1, URL2, etc.  This is the
+        # equivalent of calling request.getURL(n) for zope.publisher's
+        # request.
         request = self.makeRequest()
         self.assertEqual(request['URL'], 'http://diehard.tv:8080/john/mc/clane')
         self.assertEqual(request['URL0'], 'http://diehard.tv:8080/john/mc/clane')
@@ -40,6 +51,10 @@
         self.assertRaises(KeyError, request.get, 'URL4')
 
     def test_urlpath(self):
+        # URLPATH0, etc. works the same as URL0, etc., except that the
+        # URL scheme and server name aren't included, just the path
+        # part.  This is the equivalent of calling request.getURL(n,
+        # True) for zope.publisher's request.
         request = self.makeRequest()
         self.assertEqual(request['URLPATH0'], '/john/mc/clane')
         self.assertEqual(request['URLPATH1'], '/john/mc')
@@ -48,6 +63,7 @@
         self.assertRaises(KeyError, request.get, 'URLPATH4')
 
     def test_base(self):
+        # XXX I have no idea what BASE does... help?
         request = self.makeRequest()
         self.assertEqual(request['BASE0'], 'http://diehard.tv:8080/john/mc')
         self.assertEqual(request['BASE1'],
@@ -55,26 +71,61 @@
         self.assertRaises(KeyError, request.get, 'BASE2')
 
     def test_basepath(self):
+        # XXX I have no idea what BASEPATH does... help?
         request = self.makeRequest()
         self.assertEqual(request['BASEPATH0'], '/john/mc')
         self.assertEqual(request['BASEPATH1'], '/john/mc/clane')
         self.assertRaises(KeyError, request.get, 'BASEPATH2')
 
     def test_response(self):
+        # A request's response is typically available as
+        # request.response.  However, for legacy reasons, ZPublisher
+        # also supports request.RESPONSE as well as
+        # request['RESPONSE'].  We make sure both are supported here.
         request = self.makeRequest()
         self.assert_(request.get('RESPONSE') is not None)
         self.assert_(getattr(request, 'RESPONSE', None) is not None)
 
+    def test_getattr(self):
+        # The ZPublisher's HTTPRequest also allows access to
+        # environment variables via attribute access (I know, it
+        # sucks).
+        request = self.makeRequest()
+        self.assertEqual(request.PATH_INFO, '/john/mc/clane')
+
+        # Note that URL is a bit of a special
+        # fellow... zope.publisher's request.URL is a class property
+        # that supports a __str__ method.  In five.publisher, we
+        # really need it to be a string.
+        self.assertEqual(request.URL, 'http://diehard.tv:8080/john/mc/clane')
+        self.assertEqual(request.URL[:17], 'http://diehard.tv')
+
+    def test_body(self):
+        pass # TODO
+
+    def test_form(self):
+        # Both ZPublisher's and zope.publisher's request offer a
+        # request.form dictionary which contains request input
+        # specifically from GET or POST forms
+        request = self.makeRequest()
+        self.assertEqual(request.form['gun'], 'reload')
+        self.assertEqual(request.form['terrorist'], 'kill')
+        self.assert_(request.form.get('CONTENT_TYPE') is None)
+
+    def test_cookies(self):
+        pass # TODO
+
 class FivePublicationTestEnvironment(TestEnvironment):
 
     def makeRequest(self):
-        return BrowserRequest(StringIO(''), environ.copy())
+        return BrowserRequest(StringIO(test_body), test_environ.copy())
 
 class ZPublisherTestEnvironment(TestEnvironment):
 
     def makeRequest(self):
         response = HTTPResponse()
-        request = HTTPRequest(StringIO(''), environ.copy(), response)
+        request = HTTPRequest(StringIO(test_body), test_environ.copy(),
+                              response)
         # This dance is sadly necessary to get the request's
         # environment set up correctly
         request['PARENTS'] = [object()]



More information about the Checkins mailing list