Code

Ticket #12011: test_client_redirect_scheme.diff

File test_client_redirect_scheme.diff, 3.5 KB (added by tgardner, 5 years ago)

Patch to the test client to consider Location scheme

Line 
1Index: django/test/client.py
2===================================================================
3--- django/test/client.py       (revision 11616)
4+++ django/test/client.py       (working copy)
5@@ -459,11 +459,15 @@
6             redirect_chain = response.redirect_chain
7             redirect_chain.append((url, response.status_code))
8 
9+            extra = {}
10+            if scheme:
11+                extra['wsgi.url_scheme'] = scheme
12+
13             # The test client doesn't handle external links,
14             # but since the situation is simulated in test_client,
15             # we fake things here by ignoring the netloc portion of the
16             # redirected URL.
17-            response = self.get(path, QueryDict(query), follow=False)
18+            response = self.get(path, QueryDict(query), follow=False, **extra)
19             response.redirect_chain = redirect_chain
20 
21             # Prevent loops
22Index: tests/modeltests/test_client/views.py
23===================================================================
24--- tests/modeltests/test_client/views.py       (revision 11616)
25+++ tests/modeltests/test_client/views.py       (working copy)
26@@ -62,6 +62,12 @@
27         query = ''
28     return HttpResponseRedirect('/test_client/get_view/' + query)
29 
30+def view_with_secure(request):
31+    "A view that indicates if the request was secure"
32+    response = HttpResponse()
33+    response.test_was_secure_request = request.is_secure()
34+    return response
35+
36 def double_redirect_view(request):
37     "A view that redirects all requests to a redirection view"
38     return HttpResponseRedirect('/test_client/permanent_redirect_view/')
39Index: tests/modeltests/test_client/models.py
40===================================================================
41--- tests/modeltests/test_client/models.py      (revision 11616)
42+++ tests/modeltests/test_client/models.py      (working copy)
43@@ -138,6 +138,16 @@
44         self.assertRedirects(response, 'http://testserver/test_client/get_view/', status_code=302, target_status_code=200)
45         self.assertEquals(len(response.redirect_chain), 2)
46 
47+    def test_redirect_http(self):
48+        "GET a URL that redirects to an http URI"
49+        response = self.client.get('/test_client/http_redirect_view/',follow=True)
50+        self.assert_(not response.test_was_secure_request)
51+
52+    def test_redirect_https(self):
53+        "GET a URL that redirects to an https URI"
54+        response = self.client.get('/test_client/https_redirect_view/',follow=True)
55+        self.assert_(response.test_was_secure_request)
56+
57     def test_notfound_response(self):
58         "GET a URL that responds as '404:Not Found'"
59         response = self.client.get('/test_client/bad_view/')
60Index: tests/modeltests/test_client/urls.py
61===================================================================
62--- tests/modeltests/test_client/urls.py        (revision 11616)
63+++ tests/modeltests/test_client/urls.py        (working copy)
64@@ -8,8 +8,11 @@
65     (r'^header_view/$', views.view_with_header),
66     (r'^raw_post_view/$', views.raw_post_view),
67     (r'^redirect_view/$', views.redirect_view),
68+    (r'^secure_view/$', views.view_with_secure),
69     (r'^permanent_redirect_view/$', redirect_to, {'url': '/test_client/get_view/'}),
70     (r'^temporary_redirect_view/$', redirect_to, {'url': '/test_client/get_view/', 'permanent': False}),
71+    (r'^http_redirect_view/$', redirect_to, {'url': '/test_client/secure_view/'}),
72+    (r'^https_redirect_view/$', redirect_to, {'url': 'https://testserver/test_client/secure_view/'}),
73     (r'^double_redirect_view/$', views.double_redirect_view),
74     (r'^bad_view/$', views.bad_view),
75     (r'^form_view/$', views.form_view),