Commit 788fa9ff authored by Berker Peksag's avatar Berker Peksag Committed by Tim Graham
Browse files

Fixed #12098 -- Simplified HttpRequest.__repr__().

parent d6e2bbe7
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -64,7 +64,11 @@ class HttpRequest(object):
        self._post_parse_error = False

    def __repr__(self):
        return build_request_repr(self)
        if self.method is None or not self.get_full_path():
            return force_str('<%s>' % self.__class__.__name__)
        return force_str(
            '<%s: %s %r>' % (self.__class__.__name__, self.method, force_str(self.get_full_path()))
        )

    def get_host(self):
        """Returns the HTTP host using the environment or request headers."""
+4 −0
Original line number Diff line number Diff line
@@ -777,6 +777,10 @@ Miscellaneous
* Warnings from the MySQL database backend are no longer converted to
  exceptions when :setting:`DEBUG` is ``True``.

* :class:`~django.http.HttpRequest` now has a simplified ``repr`` (e.g.
  ``<WSGIRequest: GET '/somepath/'>``). This won't change the behavior of
  the :class:`~django.views.debug.SafeExceptionReporterFilter` class.

.. _deprecated-features-1.8:

Features deprecated in 1.8
+18 −5
Original line number Diff line number Diff line
@@ -51,15 +51,26 @@ class RequestsTests(SimpleTestCase):
    def test_httprequest_repr(self):
        request = HttpRequest()
        request.path = '/somepath/'
        request.method = 'GET'
        request.GET = {'get-key': 'get-value'}
        request.POST = {'post-key': 'post-value'}
        request.COOKIES = {'post-key': 'post-value'}
        request.META = {'post-key': 'post-value'}
        self.assertEqual(repr(request), str_prefix("<HttpRequest\npath:/somepath/,\nGET:{%(_)s'get-key': %(_)s'get-value'},\nPOST:{%(_)s'post-key': %(_)s'post-value'},\nCOOKIES:{%(_)s'post-key': %(_)s'post-value'},\nMETA:{%(_)s'post-key': %(_)s'post-value'}>"))
        self.assertEqual(build_request_repr(request), repr(request))
        self.assertEqual(repr(request), str_prefix("<HttpRequest: GET '/somepath/'>"))
        self.assertEqual(build_request_repr(request), str_prefix("<HttpRequest\npath:/somepath/,\nGET:{%(_)s'get-key': %(_)s'get-value'},\nPOST:{%(_)s'post-key': %(_)s'post-value'},\nCOOKIES:{%(_)s'post-key': %(_)s'post-value'},\nMETA:{%(_)s'post-key': %(_)s'post-value'}>"))
        self.assertEqual(build_request_repr(request, path_override='/otherpath/', GET_override={'a': 'b'}, POST_override={'c': 'd'}, COOKIES_override={'e': 'f'}, META_override={'g': 'h'}),
                         str_prefix("<HttpRequest\npath:/otherpath/,\nGET:{%(_)s'a': %(_)s'b'},\nPOST:{%(_)s'c': %(_)s'd'},\nCOOKIES:{%(_)s'e': %(_)s'f'},\nMETA:{%(_)s'g': %(_)s'h'}>"))

    def test_httprequest_repr_invalid_method_and_path(self):
        request = HttpRequest()
        self.assertEqual(repr(request), str_prefix("<HttpRequest>"))
        request = HttpRequest()
        request.method = "GET"
        self.assertEqual(repr(request), str_prefix("<HttpRequest>"))
        request = HttpRequest()
        request.path = ""
        self.assertEqual(repr(request), str_prefix("<HttpRequest>"))

    def test_bad_httprequest_repr(self):
        """
        If an exception occurs when parsing GET, POST, COOKIES, or META, the
@@ -74,7 +85,7 @@ class RequestsTests(SimpleTestCase):
        for attr in ['GET', 'POST', 'COOKIES', 'META']:
            request = HttpRequest()
            setattr(request, attr, {'bomb': bomb})
            self.assertIn('%s:<could not parse>' % attr, repr(request))
            self.assertIn('%s:<could not parse>' % attr, build_request_repr(request))

    def test_wsgirequest(self):
        request = WSGIRequest({'PATH_INFO': 'bogus', 'REQUEST_METHOD': 'bogus', 'wsgi.input': BytesIO(b'')})
@@ -125,13 +136,15 @@ class RequestsTests(SimpleTestCase):
            self.assertEqual(request.path, '/FORCED_PREFIX/somepath/')

    def test_wsgirequest_repr(self):
        request = WSGIRequest({'REQUEST_METHOD': 'get', 'wsgi.input': BytesIO(b'')})
        self.assertEqual(repr(request), str_prefix("<WSGIRequest: GET '/'>"))
        request = WSGIRequest({'PATH_INFO': '/somepath/', 'REQUEST_METHOD': 'get', 'wsgi.input': BytesIO(b'')})
        request.GET = {'get-key': 'get-value'}
        request.POST = {'post-key': 'post-value'}
        request.COOKIES = {'post-key': 'post-value'}
        request.META = {'post-key': 'post-value'}
        self.assertEqual(repr(request), str_prefix("<WSGIRequest\npath:/somepath/,\nGET:{%(_)s'get-key': %(_)s'get-value'},\nPOST:{%(_)s'post-key': %(_)s'post-value'},\nCOOKIES:{%(_)s'post-key': %(_)s'post-value'},\nMETA:{%(_)s'post-key': %(_)s'post-value'}>"))
        self.assertEqual(build_request_repr(request), repr(request))
        self.assertEqual(repr(request), str_prefix("<WSGIRequest: GET '/somepath/'>"))
        self.assertEqual(build_request_repr(request), str_prefix("<WSGIRequest\npath:/somepath/,\nGET:{%(_)s'get-key': %(_)s'get-value'},\nPOST:{%(_)s'post-key': %(_)s'post-value'},\nCOOKIES:{%(_)s'post-key': %(_)s'post-value'},\nMETA:{%(_)s'post-key': %(_)s'post-value'}>"))
        self.assertEqual(build_request_repr(request, path_override='/otherpath/', GET_override={'a': 'b'}, POST_override={'c': 'd'}, COOKIES_override={'e': 'f'}, META_override={'g': 'h'}),
                         str_prefix("<WSGIRequest\npath:/otherpath/,\nGET:{%(_)s'a': %(_)s'b'},\nPOST:{%(_)s'c': %(_)s'd'},\nCOOKIES:{%(_)s'e': %(_)s'f'},\nMETA:{%(_)s'g': %(_)s'h'}>"))