From f2303b6f7aa744f8edf4ecf28b7c88db222386f8 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Sat, 21 Jul 2007 05:17:20 +0000 Subject: [PATCH] Fixed #4402 -- Modified test client to allow multi-valued inputs on GET requests. Thanks for the suggestion, eddymul@gmail.com. git-svn-id: http://code.djangoproject.com/svn/django/trunk@5741 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/test/client.py | 2 +- django/utils/http.py | 7 +++++-- tests/modeltests/test_client/models.py | 12 ++++++++++++ tests/modeltests/test_client/views.py | 3 +-- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/django/test/client.py b/django/test/client.py index 0f84097317..7e62715205 100644 --- a/django/test/client.py +++ b/django/test/client.py @@ -195,7 +195,7 @@ class Client: 'CONTENT_LENGTH': None, 'CONTENT_TYPE': 'text/html; charset=utf-8', 'PATH_INFO': path, - 'QUERY_STRING': urlencode(data), + 'QUERY_STRING': urlencode(data, doseq=True), 'REQUEST_METHOD': 'GET', } r.update(extra) diff --git a/django/utils/http.py b/django/utils/http.py index 5a0c18d1c0..4c3b6af868 100644 --- a/django/utils/http.py +++ b/django/utils/http.py @@ -30,6 +30,9 @@ def urlencode(query, doseq=0): """ if hasattr(query, 'items'): query = query.items() - return urllib.urlencode([(smart_str(k), smart_str(v)) for k, - v in query], doseq) + return urllib.urlencode( + [(smart_str(k), + isinstance(v, (list,tuple)) and [smart_str(i) for i in v] or smart_str(v)) + for k, v in query], + doseq) diff --git a/tests/modeltests/test_client/models.py b/tests/modeltests/test_client/models.py index 91c5d8c72f..951a41d61c 100644 --- a/tests/modeltests/test_client/models.py +++ b/tests/modeltests/test_client/models.py @@ -122,6 +122,18 @@ class ClientTest(TestCase): self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "Valid POST Template") + def test_valid_form_with_hints(self): + "GET a form, providing hints in the GET data" + hints = { + 'text': 'Hello World', + 'multi': ('b','c','e') + } + response = self.client.get('/test_client/form_view/', data=hints) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, "Form GET Template") + # Check that the multi-value data has been rolled out ok + self.assertContains(response, 'Select a valid choice.', 0) + def test_incomplete_data_form(self): "POST incomplete data to a form" post_data = { diff --git a/tests/modeltests/test_client/views.py b/tests/modeltests/test_client/views.py index 21f577d758..81b4a2f283 100644 --- a/tests/modeltests/test_client/views.py +++ b/tests/modeltests/test_client/views.py @@ -84,7 +84,7 @@ def form_view(request): t = Template('Invalid POST data. {{ form.errors }}', name='Invalid POST Template') c = Context({'form': form}) else: - form = TestForm() + form = TestForm(request.GET) t = Template('Viewing base form. {{ form }}.', name='Form GET Template') c = Context({'form': form}) @@ -107,7 +107,6 @@ def form_view_with_template(request): 'message': message } ) - def login_protected_view(request): "A simple view that is login protected."