diff options
| author | Berker Peksag <berker.peksag@gmail.com> | 2014-11-20 11:28:16 +0200 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2014-11-20 08:45:11 -0500 |
| commit | 788fa9fffa0105d391b8fe35f2894bc5b4e29066 (patch) | |
| tree | 63df8a6ab2c70c2a8b67ab3e202010ed5e3a3bdb | |
| parent | d6e2bbe734fe65f1cec825b9c507ad7bbb09f2f0 (diff) | |
Fixed #12098 -- Simplified HttpRequest.__repr__().
| -rw-r--r-- | django/http/request.py | 6 | ||||
| -rw-r--r-- | docs/releases/1.8.txt | 4 | ||||
| -rw-r--r-- | tests/requests/tests.py | 23 |
3 files changed, 27 insertions, 6 deletions
diff --git a/django/http/request.py b/django/http/request.py index 486eeb5410..0ef90a1954 100644 --- a/django/http/request.py +++ b/django/http/request.py @@ -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.""" diff --git a/docs/releases/1.8.txt b/docs/releases/1.8.txt index cbd63495c5..97317b8298 100644 --- a/docs/releases/1.8.txt +++ b/docs/releases/1.8.txt @@ -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 diff --git a/tests/requests/tests.py b/tests/requests/tests.py index 2065ba7e6d..2f3bace5d3 100644 --- a/tests/requests/tests.py +++ b/tests/requests/tests.py @@ -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'}>")) |
