From 597f9d61050ee15df807bda6a7d9a0ff5def9774 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Thu, 20 Mar 2008 06:50:54 +0000 Subject: [PATCH] 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 --- AUTHORS | 1 + django/test/client.py | 5 ++-- .../test_client_regress/models.py | 30 ++++++++++++++++++- .../test_client_regress/urls.py | 1 + .../test_client_regress/views.py | 16 ++++++++-- 5 files changed, 47 insertions(+), 6 deletions(-) diff --git a/AUTHORS b/AUTHORS index 08d085bfec..947b750289 100644 --- a/AUTHORS +++ b/AUTHORS @@ -311,6 +311,7 @@ answer newbie questions, and generally made Django that much better: serbaut@gmail.com John Shaffer Pete Shinners + Leo Shklovskii jason.sidabras@gmail.com Jozko Skrablin Ben Slavin diff --git a/django/test/client.py b/django/test/client.py index 67d3aa9030..bac28f797b 100644 --- a/django/test/client.py +++ b/django/test/client.py @@ -1,3 +1,4 @@ +import urllib import sys from cStringIO import StringIO from django.conf import settings @@ -208,7 +209,7 @@ class Client: r = { 'CONTENT_LENGTH': None, 'CONTENT_TYPE': 'text/html; charset=utf-8', - 'PATH_INFO': path, + 'PATH_INFO': urllib.unquote(path), 'QUERY_STRING': urlencode(data, doseq=True), 'REQUEST_METHOD': 'GET', } @@ -227,7 +228,7 @@ class Client: r = { 'CONTENT_LENGTH': len(post_data), 'CONTENT_TYPE': content_type, - 'PATH_INFO': path, + 'PATH_INFO': urllib.unquote(path), 'REQUEST_METHOD': 'POST', 'wsgi.input': StringIO(post_data), } diff --git a/tests/regressiontests/test_client_regress/models.py b/tests/regressiontests/test_client_regress/models.py index b5d9ae63b9..305ccc9aa3 100644 --- a/tests/regressiontests/test_client_regress/models.py +++ b/tests/regressiontests/test_client_regress/models.py @@ -3,7 +3,7 @@ Regression tests for the Test Client, especially the customized assertions. """ from django.test import Client, TestCase -from django.core import mail +from django.core.urlresolvers import reverse import os class AssertContainsTests(TestCase): @@ -261,3 +261,31 @@ class LoginTests(TestCase): # Check that assertRedirects uses the original client, not the # default client. self.assertRedirects(response, "http://testserver/test_client_regress/get_view/") + + +class URLEscapingTests(TestCase): + def test_simple_argument_get(self): + "Get a view that has a simple string argument" + response = self.client.get(reverse('arg_view', args=['Slartibartfast'])) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.content, 'Howdy, Slartibartfast') + + def test_argument_with_space_get(self): + "Get a view that has a string argument that requires escaping" + response = self.client.get(reverse('arg_view', args=['Arthur Dent'])) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.content, 'Hi, Arthur') + + def test_simple_argument_post(self): + "Post for a view that has a simple string argument" + response = self.client.post(reverse('arg_view', args=['Slartibartfast'])) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.content, 'Howdy, Slartibartfast') + + def test_argument_with_space_post(self): + "Post for a view that has a string argument that requires escaping" + response = self.client.post(reverse('arg_view', args=['Arthur Dent'])) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.content, 'Hi, Arthur') + + diff --git a/tests/regressiontests/test_client_regress/urls.py b/tests/regressiontests/test_client_regress/urls.py index e771707f61..d3304caef0 100644 --- a/tests/regressiontests/test_client_regress/urls.py +++ b/tests/regressiontests/test_client_regress/urls.py @@ -5,5 +5,6 @@ urlpatterns = patterns('', (r'^no_template_view/$', views.no_template_view), (r'^file_upload/$', views.file_upload_view), (r'^get_view/$', views.get_view), + url(r'^arg_view/(?P.+)/$', views.view_with_argument, name='arg_view'), (r'^login_protected_redirect_view/$', views.login_protected_redirect_view) ) diff --git a/tests/regressiontests/test_client_regress/views.py b/tests/regressiontests/test_client_regress/views.py index 75efd212e1..f44757dc10 100644 --- a/tests/regressiontests/test_client_regress/views.py +++ b/tests/regressiontests/test_client_regress/views.py @@ -1,7 +1,5 @@ from django.contrib.auth.decorators import login_required -from django.core.mail import EmailMessage, SMTPConnection from django.http import HttpResponse, HttpResponseRedirect, HttpResponseServerError -from django.shortcuts import render_to_response def no_template_view(request): "A simple view that expects a GET request, and returns a rendered template" @@ -20,10 +18,22 @@ def file_upload_view(request): return HttpResponseServerError() def get_view(request): - "A simple login protected view" + "A simple login protected view" return HttpResponse("Hello world") get_view = login_required(get_view) +def view_with_argument(request, name): + """A view that takes a string argument + + The purpose of this view is to check that if a space is provided in + the argument, the test framework unescapes the %20 before passing + the value to the view. + """ + if name == 'Arthur Dent': + return HttpResponse('Hi, Arthur') + else: + return HttpResponse('Howdy, %s' % name) + def login_protected_redirect_view(request): "A view that redirects all requests to the GET view" return HttpResponseRedirect('/test_client_regress/get_view/')