[Zope3-checkins] SVN: Zope3/trunk/ Provide a testcase for the CookieHandler class; improve the httpCookie method to only print the values, not the Set-Cookie parameters

Martijn Pieters mj at zopatista.com
Wed Aug 2 05:49:27 EDT 2006


Log message for revision 69336:
  Provide a testcase for the CookieHandler class; improve the httpCookie method to only print the values, not the Set-Cookie parameters

Changed:
  U   Zope3/trunk/doc/CHANGES.txt
  U   Zope3/trunk/src/zope/app/session/ftests.py
  U   Zope3/trunk/src/zope/app/testing/functional.py
  U   Zope3/trunk/src/zope/app/testing/tests.py

-=-
Modified: Zope3/trunk/doc/CHANGES.txt
===================================================================
--- Zope3/trunk/doc/CHANGES.txt	2006-08-02 07:20:09 UTC (rev 69335)
+++ Zope3/trunk/doc/CHANGES.txt	2006-08-02 09:49:27 UTC (rev 69336)
@@ -89,6 +89,8 @@
         cliend id cookies fix up the set cookie path whenever the virtual host
         information changes during a request.
 
+      - Fixed cookie path handling in functional tests.
+
     Much thanks to everyone who contributed to this release:
 
       Jim Fulton, Dmitry Vasiliev

Modified: Zope3/trunk/src/zope/app/session/ftests.py
===================================================================
--- Zope3/trunk/src/zope/app/session/ftests.py	2006-08-02 07:20:09 UTC (rev 69335)
+++ Zope3/trunk/src/zope/app/session/ftests.py	2006-08-02 09:49:27 UTC (rev 69336)
@@ -19,6 +19,8 @@
 from zope.app.testing.functional import BrowserTestCase
 from zope.app.zptpage.zptpage import ZPTPage
 
+from interfaces import ISession
+
 class ZPTSessionTest(BrowserTestCase):
     content = u'''
         <div tal:define="
@@ -57,11 +59,36 @@
         self.failUnlessEqual(response2, u'2')
         response3 = self.fetch()
         self.failUnlessEqual(response3, u'3')
+        
 
+class VirtualHostSessionTest(BrowserTestCase):
+    def setUp(self):
+        super(VirtualHostSessionTest, self).setUp()
+        page = ZPTPage()
+        page.source = (u'<div '
+                       u'tal:define="session request/session:products.foo"/>')
+        page.evaluateInlineCode = True
+        root = self.getRootFolder()
+        root['page'] = page
+        self.commit()
+        
+    def tearDown(self):
+        root = self.getRootFolder()
+        del root['page']
+        self.commit()
+        super(VirtualHostSessionTest, self).tearDown()
+    
+    def testShortendPath(self):
+        response = self.publish(
+            '/++skin++Rotterdam/page/++vh++http:localhost:80/++')
+        cookie = self.cookies.values()[0]
+        self.assertEqual(cookie['path'], '/')
+        
 
 def test_suite():
     return unittest.TestSuite((
         unittest.makeSuite(ZPTSessionTest),
+        unittest.makeSuite(VirtualHostSessionTest),
         ))
 
 if __name__ == '__main__':

Modified: Zope3/trunk/src/zope/app/testing/functional.py
===================================================================
--- Zope3/trunk/src/zope/app/testing/functional.py	2006-08-02 07:20:09 UTC (rev 69335)
+++ Zope3/trunk/src/zope/app/testing/functional.py	2006-08-02 09:49:27 UTC (rev 69336)
@@ -264,7 +264,7 @@
 
     def httpCookie(self, path):
          """Return self.cookies as an HTTP_COOKIE environment value."""
-         l = [m.OutputString() for m in self.cookies.values()
+         l = [m.OutputString().split(';')[0] for m in self.cookies.values()
               if path.startswith(m['path'])]
          return '; '.join(l)
 
@@ -275,6 +275,9 @@
         """Save cookies from the response."""
         # Urgh - need to play with the response's privates to extract
         # cookies that have been set
+        # TODO: extend the IHTTPRequest interface to allow access to all 
+        # cookies
+        # TODO: handle cookie expirations
         for k,v in response._cookies.items():
             k = k.encode('utf8')
             self.cookies[k] = v['value'].encode('utf8')

Modified: Zope3/trunk/src/zope/app/testing/tests.py
===================================================================
--- Zope3/trunk/src/zope/app/testing/tests.py	2006-08-02 07:20:09 UTC (rev 69335)
+++ Zope3/trunk/src/zope/app/testing/tests.py	2006-08-02 09:49:27 UTC (rev 69336)
@@ -181,12 +181,59 @@
         self.assert_(IRequest.implementedBy(request_class))
         self.assert_(IPublication.implementedBy(publication_class))
 
+class DummyCookiesResponse(object):
+    # Ugh, this simulates the *internals* of a HTTPResponse object
+    # TODO: expand the IHTTPResponse interface to give access to all cookies
+    _cookies = None
+    
+    def __init__(self, cookies=None):
+        if not cookies:
+            cookies = {}
+        self._cookies = cookies
+        
+class CookieHandlerTestCase(unittest.TestCase):
+    def setUp(self):
+        self.handler = functional.CookieHandler()
+    
+    def test_saveCookies(self):
+        response = DummyCookiesResponse(dict(
+            spam=dict(value='eggs', path='/foo', comment='rest is ignored'),
+            monty=dict(value='python')))
+        self.handler.saveCookies(response)
+        self.assertEqual(len(self.handler.cookies), 2)
+        self.assertEqual(self.handler.cookies['spam'].OutputString(),
+                         'spam=eggs; Path=/foo;')
+        self.assertEqual(self.handler.cookies['monty'].OutputString(),
+                         'monty=python;')
+        
+    def test_httpCookie(self):
+        cookies = self.handler.cookies
+        cookies['spam'] = 'eggs'
+        cookies['spam']['path'] = '/foo'
+        cookies['bar'] = 'baz'
+        cookies['bar']['path'] = '/foo/baz'
+        cookies['monty'] = 'python'
+        
+        cookieHeader = self.handler.httpCookie('/foo/bar')
+        parts = cookieHeader.split('; ')
+        parts.sort()
+        self.assertEqual(parts, ['monty=python', 'spam=eggs'])
+        
+        cookieHeader = self.handler.httpCookie('/foo/baz')
+        parts = cookieHeader.split('; ')
+        parts.sort()
+        self.assertEqual(parts, ['bar=baz', 'monty=python', 'spam=eggs'])
+        
+    # There is no test for CookieHandler.loadCookies because it that method
+    # only passes the arguments on to Cookie.BaseCookie.load, which the 
+    # standard library has tests for (we hope).
 
 def test_suite():
     return unittest.TestSuite((
         unittest.makeSuite(FunctionalHTTPDocTest),
         unittest.makeSuite(AuthHeaderTestCase),
         unittest.makeSuite(HTTPCallerTestCase),
+        unittest.makeSuite(CookieHandlerTestCase),
         ))
 
 if __name__ == '__main__':



More information about the Zope3-Checkins mailing list