Ticket #12011: test_client_redirect_scheme.diff

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

Patch to the test client to consider Location scheme

  • django/test/client.py

     
    459459            redirect_chain = response.redirect_chain
    460460            redirect_chain.append((url, response.status_code))
    461461
     462            extra = {}
     463            if scheme:
     464                extra['wsgi.url_scheme'] = scheme
     465
    462466            # The test client doesn't handle external links,
    463467            # but since the situation is simulated in test_client,
    464468            # we fake things here by ignoring the netloc portion of the
    465469            # redirected URL.
    466             response = self.get(path, QueryDict(query), follow=False)
     470            response = self.get(path, QueryDict(query), follow=False, **extra)
    467471            response.redirect_chain = redirect_chain
    468472
    469473            # Prevent loops
  • tests/modeltests/test_client/views.py

     
    6262        query = ''
    6363    return HttpResponseRedirect('/test_client/get_view/' + query)
    6464
     65def view_with_secure(request):
     66    "A view that indicates if the request was secure"
     67    response = HttpResponse()
     68    response.test_was_secure_request = request.is_secure()
     69    return response
     70
    6571def double_redirect_view(request):
    6672    "A view that redirects all requests to a redirection view"
    6773    return HttpResponseRedirect('/test_client/permanent_redirect_view/')
  • tests/modeltests/test_client/models.py

     
    138138        self.assertRedirects(response, 'http://testserver/test_client/get_view/', status_code=302, target_status_code=200)
    139139        self.assertEquals(len(response.redirect_chain), 2)
    140140
     141    def test_redirect_http(self):
     142        "GET a URL that redirects to an http URI"
     143        response = self.client.get('/test_client/http_redirect_view/',follow=True)
     144        self.assert_(not response.test_was_secure_request)
     145
     146    def test_redirect_https(self):
     147        "GET a URL that redirects to an https URI"
     148        response = self.client.get('/test_client/https_redirect_view/',follow=True)
     149        self.assert_(response.test_was_secure_request)
     150
    141151    def test_notfound_response(self):
    142152        "GET a URL that responds as '404:Not Found'"
    143153        response = self.client.get('/test_client/bad_view/')
  • tests/modeltests/test_client/urls.py

     
    88    (r'^header_view/$', views.view_with_header),
    99    (r'^raw_post_view/$', views.raw_post_view),
    1010    (r'^redirect_view/$', views.redirect_view),
     11    (r'^secure_view/$', views.view_with_secure),
    1112    (r'^permanent_redirect_view/$', redirect_to, {'url': '/test_client/get_view/'}),
    1213    (r'^temporary_redirect_view/$', redirect_to, {'url': '/test_client/get_view/', 'permanent': False}),
     14    (r'^http_redirect_view/$', redirect_to, {'url': '/test_client/secure_view/'}),
     15    (r'^https_redirect_view/$', redirect_to, {'url': 'https://testserver/test_client/secure_view/'}),
    1316    (r'^double_redirect_view/$', views.double_redirect_view),
    1417    (r'^bad_view/$', views.bad_view),
    1518    (r'^form_view/$', views.form_view),
Back to Top