2015-10-21 05:23:58 +08:00
|
|
|
import io
|
|
|
|
|
2013-11-17 01:54:12 +08:00
|
|
|
from django.conf import settings
|
2015-11-12 03:17:32 +08:00
|
|
|
from django.core.cache import cache
|
2013-05-19 18:58:13 +08:00
|
|
|
from django.http import HttpResponse
|
2014-04-14 23:58:59 +08:00
|
|
|
from django.http.response import HttpResponseBase
|
2013-11-17 01:54:12 +08:00
|
|
|
from django.test import SimpleTestCase
|
2013-05-19 18:58:13 +08:00
|
|
|
|
2013-11-17 01:54:12 +08:00
|
|
|
UTF8 = "utf-8"
|
|
|
|
ISO88591 = "iso-8859-1"
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2013-11-17 01:54:12 +08:00
|
|
|
|
2014-04-14 23:58:59 +08:00
|
|
|
class HttpResponseBaseTests(SimpleTestCase):
|
|
|
|
def test_closed(self):
|
|
|
|
r = HttpResponseBase()
|
|
|
|
self.assertIs(r.closed, False)
|
|
|
|
|
|
|
|
r.close()
|
|
|
|
self.assertIs(r.closed, True)
|
|
|
|
|
|
|
|
def test_write(self):
|
|
|
|
r = HttpResponseBase()
|
|
|
|
self.assertIs(r.writable(), False)
|
|
|
|
|
2019-01-28 23:01:35 +08:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
OSError, "This HttpResponseBase instance is not writable"
|
|
|
|
):
|
2014-04-14 23:58:59 +08:00
|
|
|
r.write("asdf")
|
2019-01-28 23:01:35 +08:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
OSError, "This HttpResponseBase instance is not writable"
|
|
|
|
):
|
2014-04-14 23:58:59 +08:00
|
|
|
r.writelines(["asdf\n", "qwer\n"])
|
2013-05-19 18:58:13 +08:00
|
|
|
|
2014-04-14 23:58:59 +08:00
|
|
|
def test_tell(self):
|
|
|
|
r = HttpResponseBase()
|
2019-01-28 23:01:35 +08:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
OSError, "This HttpResponseBase instance cannot tell its position"
|
|
|
|
):
|
2014-04-14 23:58:59 +08:00
|
|
|
r.tell()
|
|
|
|
|
2014-12-11 06:25:05 +08:00
|
|
|
def test_setdefault(self):
|
|
|
|
"""
|
|
|
|
HttpResponseBase.setdefault() should not change an existing header
|
|
|
|
and should be case insensitive.
|
|
|
|
"""
|
|
|
|
r = HttpResponseBase()
|
|
|
|
|
2020-07-14 19:32:24 +08:00
|
|
|
r.headers["Header"] = "Value"
|
2014-12-11 06:25:05 +08:00
|
|
|
r.setdefault("header", "changed")
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(r.headers["header"], "Value")
|
2014-12-11 06:25:05 +08:00
|
|
|
|
|
|
|
r.setdefault("x-header", "DefaultValue")
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(r.headers["X-Header"], "DefaultValue")
|
2014-12-11 06:25:05 +08:00
|
|
|
|
2023-06-11 02:43:05 +08:00
|
|
|
def test_charset_setter(self):
|
|
|
|
r = HttpResponseBase()
|
|
|
|
r.charset = "utf-8"
|
|
|
|
self.assertEqual(r.charset, "utf-8")
|
|
|
|
|
|
|
|
def test_reason_phrase_setter(self):
|
|
|
|
r = HttpResponseBase()
|
|
|
|
r.reason_phrase = "test"
|
|
|
|
self.assertEqual(r.reason_phrase, "test")
|
|
|
|
|
2014-04-14 23:58:59 +08:00
|
|
|
|
|
|
|
class HttpResponseTests(SimpleTestCase):
|
2013-05-19 18:58:13 +08:00
|
|
|
def test_status_code(self):
|
2015-01-14 03:29:07 +08:00
|
|
|
resp = HttpResponse(status=503)
|
|
|
|
self.assertEqual(resp.status_code, 503)
|
|
|
|
self.assertEqual(resp.reason_phrase, "Service Unavailable")
|
2015-01-13 06:45:09 +08:00
|
|
|
|
|
|
|
def test_change_status_code(self):
|
|
|
|
resp = HttpResponse()
|
|
|
|
resp.status_code = 503
|
|
|
|
self.assertEqual(resp.status_code, 503)
|
|
|
|
self.assertEqual(resp.reason_phrase, "Service Unavailable")
|
2013-05-19 18:58:13 +08:00
|
|
|
|
2016-08-28 08:21:37 +08:00
|
|
|
def test_valid_status_code_string(self):
|
|
|
|
resp = HttpResponse(status="100")
|
|
|
|
self.assertEqual(resp.status_code, 100)
|
|
|
|
resp = HttpResponse(status="404")
|
|
|
|
self.assertEqual(resp.status_code, 404)
|
|
|
|
resp = HttpResponse(status="599")
|
|
|
|
self.assertEqual(resp.status_code, 599)
|
|
|
|
|
|
|
|
def test_invalid_status_code(self):
|
|
|
|
must_be_integer = "HTTP status code must be an integer."
|
|
|
|
must_be_integer_in_range = (
|
|
|
|
"HTTP status code must be an integer from 100 to 599."
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2016-08-28 08:21:37 +08:00
|
|
|
with self.assertRaisesMessage(TypeError, must_be_integer):
|
|
|
|
HttpResponse(status=object())
|
|
|
|
with self.assertRaisesMessage(TypeError, must_be_integer):
|
|
|
|
HttpResponse(status="J'attendrai")
|
|
|
|
with self.assertRaisesMessage(ValueError, must_be_integer_in_range):
|
|
|
|
HttpResponse(status=99)
|
|
|
|
with self.assertRaisesMessage(ValueError, must_be_integer_in_range):
|
|
|
|
HttpResponse(status=600)
|
|
|
|
|
2013-05-19 18:58:13 +08:00
|
|
|
def test_reason_phrase(self):
|
|
|
|
reason = "I'm an anarchist coffee pot on crack."
|
2016-08-28 08:21:37 +08:00
|
|
|
resp = HttpResponse(status=419, reason=reason)
|
|
|
|
self.assertEqual(resp.status_code, 419)
|
2013-05-19 18:58:13 +08:00
|
|
|
self.assertEqual(resp.reason_phrase, reason)
|
2013-11-17 01:54:12 +08:00
|
|
|
|
|
|
|
def test_charset_detection(self):
|
|
|
|
"""HttpResponse should parse charset from content_type."""
|
|
|
|
response = HttpResponse("ok")
|
|
|
|
self.assertEqual(response.charset, settings.DEFAULT_CHARSET)
|
|
|
|
|
|
|
|
response = HttpResponse(charset=ISO88591)
|
|
|
|
self.assertEqual(response.charset, ISO88591)
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(
|
|
|
|
response.headers["Content-Type"], "text/html; charset=%s" % ISO88591
|
|
|
|
)
|
2013-11-17 01:54:12 +08:00
|
|
|
|
|
|
|
response = HttpResponse(
|
|
|
|
content_type="text/plain; charset=%s" % UTF8, charset=ISO88591
|
|
|
|
)
|
|
|
|
self.assertEqual(response.charset, ISO88591)
|
|
|
|
|
|
|
|
response = HttpResponse(content_type="text/plain; charset=%s" % ISO88591)
|
|
|
|
self.assertEqual(response.charset, ISO88591)
|
|
|
|
|
|
|
|
response = HttpResponse(content_type='text/plain; charset="%s"' % ISO88591)
|
|
|
|
self.assertEqual(response.charset, ISO88591)
|
|
|
|
|
|
|
|
response = HttpResponse(content_type="text/plain; charset=")
|
|
|
|
self.assertEqual(response.charset, settings.DEFAULT_CHARSET)
|
|
|
|
|
|
|
|
response = HttpResponse(content_type="text/plain")
|
|
|
|
self.assertEqual(response.charset, settings.DEFAULT_CHARSET)
|
|
|
|
|
|
|
|
def test_response_content_charset(self):
|
|
|
|
"""HttpResponse should encode based on charset."""
|
|
|
|
content = "Café :)"
|
|
|
|
utf8_content = content.encode(UTF8)
|
|
|
|
iso_content = content.encode(ISO88591)
|
|
|
|
|
|
|
|
response = HttpResponse(utf8_content)
|
|
|
|
self.assertContains(response, utf8_content)
|
|
|
|
|
|
|
|
response = HttpResponse(
|
|
|
|
iso_content, content_type="text/plain; charset=%s" % ISO88591
|
|
|
|
)
|
|
|
|
self.assertContains(response, iso_content)
|
|
|
|
|
|
|
|
response = HttpResponse(iso_content)
|
|
|
|
self.assertContains(response, iso_content)
|
|
|
|
|
|
|
|
response = HttpResponse(iso_content, content_type="text/plain")
|
|
|
|
self.assertContains(response, iso_content)
|
2015-07-13 22:32:09 +08:00
|
|
|
|
|
|
|
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)
|
2015-10-21 05:23:58 +08:00
|
|
|
|
2016-12-27 12:29:31 +08:00
|
|
|
def test_repr_no_content_type(self):
|
|
|
|
response = HttpResponse(status=204)
|
2020-07-14 19:32:24 +08:00
|
|
|
del response.headers["Content-Type"]
|
2016-12-27 12:29:31 +08:00
|
|
|
self.assertEqual(repr(response), "<HttpResponse status_code=204>")
|
|
|
|
|
2015-10-21 05:23:58 +08:00
|
|
|
def test_wrap_textiowrapper(self):
|
|
|
|
content = "Café :)"
|
|
|
|
r = HttpResponse()
|
|
|
|
with io.TextIOWrapper(r, UTF8) as buf:
|
|
|
|
buf.write(content)
|
|
|
|
self.assertEqual(r.content, content.encode(UTF8))
|
2015-11-12 03:17:32 +08:00
|
|
|
|
|
|
|
def test_generator_cache(self):
|
2020-05-01 22:54:15 +08:00
|
|
|
generator = (str(i) for i in range(10))
|
2015-11-12 03:17:32 +08:00
|
|
|
response = HttpResponse(content=generator)
|
|
|
|
self.assertEqual(response.content, b"0123456789")
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(StopIteration):
|
|
|
|
next(generator)
|
2015-11-12 03:17:32 +08:00
|
|
|
|
|
|
|
cache.set("my-response-key", response)
|
|
|
|
response = cache.get("my-response-key")
|
|
|
|
self.assertEqual(response.content, b"0123456789")
|