Anonymous avatar Anonymous committed 6f63831

Finish the Expires setting (#253) that was accidentally committed in the previous commit. Set cookie Expires to be set in addition to Max-Age

Comments (0)

Files changed (3)

 * When parsing, interpret times with no timezone as UTC (previously
   they would be interpreted as local time).
 
+* Set the Expires property on cookies when using
+  ``response.set_cookie()``.  This is inherited from ``max_age``.
+
 0.9
 ---
 

tests/test_response.txt

     'test=value; Path=/;'
     >>> res.set_cookie('test2', 'value2', max_age=10000)
     >>> res.headers['set-cookie'] # We only see the last header
-    'test2=value2; Max-Age=10000; Path=/;'
+    'test2=value2; expires="... GMT"; Max-Age=10000; Path=/;'
     >>> res.headers.getall('set-cookie')
-    ['test=value; Path=/;', 'test2=value2; Max-Age=10000; Path=/;']
+    ['test=value; Path=/;', 'test2=value2; expires="... GMT"; Max-Age=10000; Path=/;']
     >>> res.unset_cookie('test')
     >>> res.headers.getall('set-cookie')
-    ['test2=value2; Max-Age=10000; Path=/;']
+    ['test2=value2; expires="... GMT"; Max-Age=10000; Path=/;']
 
 Most headers are available in a parsed getter/setter form through
 properties:

webob/__init__.py

 
     def set_cookie(self, key, value='', max_age=None,
                    path='/', domain=None, secure=None, httponly=False,
-                   version=None, comment=None):
+                   version=None, comment=None, expires=None):
         """
         Set (add) a cookie for the response
         """
         cookies[key] = value
         if isinstance(max_age, timedelta):
             max_age = timedelta.seconds + timedelta.days*24*60*60
-        if max_age is not None:
-            future = datetime.utcnow() + timedelta(seconds=max_age)
-            expires = _serialize_date(future)
-        else:
-            expires = None
+        if max_age is not None and expires is None:
+            expires = datetime.utcnow() + timedelta(seconds=max_age)
+        if isinstance(expires, timedelta):
+            expires = datetime.utcnow() + expires
+        if isinstance(expires, datetime):
+            expires = '"'+_serialize_date(expires)+'"'
         for var_name, var_value in [
             ('max_age', max_age),
             ('path', path),
         that it should expire immediately.
         """
         self.set_cookie(key, '', path=path, domain=domain,
-                        max_age=0)
+                        max_age=0, expires=timedelta(days=-5))
 
     def unset_cookie(self, key):
         """
             if key in cookies:
                 found = True
                 del cookies[key]
-            header = cookies.output(header='').lstrip()
+                header = cookies.output(header='').lstrip()
             if header:
                 self.headers.add('Set-Cookie', header)
         if not found:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.