Ticket #26428: 26428-extra.diff

File 26428-extra.diff, 2.4 KB (added by Ian Tyrrell, 4 years ago)

Also fixes Client.get()

  • django/test/client.py

    diff --git a/django/test/client.py b/django/test/client.py
    index 166e6ef..843c01c 100644
    a b from django.utils.encoding import force_bytes, force_str, uri_to_iri 
    2626from django.utils.functional import SimpleLazyObject, curry
    2727from django.utils.http import urlencode
    2828from django.utils.itercompat import is_iterable
    29 from django.utils.six.moves.urllib.parse import urlparse, urlsplit
     29from django.utils.six.moves.urllib.parse import urlparse, urlsplit, urljoin
    3030
    3131__all__ = ('Client', 'RedirectCycleError', 'RequestFactory', 'encode_file', 'encode_multipart')
    3232
    class Client(RequestFactory): 
    699699            if url.port:
    700700                extra['SERVER_PORT'] = str(url.port)
    701701
    702             response = self.get(url.path, QueryDict(url.query), follow=False, **extra)
     702            # Prepend the request path to handle relative path redirects
     703            path = url.path
     704            if not path.startswith('/'):
     705                path = urljoin(response.request['PATH_INFO'], path)
     706
     707            response = self.get(path, QueryDict(url.query), follow=False, **extra)
    703708            response.redirect_chain = redirect_chain
    704709
    705710            if redirect_chain[-1] in redirect_chain[:-1]:
  • tests/test_client/tests.py

    diff --git a/tests/test_client/tests.py b/tests/test_client/tests.py
    index 5c37356..dff7e39 100644
    a b class ClientTest(TestCase): 
    198198        self.assertRedirects(response, '/get_view/', status_code=302, target_status_code=200)
    199199        self.assertEqual(len(response.redirect_chain), 2)
    200200
     201    def test_follow_relative_redirect(self):
     202        "A URL with a relative redirect can be followed."
     203        response = self.client.get('/accounts/', follow=True)
     204        self.assertEqual(response.status_code, 200)
     205        self.assertEqual(response.request['PATH_INFO'], '/accounts/login/')
     206
     207    def test_follow_relative_redirect_no_trailing_slash(self):
     208        "A URL with a relative redirect with no trailing slash can be followed."
     209        response = self.client.get('/accounts/no_trailing_slash', follow=True)
     210        self.assertEqual(response.status_code, 200)
     211        self.assertEqual(response.request['PATH_INFO'], '/accounts/login/')
     212
    201213    def test_redirect_http(self):
    202214        "GET a URL that redirects to an http URI"
    203215        response = self.client.get('/http_redirect_view/', follow=True)
Back to Top