Fixed #28720 -- Added HttpRequest.get_full_path_info().

This commit is contained in:
Jonas Haag 2017-11-07 21:58:05 +01:00 committed by Tim Graham
parent 3e7497a05e
commit a2851f204c
4 changed files with 24 additions and 5 deletions

View File

@ -113,11 +113,17 @@ class HttpRequest:
return str(port)
def get_full_path(self, force_append_slash=False):
return self._get_full_path(self.path, force_append_slash)
def get_full_path_info(self, force_append_slash=False):
return self._get_full_path(self.path_info, force_append_slash)
def _get_full_path(self, path, force_append_slash):
# RFC 3986 requires query string arguments to be in the ASCII range.
# Rather than crash if this doesn't happen, we encode defensively.
return '%s%s%s' % (
escape_uri_path(self.path),
'/' if force_append_slash and not self.path.endswith('/') else '',
escape_uri_path(path),
'/' if force_append_slash and not path.endswith('/') else '',
('?' + iri_to_uri(self.META.get('QUERY_STRING', ''))) if self.META.get('QUERY_STRING', '') else ''
)

View File

@ -284,6 +284,15 @@ Methods
Example: ``"/music/bands/the_beatles/?print=true"``
.. method:: HttpRequest.get_full_path_info()
.. versionadded:: 2.1
Like :meth:`get_full_path`, but uses :attr:`path_info` instead of
:attr:`path`.
Example: ``"/minfo/music/bands/the_beatles/?print=true"``
.. method:: HttpRequest.build_absolute_uri(location)
Returns the absolute URI form of ``location``. If no location is provided,

View File

@ -159,7 +159,7 @@ Models
Requests and Responses
~~~~~~~~~~~~~~~~~~~~~~
* ...
* Added :meth:`.HttpRequest.get_full_path_info`.
Serialization
~~~~~~~~~~~~~

View File

@ -38,16 +38,20 @@ class RequestsTests(SimpleTestCase):
def test_httprequest_full_path(self):
request = HttpRequest()
request.path = request.path_info = '/;some/?awful/=path/foo:bar/'
request.path = '/;some/?awful/=path/foo:bar/'
request.path_info = '/prefix' + request.path
request.META['QUERY_STRING'] = ';some=query&+query=string'
expected = '/%3Bsome/%3Fawful/%3Dpath/foo:bar/?;some=query&+query=string'
self.assertEqual(request.get_full_path(), expected)
self.assertEqual(request.get_full_path_info(), '/prefix' + expected)
def test_httprequest_full_path_with_query_string_and_fragment(self):
request = HttpRequest()
request.path = request.path_info = '/foo#bar'
request.path = '/foo#bar'
request.path_info = '/prefix' + request.path
request.META['QUERY_STRING'] = 'baz#quux'
self.assertEqual(request.get_full_path(), '/foo%23bar?baz#quux')
self.assertEqual(request.get_full_path_info(), '/prefix/foo%23bar?baz#quux')
def test_httprequest_repr(self):
request = HttpRequest()