From 7df2dfff6c607d52878b7fad5b9a2dcaeda1c039 Mon Sep 17 00:00:00 2001
From: Travis Cline <travis.cline@gmail.com>
Date: Sun, 23 Aug 2009 12:06:38 -0500
Subject: [PATCH] Fixed #5176: TestClient now can accept SimpleCookies when generating cache keys
---
AUTHORS | 2 +-
django/utils/cache.py | 4 ++++
.../regressiontests/test_client_regress/models.py | 14 +++++++++++++-
tests/regressiontests/test_client_regress/urls.py | 1 +
tests/regressiontests/test_client_regress/views.py | 7 +++++++
5 files changed, 26 insertions(+), 2 deletions(-)
diff --git a/AUTHORS b/AUTHORS
index 58179e1..abdb5ce 100644
a
|
b
|
answer newbie questions, and generally made Django that much better:
|
262 | 262 | Lau Bech Lauritzen |
263 | 263 | Rune Rønde Laursen <runerl@skjoldhoej.dk> |
264 | 264 | Eugene Lazutkin <http://lazutkin.com/blog/> |
265 | | lcordier@point45.com |
| 265 | Louis Cordier <lcordier@gmail.com> |
266 | 266 | Jeong-Min Lee <falsetru@gmail.com> |
267 | 267 | Tai Lee <real.human@mrmachine.net> |
268 | 268 | Jannis Leidel <jl@websushi.org> |
diff --git a/django/utils/cache.py b/django/utils/cache.py
index 3c40e17..b97e8a9 100644
a
|
b
|
try:
|
23 | 23 | set |
24 | 24 | except NameError: |
25 | 25 | from sets import Set as set # Python 2.3 fallback |
| 26 | from Cookie import SimpleCookie |
26 | 27 | |
27 | 28 | from django.conf import settings |
28 | 29 | from django.core.cache import cache |
… |
… |
def _generate_cache_key(request, headerlist, key_prefix):
|
142 | 143 | ctx = md5_constructor() |
143 | 144 | for header in headerlist: |
144 | 145 | value = request.META.get(header, None) |
| 146 | if isinstance(value, SimpleCookie): |
| 147 | value = value.output() |
| 148 | |
145 | 149 | if value is not None: |
146 | 150 | ctx.update(value) |
147 | 151 | path = md5_constructor(iri_to_uri(request.path)) |
diff --git a/tests/regressiontests/test_client_regress/models.py b/tests/regressiontests/test_client_regress/models.py
index be2cb8f..1d38f51 100644
a
|
b
|
|
3 | 3 | Regression tests for the Test Client, especially the customized assertions. |
4 | 4 | """ |
5 | 5 | import os |
6 | | from django.conf import settings |
| 6 | from Cookie import SimpleCookie |
7 | 7 | |
| 8 | from django.conf import settings |
8 | 9 | from django.test import Client, TestCase |
9 | 10 | from django.test.utils import ContextList |
10 | 11 | from django.core.urlresolvers import reverse |
… |
… |
class LoginTests(TestCase):
|
385 | 386 | # default client. |
386 | 387 | self.assertRedirects(response, "http://testserver/test_client_regress/get_view/") |
387 | 388 | |
| 389 | class CookieTests(TestCase): |
| 390 | |
| 391 | def test_cookies(self): |
| 392 | "Check that cookies work in the test client, see ticket #5176." |
| 393 | |
| 394 | c = Client() |
| 395 | response = c.get('/test_client_regress/set_cookie/') |
| 396 | |
| 397 | self.assert_(isinstance(response.cookies, SimpleCookie)) |
| 398 | self.assert_('Set-Cookie: test_cookie=test_value; Path=/' in response.cookies.output()) |
| 399 | |
388 | 400 | class URLEscapingTests(TestCase): |
389 | 401 | def test_simple_argument_get(self): |
390 | 402 | "Get a view that has a simple string argument" |
diff --git a/tests/regressiontests/test_client_regress/urls.py b/tests/regressiontests/test_client_regress/urls.py
index 99eb7b7..df15aca 100644
a
|
b
|
urlpatterns = patterns('',
|
24 | 24 | (r'^request_methods/$', views.request_methods_view), |
25 | 25 | (r'^check_unicode/$', views.return_unicode), |
26 | 26 | (r'^parse_unicode_json/$', views.return_json_file), |
| 27 | (r'^set_cookie/$', views.set_cookie_view), |
27 | 28 | ) |
diff --git a/tests/regressiontests/test_client_regress/views.py b/tests/regressiontests/test_client_regress/views.py
index 5ba7cc3..fc6b7ae 100644
a
|
b
|
def return_json_file(request):
|
86 | 86 | mimetype='application/json; charset=' + charset) |
87 | 87 | response['Content-Disposition'] = 'attachment; filename=testfile.json' |
88 | 88 | return response |
| 89 | |
| 90 | def set_cookie_view(request): |
| 91 | "A view that sets a cookie." |
| 92 | response = HttpResponse('set cookie') |
| 93 | response.set_cookie('test_cookie', 'test_value') |
| 94 | |
| 95 | return response |