From c96f11257baf43188ff9daeddab3221992925c85 Mon Sep 17 00:00:00 2001 From: Keryn Knight Date: Mon, 13 Jul 2015 15:32:09 +0100 Subject: [PATCH] Refs #24121 -- Added meaningful repr() to HttpResponse and subclasses. --- django/http/response.py | 23 +++++++++++++++++++++++ tests/httpwrappers/tests.py | 10 ++++++++++ tests/responses/tests.py | 5 +++++ 3 files changed, 38 insertions(+) diff --git a/django/http/response.py b/django/http/response.py index c8d693085f1..aece5ec5a69 100644 --- a/django/http/response.py +++ b/django/http/response.py @@ -285,6 +285,13 @@ class HttpResponse(HttpResponseBase): # Content is a bytestring. See the `content` property methods. self.content = content + def __repr__(self): + return '<%(cls)s status_code=%(status_code)d, "%(content_type)s">' % { + 'cls': self.__class__.__name__, + 'status_code': self.status_code, + 'content_type': self['Content-Type'], + } + def serialize(self): """Full HTTP message, including headers, as a bytestring.""" return self.serialize_headers() + b'\r\n\r\n' + self.content @@ -403,6 +410,14 @@ class HttpResponseRedirectBase(HttpResponse): url = property(lambda self: self['Location']) + def __repr__(self): + return '<%(cls)s status_code=%(status_code)d, "%(content_type)s", url="%(url)s">' % { + 'cls': self.__class__.__name__, + 'status_code': self.status_code, + 'content_type': self['Content-Type'], + 'url': self.url, + } + class HttpResponseRedirect(HttpResponseRedirectBase): status_code = 302 @@ -445,6 +460,14 @@ class HttpResponseNotAllowed(HttpResponse): super(HttpResponseNotAllowed, self).__init__(*args, **kwargs) self['Allow'] = ', '.join(permitted_methods) + def __repr__(self): + return '<%(cls)s [%(methods)s] status_code=%(status_code)d, "%(content_type)s">' % { + 'cls': self.__class__.__name__, + 'status_code': self.status_code, + 'content_type': self['Content-Type'], + 'methods': self['Allow'], + } + class HttpResponseGone(HttpResponse): status_code = 410 diff --git a/tests/httpwrappers/tests.py b/tests/httpwrappers/tests.py index 84d65ad8017..74a8f923cf1 100644 --- a/tests/httpwrappers/tests.py +++ b/tests/httpwrappers/tests.py @@ -443,6 +443,11 @@ class HttpResponseSubclassesTests(SimpleTestCase): r = HttpResponseRedirect(lazystr('/redirected/')) self.assertEqual(r.url, '/redirected/') + def test_redirect_repr(self): + response = HttpResponseRedirect('/redirected/') + expected = '' + self.assertEqual(repr(response), expected) + def test_not_modified(self): response = HttpResponseNotModified() self.assertEqual(response.status_code, 304) @@ -460,6 +465,11 @@ class HttpResponseSubclassesTests(SimpleTestCase): content_type='text/html') self.assertContains(response, 'Only the GET method is allowed', status_code=405) + def test_not_allowed_repr(self): + response = HttpResponseNotAllowed(['GET', 'OPTIONS'], content_type='text/plain') + expected = '' + self.assertEqual(repr(response), expected) + class JsonResponseTests(SimpleTestCase): def test_json_response_non_ascii(self): diff --git a/tests/responses/tests.py b/tests/responses/tests.py index 91ec710c721..a34228ffa7c 100644 --- a/tests/responses/tests.py +++ b/tests/responses/tests.py @@ -107,3 +107,8 @@ class HttpResponseTests(SimpleTestCase): response = HttpResponse(iso_content, content_type='text/plain') self.assertContains(response, iso_content) + + def test_repr(self): + response = HttpResponse(content="Café :)".encode(UTF8), status=201) + expected = '' + self.assertEqual(repr(response), expected)