Changeset 4464
- Timestamp:
- 02/09/07 07:47:36 (2 years ago)
- Files:
-
- django/trunk/django/test/client.py (modified) (4 diffs)
- django/trunk/docs/testing.txt (modified) (2 diffs)
- django/trunk/tests/modeltests/test_client/models.py (modified) (1 diff)
- django/trunk/tests/modeltests/test_client/urls.py (modified) (1 diff)
- django/trunk/tests/modeltests/test_client/views.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/test/client.py
r4265 r4464 1 1 from cStringIO import StringIO 2 from django.conf import settings 2 3 from django.core.handlers.base import BaseHandler 3 4 from django.core.handlers.wsgi import WSGIRequest … … 98 99 self.handler = ClientHandler() 99 100 self.defaults = defaults 100 self.cookie = SimpleCookie() 101 self.cookies = SimpleCookie() 102 self.session = {} 101 103 102 104 def request(self, **request): … … 109 111 110 112 environ = { 111 'HTTP_COOKIE': self.cookie ,113 'HTTP_COOKIE': self.cookies, 112 114 'PATH_INFO': '/', 113 115 'QUERY_STRING': '', … … 142 144 143 145 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 146 154 return response 147 155 django/trunk/docs/testing.txt
r4368 r4464 199 199 .. _Selenium: http://www.openqa.org/selenium/ 200 200 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 Client203 instance. Expiry policies for these cookies are not followed; if you want204 a cookie to expire, either delete it manually from ``client.cookies``, or205 create a new Client instance (which will effectively delete all cookies).206 201 207 202 Making requests … … 296 291 297 292 .. _RFC2616: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html 293 294 Persistent state 295 ~~~~~~~~~~~~~~~~ 296 297 The Test Client is stateful; if a cookie is returned as part of a response, 298 that cookie is provided as part of the next request issued by that Client 299 instance. Expiry policies for these cookies are not followed; if you want 300 a cookie to expire, either delete it manually or create a new Client 301 instance (which will effectively delete all cookies). 302 303 There are two properties of the Test Client which are used to store persistent 304 state information. If necessary, these properties can be interrogated as 305 part 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 318 Example 319 ~~~~~~~ 298 320 299 321 The following is a simple unit test using the Test Client:: django/trunk/tests/modeltests/test_client/models.py
r3708 r4464 100 100 response = self.client.login('/test_client/login_protected_view/', 'otheruser', 'nopassword') 101 101 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 7 7 (r'^redirect_view/$', views.redirect_view), 8 8 (r'^login_protected_view/$', views.login_protected_view), 9 (r'^session_view/$', views.session_view) 9 10 ) django/trunk/tests/modeltests/test_client/views.py
r4451 r4464 34 34 return HttpResponse(t.render(c)) 35 35 login_protected_view = login_required(login_protected_view) 36 37 def 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
