Code

Ticket #5176: 0001-Fixed-5176-TestClient-now-can-accept-SimpleCookies-w.patch

File 0001-Fixed-5176-TestClient-now-can-accept-SimpleCookies-w.patch, 4.3 KB (added by tclineks, 5 years ago)
  • AUTHORS

    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: 
    262262    Lau Bech Lauritzen 
    263263    Rune Rønde Laursen <runerl@skjoldhoej.dk> 
    264264    Eugene Lazutkin <http://lazutkin.com/blog/> 
    265     lcordier@point45.com 
     265    Louis Cordier <lcordier@gmail.com> 
    266266    Jeong-Min Lee <falsetru@gmail.com> 
    267267    Tai Lee <real.human@mrmachine.net> 
    268268    Jannis Leidel <jl@websushi.org> 
  • django/utils/cache.py

    diff --git a/django/utils/cache.py b/django/utils/cache.py
    index 3c40e17..b97e8a9 100644
    a b try: 
    2323    set 
    2424except NameError: 
    2525    from sets import Set as set   # Python 2.3 fallback 
     26from Cookie import SimpleCookie 
    2627 
    2728from django.conf import settings 
    2829from django.core.cache import cache 
    def _generate_cache_key(request, headerlist, key_prefix): 
    142143    ctx = md5_constructor() 
    143144    for header in headerlist: 
    144145        value = request.META.get(header, None) 
     146        if isinstance(value, SimpleCookie): 
     147            value = value.output() 
     148 
    145149        if value is not None: 
    146150            ctx.update(value) 
    147151    path = md5_constructor(iri_to_uri(request.path)) 
  • tests/regressiontests/test_client_regress/models.py

    diff --git a/tests/regressiontests/test_client_regress/models.py b/tests/regressiontests/test_client_regress/models.py
    index be2cb8f..1d38f51 100644
    a b  
    33Regression tests for the Test Client, especially the customized assertions. 
    44""" 
    55import os 
    6 from django.conf import settings 
     6from Cookie import SimpleCookie 
    77 
     8from django.conf import settings 
    89from django.test import Client, TestCase 
    910from django.test.utils import ContextList 
    1011from django.core.urlresolvers import reverse 
    class LoginTests(TestCase): 
    385386        # default client. 
    386387        self.assertRedirects(response, "http://testserver/test_client_regress/get_view/") 
    387388 
     389class 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 
    388400class URLEscapingTests(TestCase): 
    389401    def test_simple_argument_get(self): 
    390402        "Get a view that has a simple string argument" 
  • tests/regressiontests/test_client_regress/urls.py

    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('', 
    2424    (r'^request_methods/$', views.request_methods_view), 
    2525    (r'^check_unicode/$', views.return_unicode), 
    2626    (r'^parse_unicode_json/$', views.return_json_file), 
     27    (r'^set_cookie/$', views.set_cookie_view), 
    2728) 
  • tests/regressiontests/test_client_regress/views.py

    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): 
    8686                            mimetype='application/json; charset=' + charset) 
    8787    response['Content-Disposition'] = 'attachment; filename=testfile.json' 
    8888    return response 
     89 
     90def 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