[Checkins] SVN: zope.testbrowser/branches/gary-cookie/src/zope/testbrowser/ some fixes and some tests that need more polish.

Gary Poster gary at modernsongs.com
Sun Nov 2 19:21:52 EST 2008


Log message for revision 92754:
  some fixes and some tests that need more polish.

Changed:
  U   zope.testbrowser/branches/gary-cookie/src/zope/testbrowser/README.txt
  U   zope.testbrowser/branches/gary-cookie/src/zope/testbrowser/cookies.py

-=-
Modified: zope.testbrowser/branches/gary-cookie/src/zope/testbrowser/README.txt
===================================================================
--- zope.testbrowser/branches/gary-cookie/src/zope/testbrowser/README.txt	2008-11-02 16:58:14 UTC (rev 92753)
+++ zope.testbrowser/branches/gary-cookie/src/zope/testbrowser/README.txt	2008-11-03 00:21:50 UTC (rev 92754)
@@ -618,6 +618,51 @@
 
 XXX show can't set hidden cookie, but can hide another cookie
 
+The ``expire`` method is really just a convenience.  Here are some examples.
+XXX this pretty much straight from an email with John.  Adjust to put in
+context.
+
+    >>> import zope.testbrowser.cookies
+    >>> import mechanize
+    >>> import datetime
+    >>> br = mechanize.Browser()
+    >>> cookies = zope.testbrowser.cookies.Cookies(br, 'http://example.com')
+    >>> cookies.set('foo', 'bar', domain='example.com')
+    >>> cookies.expire('foo', datetime.datetime(2021, 1, 1))
+    >>> cookies.getinfo('foo')['expires']
+    datetime.datetime(2021, 1, 1, 0, 0, tzinfo=<UTC>)
+    >>> cookies.expire('foo')
+    >>> len(cookies)
+    0
+
+That's the main story.  Now here are some edge cases.
+
+    >>> cookies.set('foo', 'bar', domain='example.com')
+    >>> cookies.expire(
+    ...     'foo',
+    ...     zope.testbrowser.cookies.expiration_string(
+    ...         datetime.datetime(2020, 1, 1)))
+    >>> cookies.getinfo('foo')['expires']
+    datetime.datetime(2020, 1, 1, 0, 0, tzinfo=<UTC>)
+    >>> cookies.forURL('http://example.com').expire(
+    ...     'foo',
+    ...     zope.testbrowser.cookies.expiration_string(
+    ...         datetime.datetime(2019, 1, 1)))
+    >>> cookies.getinfo('foo')['expires']
+    datetime.datetime(2019, 1, 1, 0, 0, tzinfo=<UTC>)
+    >>> cookies['foo']
+    'bar'
+    >>> cookies.expire('foo', datetime.datetime(1999, 1, 1))
+    >>> len(cookies)
+    0
+    >>> cookies.expire(
+    ...     'foo',
+    ...     zope.testbrowser.cookies.expiration_string(
+    ...         datetime.datetime(1999, 1, 1)))
+    >>> len(cookies)
+    0
+
+
 #Note that explicitly setting a Cookie header is an error if the ``cookies``
 #mapping has any values; and adding a new cookie to the ``cookies`` mapping
 #is an error if the Cookie header is already set.

Modified: zope.testbrowser/branches/gary-cookie/src/zope/testbrowser/cookies.py
===================================================================
--- zope.testbrowser/branches/gary-cookie/src/zope/testbrowser/cookies.py	2008-11-02 16:58:14 UTC (rev 92753)
+++ zope.testbrowser/branches/gary-cookie/src/zope/testbrowser/cookies.py	2008-11-03 00:21:50 UTC (rev 92754)
@@ -15,7 +15,7 @@
     HOUR = datetime.timedelta(hours=1)
 
 
-    class UTC(datetime.tzinfo):
+    class _UTC(datetime.tzinfo):
         """UTC
 
         The reference UTC implementation given in Python docs.
@@ -49,6 +49,8 @@
         def __str__(self):
             return "UTC"
 
+    UTC = _UTC()
+
 # Cookies class helpers
 
 
@@ -238,6 +240,12 @@
             # you CAN hide an existing cookie, by passing an explicit path
         elif use_ck:
             path = ck.path
+        if expires is not None and self._is_expired(expires):
+            if use_ck:
+                raise ValueError('May not delete a cookie using ``set``')
+            else:
+                raise ValueError(
+                    'May not create a cookie that is immediately ignored')
         version = None
         if use_ck:
             # keep unchanged existing cookie values
@@ -259,6 +267,8 @@
         # else...if the domain is bad, set_cookie_if_ok should catch it.
         c = Cookie.SimpleCookie()
         name = str(name)
+        if value is None:
+            raise ValueError('if cookie does not exist, must provide value')
         c[name] = value.encode('utf8')
         if secure:
             c[name]['secure'] = True
@@ -281,6 +291,8 @@
         # fact supported by the documented client cookie API.
         cookies = self._jar.make_cookies(
             _StubResponse([c.output(header='').strip()]), request)
+        assert len(cookies) == 1, (
+            'programmer error: %d cookies made' % (len(cookies),))
         self._jar.set_cookie_if_ok(cookies[0], request)
 
     def update(self, source=None, **kwargs):
@@ -301,13 +313,25 @@
     def __setitem__(self, key, value):
         self.set(key, value)
 
+    def _is_expired(self, value):
+        if isinstance(value, datetime.datetime):
+            if value.tzinfo is None:
+                 if value <= datetime.datetime.utcnow():
+                    return True
+            elif value <= datetime.datetime.now(UTC):
+                return True
+        elif isinstance(value, basestring):
+            if datetime.datetime.fromtimestamp(
+                mechanize.str2time(value),
+                UTC) <= datetime.datetime.now(UTC):
+                return True
+        return False
+
     def expire(self, name, expires=None):
-        if expires is None:
+        if expires is None or self._is_expired(expires):
             del self[name]
         else:
-            ck = self._get(name)
-            self.set(ck.name, ck.value, expires, ck.domain, ck.path, ck.secure,
-                     ck.comment)
+            res = self.set(name, expires=expires)
 
     def clear(self):
         # to give expected mapping behavior of resulting in an empty dict,



More information about the Checkins mailing list