Django

Code

Changeset 4464

Show
Ignore:
Timestamp:
02/09/07 07:47:36 (2 years ago)
Author:
russellm
Message:

Added a session attribute to the test Client, to make it easier to test if session variables have been modified in a view. Also renamed Client.cookie to Client.cookies, to match documentation and common sense.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/test/client.py

    r4265 r4464  
    11from cStringIO import StringIO 
     2from django.conf import settings 
    23from django.core.handlers.base import BaseHandler 
    34from django.core.handlers.wsgi import WSGIRequest 
     
    9899        self.handler = ClientHandler() 
    99100        self.defaults = defaults 
    100         self.cookie = SimpleCookie() 
     101        self.cookies = SimpleCookie() 
     102        self.session = {} 
    101103 
    102104    def request(self, **request): 
     
    109111 
    110112        environ = { 
    111             'HTTP_COOKIE':      self.cookie
     113            'HTTP_COOKIE':      self.cookies
    112114            'PATH_INFO':         '/', 
    113115            'QUERY_STRING':      '', 
     
    142144 
    143145        if response.cookies: 
    144             self.cookie.update(response.cookies) 
    145  
     146            self.cookies.update(response.cookies) 
     147 
     148        if 'django.contrib.sessions' in settings.INSTALLED_APPS: 
     149            from django.contrib.sessions.middleware import SessionWrapper 
     150            cookie = self.cookies.get(settings.SESSION_COOKIE_NAME, None) 
     151            if cookie: 
     152                self.session = SessionWrapper(cookie.value) 
     153             
    146154        return response 
    147155 
  • django/trunk/docs/testing.txt

    r4368 r4464  
    199199.. _Selenium: http://www.openqa.org/selenium/ 
    200200 
    201 The Test Client is stateful; if a cookie is returned as part of a response, 
    202 that cookie is provided as part of the next request issued to that Client 
    203 instance. Expiry policies for these cookies are not followed; if you want  
    204 a cookie to expire, either delete it manually from ``client.cookies``, or  
    205 create a new Client instance (which will effectively delete all cookies). 
    206201 
    207202Making requests 
     
    296291 
    297292.. _RFC2616: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html 
     293 
     294Persistent state 
     295~~~~~~~~~~~~~~~~ 
     296 
     297The Test Client is stateful; if a cookie is returned as part of a response, 
     298that cookie is provided as part of the next request issued by that Client 
     299instance. Expiry policies for these cookies are not followed; if you want  
     300a cookie to expire, either delete it manually or create a new Client  
     301instance (which will effectively delete all cookies). 
     302 
     303There are two properties of the Test Client which are used to store persistent 
     304state information. If necessary, these properties can be interrogated as 
     305part of a test condition. 
     306 
     307    ===============  ========================================================== 
     308    Property         Description 
     309    ===============  ========================================================== 
     310    ``cookies``      A Python ``SimpleCookie`` object, containing the current 
     311                     values of all the client cookies. 
     312 
     313    ``session``      A dictionary-like object containing session information. 
     314                     See the `session documentation`_ for full details. 
     315 
     316.. _`session documentation`: ../sessions/ 
     317                      
     318Example 
     319~~~~~~~ 
    298320 
    299321The following is a simple unit test using the Test Client:: 
  • django/trunk/tests/modeltests/test_client/models.py

    r3708 r4464  
    100100        response = self.client.login('/test_client/login_protected_view/', 'otheruser', 'nopassword') 
    101101        self.assertFalse(response) 
     102 
     103    def test_session_modifying_view(self): 
     104        "Request a page that modifies the session" 
     105        # Session value isn't set initially 
     106        try: 
     107            self.client.session['tobacconist'] 
     108            self.fail("Shouldn't have a session value") 
     109        except KeyError: 
     110            pass 
     111         
     112        from django.contrib.sessions.models import Session 
     113        response = self.client.post('/test_client/session_view/') 
     114         
     115        # Check that the session was modified 
     116        self.assertEquals(self.client.session['tobacconist'], 'hovercraft') 
     117         
  • django/trunk/tests/modeltests/test_client/urls.py

    r3708 r4464  
    77    (r'^redirect_view/$', views.redirect_view), 
    88    (r'^login_protected_view/$', views.login_protected_view), 
     9    (r'^session_view/$', views.session_view) 
    910) 
  • django/trunk/tests/modeltests/test_client/views.py

    r4451 r4464  
    3434    return HttpResponse(t.render(c)) 
    3535login_protected_view = login_required(login_protected_view) 
     36 
     37def session_view(request): 
     38    "A view that modifies the session" 
     39     
     40    request.session['tobacconist'] = 'hovercraft' 
     41     
     42    t = Template('This is a view that modifies the session.',  
     43                 name='Session Modifying View Template') 
     44    c = Context() 
     45    return HttpResponse(t.render(c)) 
     46