From 788fa9fffa0105d391b8fe35f2894bc5b4e29066 Mon Sep 17 00:00:00 2001 From: Berker Peksag Date: Thu, 20 Nov 2014 11:28:16 +0200 Subject: [PATCH] Fixed #12098 -- Simplified HttpRequest.__repr__(). --- django/http/request.py | 6 +++++- docs/releases/1.8.txt | 4 ++++ 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 486eeb54104..0ef90a19547 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 cbd63495c5e..97317b8298e 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. + ````). 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 2065ba7e6d8..2f3bace5d35 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("")) - self.assertEqual(build_request_repr(request), repr(request)) + self.assertEqual(repr(request), str_prefix("")) + self.assertEqual(build_request_repr(request), str_prefix("")) 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("")) + def test_httprequest_repr_invalid_method_and_path(self): + request = HttpRequest() + self.assertEqual(repr(request), str_prefix("")) + request = HttpRequest() + request.method = "GET" + self.assertEqual(repr(request), str_prefix("")) + request = HttpRequest() + request.path = "" + self.assertEqual(repr(request), str_prefix("")) + 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:' % attr, repr(request)) + self.assertIn('%s:' % 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("")) 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("")) - self.assertEqual(build_request_repr(request), repr(request)) + self.assertEqual(repr(request), str_prefix("")) + self.assertEqual(build_request_repr(request), str_prefix("")) 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(""))