Ticket #17931: 17931.diff

File 17931.diff, 2.6 KB (added by Aymeric Augustin, 13 years ago)
  • tests/regressiontests/requests/tests.py

     
    1212from django.test.utils import get_warnings_state, restore_warnings_state
    1313from django.utils import unittest
    1414from django.utils.http import cookie_date
     15from django.utils.timezone import utc
    1516
    1617
    1718class RequestsTests(unittest.TestCase):
     
    207208        datetime_cookie = response.cookies['datetime']
    208209        self.assertEqual(datetime_cookie['max-age'], 10)
    209210
     211    def test_aware_expiration(self):
     212        "Cookie accepts an aware datetime as expiration time"
     213        response = HttpResponse()
     214        expires = (datetime.utcnow() + timedelta(seconds=10)).replace(tzinfo=utc)
     215        time.sleep(0.001)
     216        response.set_cookie('datetime', expires=expires)
     217        datetime_cookie = response.cookies['datetime']
     218        self.assertEqual(datetime_cookie['max-age'], 10)
     219
    210220    def test_far_expiration(self):
    211221        "Cookie will expire when an distant expiration time is provided"
    212222        response = HttpResponse()
  • django/http/__init__.py

     
    121121from django.utils.datastructures import MultiValueDict, ImmutableList
    122122from django.utils.encoding import smart_str, iri_to_uri, force_unicode
    123123from django.utils.http import cookie_date
     124from django.utils import timezone
    124125
    125126RESERVED_CHARS="!*'();:@&=+$,/?%#[]"
    126127
     
    641642        """
    642643        Sets a cookie.
    643644
    644         ``expires`` can be a string in the correct format or a
    645         ``datetime.datetime`` object in UTC. If ``expires`` is a datetime
    646         object then ``max_age`` will be calculated.
     645        ``expires`` can be:
     646        - a string in the correct format,
     647        - a naive ``datetime.datetime`` object in UTC,
     648        - an aware ``datetime.datetime`` object in any time zone.
     649        If it is a ``datetime.datetime`` object then ``max_age`` will be calculated.
     650
    647651        """
    648652        self.cookies[key] = value
    649653        if expires is not None:
    650654            if isinstance(expires, datetime.datetime):
     655                if timezone.is_aware(expires):
     656                    expires = timezone.make_naive(expires, timezone.utc)
    651657                delta = expires - expires.utcnow()
    652658                # Add one second so the date matches exactly (a fraction of
    653659                # time gets lost between converting to a timedelta and
Back to Top