Code

Ticket #5176: 0002_generate_cache_key.diff

File 0002_generate_cache_key.diff, 3.6 KB (added by lcordier, 6 years ago)

Some basic test code added, simply check that cookies work the they are supposed to work.

Line 
1Index: django/utils/cache.py
2===================================================================
3--- django/utils/cache.py       (revision 9212)
4+++ django/utils/cache.py       (working copy)
5@@ -23,6 +23,7 @@
6     set
7 except NameError:
8     from sets import Set as set   # Python 2.3 fallback
9+from Cookie import SimpleCookie
10 
11 from django.conf import settings
12 from django.core.cache import cache
13@@ -141,6 +142,9 @@
14     ctx = md5_constructor()
15     for header in headerlist:
16         value = request.META.get(header, None)
17+        if isinstance(value, SimpleCookie):
18+            value = value.output()
19+       
20         if value is not None:
21             ctx.update(value)
22     return 'views.decorators.cache.cache_page.%s.%s.%s' % (
23Index: tests/regressiontests/test_client_regress/views.py
24===================================================================
25--- tests/regressiontests/test_client_regress/views.py  (revision 9212)
26+++ tests/regressiontests/test_client_regress/views.py  (working copy)
27@@ -46,4 +46,11 @@
28 
29 def request_methods_view(request):
30     "A view that responds with the request method"
31-    return HttpResponse('request method: %s' % request.method)
32\ No newline at end of file
33+    return HttpResponse('request method: %s' % request.method)
34+
35+def set_cookie_view(request):
36+    "A view that sets a cookie."
37+    response = HttpResponse('set cookie')
38+    response.set_cookie('test_cookie', 'test_value')
39+   
40+    return response
41Index: tests/regressiontests/test_client_regress/models.py
42===================================================================
43--- tests/regressiontests/test_client_regress/models.py (revision 9212)
44+++ tests/regressiontests/test_client_regress/models.py (working copy)
45@@ -2,8 +2,9 @@
46 Regression tests for the Test Client, especially the customized assertions.
47 """
48 import os
49+from Cookie import SimpleCookie
50+
51 from django.conf import settings
52-
53 from django.test import Client, TestCase
54 from django.core.urlresolvers import reverse
55 from django.core.exceptions import SuspiciousOperation
56@@ -261,6 +262,17 @@
57         # default client.
58         self.assertRedirects(response, "http://testserver/test_client_regress/get_view/")
59 
60+class CookieTests(TestCase):
61+   
62+    def test_cookies(self):
63+        "Check that cookies work in the test client, see ticket #5176."
64+
65+        c = Client()
66+        response = c.get('/test_client_regress/set_cookie/')
67+
68+        self.assert_(isinstance(response.cookies, SimpleCookie))
69+        self.assert_(response.cookies.output().find('Set-Cookie: test_cookie=test_value; Path=/') >= 0)
70+
71 class URLEscapingTests(TestCase):
72     def test_simple_argument_get(self):
73         "Get a view that has a simple string argument"
74Index: tests/regressiontests/test_client_regress/urls.py
75===================================================================
76--- tests/regressiontests/test_client_regress/urls.py   (revision 9212)
77+++ tests/regressiontests/test_client_regress/urls.py   (working copy)
78@@ -10,4 +10,5 @@
79     (r'^set_session/$', views.set_session_view),
80     (r'^check_session/$', views.check_session_view),
81     (r'^request_methods/$', views.request_methods_view),
82+    (r'^set_cookie/$', views.set_cookie_view)
83 )
84Index: AUTHORS
85===================================================================
86--- AUTHORS     (revision 9212)
87+++ AUTHORS     (working copy)
88@@ -239,7 +239,7 @@
89     Lau Bech Lauritzen
90     Rune Rønde Laursen <runerl@skjoldhoej.dk>
91     Eugene Lazutkin <http://lazutkin.com/blog/>
92-    lcordier@point45.com
93+    Louis Cordier <lcordier@gmail.com>
94     Jeong-Min Lee <falsetru@gmail.com>
95     Jannis Leidel <jl@websushi.org>
96     Christopher Lenz <http://www.cmlenz.net/>