Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #5982 -- Changed test client's URL processing to match core's (…

…everything

gets run through urllib.unquote()). Patch from Leo Shklovskii and Russell
Keith-Magee.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@7330 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 597f9d61050ee15df807bda6a7d9a0ff5def9774 1 parent 9e47cc2
Malcolm Tredinnick authored March 20, 2008
1  AUTHORS
@@ -311,6 +311,7 @@ answer newbie questions, and generally made Django that much better:
311 311
     serbaut@gmail.com
312 312
     John Shaffer <jshaffer2112@gmail.com>
313 313
     Pete Shinners <pete@shinners.org>
  314
+    Leo Shklovskii
314 315
     jason.sidabras@gmail.com
315 316
     Jozko Skrablin <jozko.skrablin@gmail.com>
316 317
     Ben Slavin <benjamin.slavin@gmail.com>
5  django/test/client.py
... ...
@@ -1,3 +1,4 @@
  1
+import urllib
1 2
 import sys
2 3
 from cStringIO import StringIO
3 4
 from django.conf import settings
@@ -208,7 +209,7 @@ def get(self, path, data={}, **extra):
208 209
         r = {
209 210
             'CONTENT_LENGTH':  None,
210 211
             'CONTENT_TYPE':    'text/html; charset=utf-8',
211  
-            'PATH_INFO':       path,
  212
+            'PATH_INFO':       urllib.unquote(path),
212 213
             'QUERY_STRING':    urlencode(data, doseq=True),
213 214
             'REQUEST_METHOD': 'GET',
214 215
         }
@@ -227,7 +228,7 @@ def post(self, path, data={}, content_type=MULTIPART_CONTENT, **extra):
227 228
         r = {
228 229
             'CONTENT_LENGTH': len(post_data),
229 230
             'CONTENT_TYPE':   content_type,
230  
-            'PATH_INFO':      path,
  231
+            'PATH_INFO':      urllib.unquote(path),
231 232
             'REQUEST_METHOD': 'POST',
232 233
             'wsgi.input':     StringIO(post_data),
233 234
         }
30  tests/regressiontests/test_client_regress/models.py
@@ -3,7 +3,7 @@
3 3
 
4 4
 """
5 5
 from django.test import Client, TestCase
6  
-from django.core import mail
  6
+from django.core.urlresolvers import reverse
7 7
 import os
8 8
 
9 9
 class AssertContainsTests(TestCase):
@@ -261,3 +261,31 @@ def test_login_different_client(self):
261 261
         # Check that assertRedirects uses the original client, not the
262 262
         # default client.
263 263
         self.assertRedirects(response, "http://testserver/test_client_regress/get_view/")
  264
+
  265
+
  266
+class URLEscapingTests(TestCase):
  267
+    def test_simple_argument_get(self):
  268
+        "Get a view that has a simple string argument"
  269
+        response = self.client.get(reverse('arg_view', args=['Slartibartfast']))
  270
+        self.assertEqual(response.status_code, 200)
  271
+        self.assertEqual(response.content, 'Howdy, Slartibartfast')
  272
+
  273
+    def test_argument_with_space_get(self):
  274
+        "Get a view that has a string argument that requires escaping"
  275
+        response = self.client.get(reverse('arg_view', args=['Arthur Dent']))
  276
+        self.assertEqual(response.status_code, 200)
  277
+        self.assertEqual(response.content, 'Hi, Arthur')
  278
+
  279
+    def test_simple_argument_post(self):
  280
+        "Post for a view that has a simple string argument"
  281
+        response = self.client.post(reverse('arg_view', args=['Slartibartfast']))
  282
+        self.assertEqual(response.status_code, 200)
  283
+        self.assertEqual(response.content, 'Howdy, Slartibartfast')
  284
+
  285
+    def test_argument_with_space_post(self):
  286
+        "Post for a view that has a string argument that requires escaping"
  287
+        response = self.client.post(reverse('arg_view', args=['Arthur Dent']))
  288
+        self.assertEqual(response.status_code, 200)
  289
+        self.assertEqual(response.content, 'Hi, Arthur')
  290
+
  291
+
1  tests/regressiontests/test_client_regress/urls.py
@@ -5,5 +5,6 @@
5 5
     (r'^no_template_view/$', views.no_template_view),
6 6
     (r'^file_upload/$', views.file_upload_view),
7 7
     (r'^get_view/$', views.get_view),
  8
+    url(r'^arg_view/(?P<name>.+)/$', views.view_with_argument, name='arg_view'),
8 9
     (r'^login_protected_redirect_view/$', views.login_protected_redirect_view)
9 10
 )
16  tests/regressiontests/test_client_regress/views.py
... ...
@@ -1,7 +1,5 @@
1 1
 from django.contrib.auth.decorators import login_required
2  
-from django.core.mail import EmailMessage, SMTPConnection
3 2
 from django.http import HttpResponse, HttpResponseRedirect, HttpResponseServerError
4  
-from django.shortcuts import render_to_response
5 3
 
6 4
 def no_template_view(request):
7 5
     "A simple view that expects a GET request, and returns a rendered template"
@@ -20,10 +18,22 @@ def file_upload_view(request):
20 18
         return HttpResponseServerError()
21 19
 
22 20
 def get_view(request):
23  
-    "A simple login protected view"    
  21
+    "A simple login protected view"
24 22
     return HttpResponse("Hello world")
25 23
 get_view = login_required(get_view)
26 24
 
  25
+def view_with_argument(request, name):
  26
+    """A view that takes a string argument
  27
+
  28
+    The purpose of this view is to check that if a space is provided in
  29
+    the argument, the test framework unescapes the %20 before passing
  30
+    the value to the view.
  31
+    """
  32
+    if name == 'Arthur Dent':
  33
+        return HttpResponse('Hi, Arthur')
  34
+    else:
  35
+        return HttpResponse('Howdy, %s' % name)
  36
+
27 37
 def login_protected_redirect_view(request):
28 38
     "A view that redirects all requests to the GET view"
29 39
     return HttpResponseRedirect('/test_client_regress/get_view/')

0 notes on commit 597f9d6

Please sign in to comment.
Something went wrong with that request. Please try again.