Ticket #4968: 4968.diff

File 4968.diff, 5.0 KB (added by Ivan Sagalaev <Maniac@…>, 17 years ago)

Patch

  • django/test/testcases.py

     
    11import re, unittest
    2 from urlparse import urlparse
     2from urlparse import urlsplit
     3from django.http import QueryDict
    34from django.db import transaction
    45from django.core import management, mail
    56from django.db.models import get_apps
     
    5758        self._pre_setup()
    5859        super(TestCase, self).__call__(result)
    5960
    60     def assertRedirects(self, response, expected_path, status_code=302, target_status_code=200):
     61    def assertRedirects(self, response, expected_url, status_code=302, target_status_code=200):
    6162        """Assert that a response redirected to a specific URL, and that the
    6263        redirect URL can be loaded.
    6364       
     65        Note that assertRedirects won't work for external links since it uses
     66        TestClient to do a request.
    6467        """
    6568        self.assertEqual(response.status_code, status_code,
    6669            "Response didn't redirect as expected: Response code was %d (expected %d)" %
    6770                (response.status_code, status_code))
    68         scheme, netloc, path, params, query, fragment = urlparse(response['Location'])
    69         self.assertEqual(path, expected_path,
    70             "Response redirected to '%s', expected '%s'" % (path, expected_path))
    71         redirect_response = self.client.get(path)
     71        scheme, netloc, path, query, fragment = urlsplit(response['Location'])
     72        url = path
     73        if query:
     74            url += '?' + query
     75        if fragment:
     76            url += '#' + fragment
     77        self.assertEqual(url, expected_url,
     78            "Response redirected to '%s', expected '%s'" % (url, expected_url))
     79       
     80        redirect_response = self.client.get(path, QueryDict(query))
    7281        self.assertEqual(redirect_response.status_code, target_status_code,
    7382            "Couldn't retrieve redirection page '%s': response code was %d (expected %d)" %
    7483                (path, redirect_response.status_code, target_status_code))
  • tests/modeltests/test_client/views.py

     
    4848
    4949def redirect_view(request):
    5050    "A view that redirects all requests to the GET view"
    51     return HttpResponseRedirect('/test_client/get_view/')
     51    if request.GET:
     52        from urllib import urlencode
     53        query = '?' + urlencode(request.GET, True)
     54    else:
     55        query = ''
     56    return HttpResponseRedirect('/test_client/get_view/' + query)
    5257
    5358def double_redirect_view(request):
    5459    "A view that redirects all requests to a redirection view"
  • tests/modeltests/test_client/models.py

     
    8686       
    8787        # Check that the response was a 302 (redirect)
    8888        self.assertRedirects(response, '/test_client/get_view/')
     89   
     90    def test_redirect_with_query(self):
     91        "GET a URL that redirects with given GET parameters"
     92        response = self.client.get('/test_client/redirect_view/', {'var': 'value'})
     93       
     94        # Check if parameters are intact
     95        self.assertRedirects(response, '/test_client/get_view/?var=value')
    8996
    9097    def test_permanent_redirect(self):
    9198        "GET a URL that redirects permanently elsewhere"
     
    224231       
    225232        # Get the page without logging in. Should result in 302.
    226233        response = self.client.get('/test_client/login_protected_view/')
    227         self.assertRedirects(response, '/accounts/login/')
     234        self.assertRedirects(response, '/accounts/login/?next=/test_client/login_protected_view/')
    228235       
    229236        # Log in
    230237        self.client.login(username='testclient', password='password')
  • tests/regressiontests/test_client_regress/models.py

     
    112112            self.assertRedirects(response, '/test_client/get_view/')
    113113        except AssertionError, e:
    114114            self.assertEquals(str(e), "Response didn't redirect as expected: Response code was 301 (expected 302)")
     115   
     116    def test_lost_query(self):
     117        "An assertion is raised if the redirect location doesn't preserve GET parameters"
     118        response = self.client.get('/test_client/redirect_view/', {'var': 'value'})
     119        try:
     120            self.assertRedirects(response, '/test_client/get_view/')
     121        except AssertionError, e:
     122            self.assertEquals(str(e), "Response redirected to '/test_client/get_view/?var=value', expected '/test_client/get_view/'")
    115123
    116124    def test_incorrect_target(self):
    117125        "An assertion is raised if the response redirects to another target"
Back to Top