Refs #24121 -- Added meaningful repr() to HttpResponse and subclasses.
This commit is contained in:
parent
b356dc4e07
commit
c96f11257b
|
@ -285,6 +285,13 @@ class HttpResponse(HttpResponseBase):
|
||||||
# Content is a bytestring. See the `content` property methods.
|
# Content is a bytestring. See the `content` property methods.
|
||||||
self.content = content
|
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):
|
def serialize(self):
|
||||||
"""Full HTTP message, including headers, as a bytestring."""
|
"""Full HTTP message, including headers, as a bytestring."""
|
||||||
return self.serialize_headers() + b'\r\n\r\n' + self.content
|
return self.serialize_headers() + b'\r\n\r\n' + self.content
|
||||||
|
@ -403,6 +410,14 @@ class HttpResponseRedirectBase(HttpResponse):
|
||||||
|
|
||||||
url = property(lambda self: self['Location'])
|
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):
|
class HttpResponseRedirect(HttpResponseRedirectBase):
|
||||||
status_code = 302
|
status_code = 302
|
||||||
|
@ -445,6 +460,14 @@ class HttpResponseNotAllowed(HttpResponse):
|
||||||
super(HttpResponseNotAllowed, self).__init__(*args, **kwargs)
|
super(HttpResponseNotAllowed, self).__init__(*args, **kwargs)
|
||||||
self['Allow'] = ', '.join(permitted_methods)
|
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):
|
class HttpResponseGone(HttpResponse):
|
||||||
status_code = 410
|
status_code = 410
|
||||||
|
|
|
@ -443,6 +443,11 @@ class HttpResponseSubclassesTests(SimpleTestCase):
|
||||||
r = HttpResponseRedirect(lazystr('/redirected/'))
|
r = HttpResponseRedirect(lazystr('/redirected/'))
|
||||||
self.assertEqual(r.url, '/redirected/')
|
self.assertEqual(r.url, '/redirected/')
|
||||||
|
|
||||||
|
def test_redirect_repr(self):
|
||||||
|
response = HttpResponseRedirect('/redirected/')
|
||||||
|
expected = '<HttpResponseRedirect status_code=302, "text/html; charset=utf-8", url="/redirected/">'
|
||||||
|
self.assertEqual(repr(response), expected)
|
||||||
|
|
||||||
def test_not_modified(self):
|
def test_not_modified(self):
|
||||||
response = HttpResponseNotModified()
|
response = HttpResponseNotModified()
|
||||||
self.assertEqual(response.status_code, 304)
|
self.assertEqual(response.status_code, 304)
|
||||||
|
@ -460,6 +465,11 @@ class HttpResponseSubclassesTests(SimpleTestCase):
|
||||||
content_type='text/html')
|
content_type='text/html')
|
||||||
self.assertContains(response, 'Only the GET method is allowed', status_code=405)
|
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 = '<HttpResponseNotAllowed [GET, OPTIONS] status_code=405, "text/plain">'
|
||||||
|
self.assertEqual(repr(response), expected)
|
||||||
|
|
||||||
|
|
||||||
class JsonResponseTests(SimpleTestCase):
|
class JsonResponseTests(SimpleTestCase):
|
||||||
def test_json_response_non_ascii(self):
|
def test_json_response_non_ascii(self):
|
||||||
|
|
|
@ -107,3 +107,8 @@ class HttpResponseTests(SimpleTestCase):
|
||||||
|
|
||||||
response = HttpResponse(iso_content, content_type='text/plain')
|
response = HttpResponse(iso_content, content_type='text/plain')
|
||||||
self.assertContains(response, iso_content)
|
self.assertContains(response, iso_content)
|
||||||
|
|
||||||
|
def test_repr(self):
|
||||||
|
response = HttpResponse(content="Café :)".encode(UTF8), status=201)
|
||||||
|
expected = '<HttpResponse status_code=201, "text/html; charset=utf-8">'
|
||||||
|
self.assertEqual(repr(response), expected)
|
||||||
|
|
Loading…
Reference in New Issue