2011-01-24 14:24:35 +00:00
|
|
|
import pickle
|
|
|
|
import time
|
2015-01-28 12:35:27 +00:00
|
|
|
from datetime import datetime
|
2011-10-13 21:34:56 +00:00
|
|
|
|
2015-09-05 16:06:34 +00:00
|
|
|
from django.template import engines
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.template.response import (
|
|
|
|
ContentNotRenderedError,
|
|
|
|
SimpleTemplateResponse,
|
|
|
|
TemplateResponse,
|
|
|
|
)
|
2015-11-07 15:12:37 +00:00
|
|
|
from django.test import (
|
|
|
|
RequestFactory,
|
|
|
|
SimpleTestCase,
|
|
|
|
modify_settings,
|
|
|
|
override_settings,
|
|
|
|
)
|
2016-12-31 18:24:00 +00:00
|
|
|
from django.test.utils import require_jinja2
|
2010-12-07 13:57:01 +00:00
|
|
|
|
2015-02-23 21:30:05 +00:00
|
|
|
from .utils import TEMPLATE_DIR
|
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2010-12-07 13:57:01 +00:00
|
|
|
def test_processor(request):
|
|
|
|
return {"processors": "yes"}
|
2016-11-12 17:11:23 +00:00
|
|
|
|
|
|
|
|
2013-04-06 18:51:40 +00:00
|
|
|
test_processor_name = "template_tests.test_response.test_processor"
|
2010-12-07 13:57:01 +00:00
|
|
|
|
2011-01-16 15:38:03 +00:00
|
|
|
|
|
|
|
# A test middleware that installs a temporary URLConf
|
2019-09-26 17:06:35 +00:00
|
|
|
def custom_urlconf_middleware(get_response):
|
|
|
|
def middleware(request):
|
2013-02-26 12:19:18 +00:00
|
|
|
request.urlconf = "template_tests.alternate_urls"
|
2019-09-26 17:06:35 +00:00
|
|
|
return get_response(request)
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2019-09-26 17:06:35 +00:00
|
|
|
return middleware
|
2011-01-16 15:38:03 +00:00
|
|
|
|
|
|
|
|
2014-12-03 20:36:17 +00:00
|
|
|
class SimpleTemplateResponseTest(SimpleTestCase):
|
2010-12-07 13:57:01 +00:00
|
|
|
def _response(self, template="foo", *args, **kwargs):
|
2015-01-09 21:59:00 +00:00
|
|
|
template = engines["django"].from_string(template)
|
|
|
|
return SimpleTemplateResponse(template, *args, **kwargs)
|
2010-12-07 13:57:01 +00:00
|
|
|
|
|
|
|
def test_template_resolving(self):
|
|
|
|
response = SimpleTemplateResponse("first/test.html")
|
|
|
|
response.render()
|
2012-08-14 09:57:18 +00:00
|
|
|
self.assertEqual(response.content, b"First template\n")
|
2010-12-07 13:57:01 +00:00
|
|
|
|
|
|
|
templates = ["foo.html", "second/test.html", "first/test.html"]
|
|
|
|
response = SimpleTemplateResponse(templates)
|
|
|
|
response.render()
|
2012-08-14 09:57:18 +00:00
|
|
|
self.assertEqual(response.content, b"Second template\n")
|
2010-12-07 13:57:01 +00:00
|
|
|
|
|
|
|
response = self._response()
|
|
|
|
response.render()
|
2012-08-14 09:57:18 +00:00
|
|
|
self.assertEqual(response.content, b"foo")
|
2010-12-07 13:57:01 +00:00
|
|
|
|
|
|
|
def test_explicit_baking(self):
|
|
|
|
# explicit baking
|
|
|
|
response = self._response()
|
|
|
|
self.assertFalse(response.is_rendered)
|
|
|
|
response.render()
|
|
|
|
self.assertTrue(response.is_rendered)
|
|
|
|
|
|
|
|
def test_render(self):
|
|
|
|
# response is not re-rendered without the render call
|
|
|
|
response = self._response().render()
|
2012-08-14 09:57:18 +00:00
|
|
|
self.assertEqual(response.content, b"foo")
|
2010-12-07 13:57:01 +00:00
|
|
|
|
|
|
|
# rebaking doesn't change the rendered content
|
2015-01-09 21:59:00 +00:00
|
|
|
template = engines["django"].from_string("bar{{ baz }}")
|
|
|
|
response.template_name = template
|
2010-12-07 13:57:01 +00:00
|
|
|
response.render()
|
2012-08-14 09:57:18 +00:00
|
|
|
self.assertEqual(response.content, b"foo")
|
2010-12-07 13:57:01 +00:00
|
|
|
|
|
|
|
# but rendered content can be overridden by manually
|
|
|
|
# setting content
|
|
|
|
response.content = "bar"
|
2012-08-14 09:57:18 +00:00
|
|
|
self.assertEqual(response.content, b"bar")
|
2010-12-07 13:57:01 +00:00
|
|
|
|
|
|
|
def test_iteration_unrendered(self):
|
|
|
|
# unrendered response raises an exception on iteration
|
|
|
|
response = self._response()
|
|
|
|
self.assertFalse(response.is_rendered)
|
|
|
|
|
|
|
|
def iteration():
|
2019-02-09 14:18:48 +00:00
|
|
|
list(response)
|
|
|
|
|
|
|
|
msg = "The response content must be rendered before it can be iterated over."
|
|
|
|
with self.assertRaisesMessage(ContentNotRenderedError, msg):
|
2016-01-17 11:26:39 +00:00
|
|
|
iteration()
|
2010-12-07 13:57:01 +00:00
|
|
|
self.assertFalse(response.is_rendered)
|
|
|
|
|
|
|
|
def test_iteration_rendered(self):
|
|
|
|
# iteration works for rendered responses
|
|
|
|
response = self._response().render()
|
2019-02-09 14:18:48 +00:00
|
|
|
self.assertEqual(list(response), [b"foo"])
|
2010-12-07 13:57:01 +00:00
|
|
|
|
|
|
|
def test_content_access_unrendered(self):
|
|
|
|
# unrendered response raises an exception when content is accessed
|
|
|
|
response = self._response()
|
|
|
|
self.assertFalse(response.is_rendered)
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(ContentNotRenderedError):
|
|
|
|
response.content
|
2010-12-07 13:57:01 +00:00
|
|
|
self.assertFalse(response.is_rendered)
|
|
|
|
|
|
|
|
def test_content_access_rendered(self):
|
|
|
|
# rendered response content can be accessed
|
|
|
|
response = self._response().render()
|
2012-08-14 09:57:18 +00:00
|
|
|
self.assertEqual(response.content, b"foo")
|
2010-12-07 13:57:01 +00:00
|
|
|
|
|
|
|
def test_set_content(self):
|
2013-07-28 01:45:25 +00:00
|
|
|
# content can be overridden
|
2010-12-07 13:57:01 +00:00
|
|
|
response = self._response()
|
|
|
|
self.assertFalse(response.is_rendered)
|
|
|
|
response.content = "spam"
|
|
|
|
self.assertTrue(response.is_rendered)
|
2012-08-14 09:57:18 +00:00
|
|
|
self.assertEqual(response.content, b"spam")
|
2010-12-07 13:57:01 +00:00
|
|
|
response.content = "baz"
|
2012-08-14 09:57:18 +00:00
|
|
|
self.assertEqual(response.content, b"baz")
|
2010-12-07 13:57:01 +00:00
|
|
|
|
|
|
|
def test_dict_context(self):
|
|
|
|
response = self._response("{{ foo }}{{ processors }}", {"foo": "bar"})
|
|
|
|
self.assertEqual(response.context_data, {"foo": "bar"})
|
|
|
|
response.render()
|
2012-08-14 09:57:18 +00:00
|
|
|
self.assertEqual(response.content, b"bar")
|
2010-12-07 13:57:01 +00:00
|
|
|
|
|
|
|
def test_kwargs(self):
|
2016-11-24 10:56:39 +00:00
|
|
|
response = self._response(
|
|
|
|
content_type="application/json", status=504, charset="ascii"
|
|
|
|
)
|
2020-07-14 11:32:24 +00:00
|
|
|
self.assertEqual(response.headers["content-type"], "application/json")
|
2010-12-07 13:57:01 +00:00
|
|
|
self.assertEqual(response.status_code, 504)
|
2016-11-24 10:56:39 +00:00
|
|
|
self.assertEqual(response.charset, "ascii")
|
2010-12-07 13:57:01 +00:00
|
|
|
|
|
|
|
def test_args(self):
|
|
|
|
response = SimpleTemplateResponse("", {}, "application/json", 504)
|
2020-07-14 11:32:24 +00:00
|
|
|
self.assertEqual(response.headers["content-type"], "application/json")
|
2010-12-07 13:57:01 +00:00
|
|
|
self.assertEqual(response.status_code, 504)
|
|
|
|
|
2015-01-26 20:57:10 +00:00
|
|
|
@require_jinja2
|
|
|
|
def test_using(self):
|
|
|
|
response = SimpleTemplateResponse("template_tests/using.html").render()
|
|
|
|
self.assertEqual(response.content, b"DTL\n")
|
|
|
|
response = SimpleTemplateResponse(
|
|
|
|
"template_tests/using.html", using="django"
|
|
|
|
).render()
|
|
|
|
self.assertEqual(response.content, b"DTL\n")
|
|
|
|
response = SimpleTemplateResponse(
|
|
|
|
"template_tests/using.html", using="jinja2"
|
|
|
|
).render()
|
|
|
|
self.assertEqual(response.content, b"Jinja2\n")
|
|
|
|
|
2011-01-24 14:24:35 +00:00
|
|
|
def test_post_callbacks(self):
|
|
|
|
"Rendering a template response triggers the post-render callbacks"
|
|
|
|
post = []
|
|
|
|
|
|
|
|
def post1(obj):
|
|
|
|
post.append("post1")
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2011-01-24 14:24:35 +00:00
|
|
|
def post2(obj):
|
|
|
|
post.append("post2")
|
|
|
|
|
|
|
|
response = SimpleTemplateResponse("first/test.html", {})
|
|
|
|
response.add_post_render_callback(post1)
|
|
|
|
response.add_post_render_callback(post2)
|
|
|
|
|
|
|
|
# When the content is rendered, all the callbacks are invoked, too.
|
|
|
|
response.render()
|
2012-08-14 09:57:18 +00:00
|
|
|
self.assertEqual(response.content, b"First template\n")
|
2013-10-26 19:15:03 +00:00
|
|
|
self.assertEqual(post, ["post1", "post2"])
|
2011-01-24 14:24:35 +00:00
|
|
|
|
|
|
|
def test_pickling(self):
|
|
|
|
# Create a template response. The context is
|
2015-12-02 23:55:50 +00:00
|
|
|
# known to be unpicklable (e.g., a function).
|
2011-01-24 14:24:35 +00:00
|
|
|
response = SimpleTemplateResponse(
|
|
|
|
"first/test.html",
|
|
|
|
{
|
2013-10-19 23:33:10 +00:00
|
|
|
"value": 123,
|
|
|
|
"fn": datetime.now,
|
2013-10-18 09:02:43 +00:00
|
|
|
},
|
|
|
|
)
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(ContentNotRenderedError):
|
|
|
|
pickle.dumps(response)
|
2011-01-24 14:24:35 +00:00
|
|
|
|
|
|
|
# But if we render the response, we can pickle it.
|
|
|
|
response.render()
|
|
|
|
pickled_response = pickle.dumps(response)
|
|
|
|
unpickled_response = pickle.loads(pickled_response)
|
|
|
|
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(unpickled_response.content, response.content)
|
2020-07-14 11:32:24 +00:00
|
|
|
self.assertEqual(
|
|
|
|
unpickled_response.headers["content-type"], response.headers["content-type"]
|
|
|
|
)
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(unpickled_response.status_code, response.status_code)
|
2011-01-24 14:24:35 +00:00
|
|
|
|
2014-04-26 17:18:45 +00:00
|
|
|
# ...and the unpickled response doesn't have the
|
2011-01-24 14:24:35 +00:00
|
|
|
# template-related attributes, so it can't be re-rendered
|
2011-07-29 09:40:50 +00:00
|
|
|
template_attrs = ("template_name", "context_data", "_post_render_callbacks")
|
|
|
|
for attr in template_attrs:
|
|
|
|
self.assertFalse(hasattr(unpickled_response, attr))
|
|
|
|
|
|
|
|
# ...and requesting any of those attributes raises an exception
|
|
|
|
for attr in template_attrs:
|
2011-09-30 11:46:23 +00:00
|
|
|
with self.assertRaises(AttributeError):
|
2011-07-29 09:40:50 +00:00
|
|
|
getattr(unpickled_response, attr)
|
|
|
|
|
|
|
|
def test_repickling(self):
|
|
|
|
response = SimpleTemplateResponse(
|
|
|
|
"first/test.html",
|
|
|
|
{
|
2013-10-19 23:33:10 +00:00
|
|
|
"value": 123,
|
|
|
|
"fn": datetime.now,
|
2013-10-18 09:02:43 +00:00
|
|
|
},
|
|
|
|
)
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(ContentNotRenderedError):
|
|
|
|
pickle.dumps(response)
|
2011-07-29 09:40:50 +00:00
|
|
|
|
|
|
|
response.render()
|
|
|
|
pickled_response = pickle.dumps(response)
|
|
|
|
unpickled_response = pickle.loads(pickled_response)
|
2013-10-19 12:31:38 +00:00
|
|
|
pickle.dumps(unpickled_response)
|
2010-12-07 13:57:01 +00:00
|
|
|
|
2012-11-09 20:07:53 +00:00
|
|
|
def test_pickling_cookie(self):
|
|
|
|
response = SimpleTemplateResponse(
|
|
|
|
"first/test.html",
|
|
|
|
{
|
2013-10-19 23:33:10 +00:00
|
|
|
"value": 123,
|
|
|
|
"fn": datetime.now,
|
2013-10-18 09:02:43 +00:00
|
|
|
},
|
|
|
|
)
|
2012-11-09 20:07:53 +00:00
|
|
|
|
|
|
|
response.cookies["key"] = "value"
|
|
|
|
|
|
|
|
response.render()
|
|
|
|
pickled_response = pickle.dumps(response, pickle.HIGHEST_PROTOCOL)
|
|
|
|
unpickled_response = pickle.loads(pickled_response)
|
|
|
|
|
|
|
|
self.assertEqual(unpickled_response.cookies["key"].value, "value")
|
|
|
|
|
2020-09-15 10:43:37 +00:00
|
|
|
def test_headers(self):
|
|
|
|
response = SimpleTemplateResponse(
|
|
|
|
"first/test.html",
|
|
|
|
{"value": 123, "fn": datetime.now},
|
|
|
|
headers={"X-Foo": "foo"},
|
|
|
|
)
|
|
|
|
self.assertEqual(response.headers["X-Foo"], "foo")
|
2022-02-03 19:24:19 +00:00
|
|
|
|
|
|
|
|
2014-12-17 21:51:42 +00:00
|
|
|
@override_settings(
|
|
|
|
TEMPLATES=[
|
|
|
|
{
|
|
|
|
"BACKEND": "django.template.backends.django.DjangoTemplates",
|
2015-02-23 21:30:05 +00:00
|
|
|
"DIRS": [TEMPLATE_DIR],
|
2014-12-17 21:51:42 +00:00
|
|
|
"OPTIONS": {
|
|
|
|
"context_processors": [test_processor_name],
|
|
|
|
},
|
|
|
|
}
|
2022-02-03 19:24:19 +00:00
|
|
|
]
|
2014-12-17 21:51:42 +00:00
|
|
|
)
|
2014-12-03 20:36:17 +00:00
|
|
|
class TemplateResponseTest(SimpleTestCase):
|
2018-11-26 19:01:27 +00:00
|
|
|
factory = RequestFactory()
|
2010-12-07 13:57:01 +00:00
|
|
|
|
|
|
|
def _response(self, template="foo", *args, **kwargs):
|
2015-01-09 21:59:00 +00:00
|
|
|
self._request = self.factory.get("/")
|
|
|
|
template = engines["django"].from_string(template)
|
|
|
|
return TemplateResponse(self._request, template, *args, **kwargs)
|
2010-12-07 13:57:01 +00:00
|
|
|
|
|
|
|
def test_render(self):
|
|
|
|
response = self._response("{{ foo }}{{ processors }}").render()
|
2012-08-14 09:57:18 +00:00
|
|
|
self.assertEqual(response.content, b"yes")
|
2010-12-07 13:57:01 +00:00
|
|
|
|
|
|
|
def test_render_with_requestcontext(self):
|
|
|
|
response = self._response("{{ foo }}{{ processors }}", {"foo": "bar"}).render()
|
2012-08-14 09:57:18 +00:00
|
|
|
self.assertEqual(response.content, b"baryes")
|
2010-12-07 13:57:01 +00:00
|
|
|
|
2014-11-10 14:43:40 +00:00
|
|
|
def test_context_processor_priority(self):
|
|
|
|
# context processors should be overridden by passed-in context
|
|
|
|
response = self._response(
|
|
|
|
"{{ foo }}{{ processors }}", {"processors": "no"}
|
|
|
|
).render()
|
|
|
|
self.assertEqual(response.content, b"no")
|
|
|
|
|
2010-12-07 13:57:01 +00:00
|
|
|
def test_kwargs(self):
|
2018-03-16 09:54:34 +00:00
|
|
|
response = self._response(content_type="application/json", status=504)
|
2020-07-14 11:32:24 +00:00
|
|
|
self.assertEqual(response.headers["content-type"], "application/json")
|
2010-12-07 13:57:01 +00:00
|
|
|
self.assertEqual(response.status_code, 504)
|
|
|
|
|
|
|
|
def test_args(self):
|
|
|
|
response = TemplateResponse(
|
|
|
|
self.factory.get("/"), "", {}, "application/json", 504
|
|
|
|
)
|
2020-07-14 11:32:24 +00:00
|
|
|
self.assertEqual(response.headers["content-type"], "application/json")
|
2010-12-07 13:57:01 +00:00
|
|
|
self.assertEqual(response.status_code, 504)
|
2011-01-05 22:41:43 +00:00
|
|
|
|
2015-01-26 20:57:10 +00:00
|
|
|
@require_jinja2
|
|
|
|
def test_using(self):
|
|
|
|
request = self.factory.get("/")
|
|
|
|
response = TemplateResponse(request, "template_tests/using.html").render()
|
|
|
|
self.assertEqual(response.content, b"DTL\n")
|
|
|
|
response = TemplateResponse(
|
|
|
|
request, "template_tests/using.html", using="django"
|
|
|
|
).render()
|
|
|
|
self.assertEqual(response.content, b"DTL\n")
|
|
|
|
response = TemplateResponse(
|
|
|
|
request, "template_tests/using.html", using="jinja2"
|
|
|
|
).render()
|
|
|
|
self.assertEqual(response.content, b"Jinja2\n")
|
|
|
|
|
2011-01-24 14:24:35 +00:00
|
|
|
def test_pickling(self):
|
|
|
|
# Create a template response. The context is
|
2015-12-02 23:55:50 +00:00
|
|
|
# known to be unpicklable (e.g., a function).
|
2016-02-05 18:53:03 +00:00
|
|
|
response = TemplateResponse(
|
|
|
|
self.factory.get("/"),
|
2011-01-24 14:24:35 +00:00
|
|
|
"first/test.html",
|
|
|
|
{
|
|
|
|
"value": 123,
|
|
|
|
"fn": datetime.now,
|
2014-03-30 19:11:05 +00:00
|
|
|
},
|
|
|
|
)
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(ContentNotRenderedError):
|
|
|
|
pickle.dumps(response)
|
2011-01-24 14:24:35 +00:00
|
|
|
|
|
|
|
# But if we render the response, we can pickle it.
|
|
|
|
response.render()
|
|
|
|
pickled_response = pickle.dumps(response)
|
|
|
|
unpickled_response = pickle.loads(pickled_response)
|
|
|
|
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(unpickled_response.content, response.content)
|
2020-07-14 11:32:24 +00:00
|
|
|
self.assertEqual(
|
|
|
|
unpickled_response.headers["content-type"], response.headers["content-type"]
|
|
|
|
)
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(unpickled_response.status_code, response.status_code)
|
2011-01-24 14:24:35 +00:00
|
|
|
|
2014-04-26 17:18:45 +00:00
|
|
|
# ...and the unpickled response doesn't have the
|
2011-01-24 14:24:35 +00:00
|
|
|
# template-related attributes, so it can't be re-rendered
|
2015-09-03 19:52:04 +00:00
|
|
|
template_attrs = (
|
|
|
|
"template_name",
|
|
|
|
"context_data",
|
|
|
|
"_post_render_callbacks",
|
|
|
|
"_request",
|
|
|
|
)
|
2011-07-29 09:40:50 +00:00
|
|
|
for attr in template_attrs:
|
|
|
|
self.assertFalse(hasattr(unpickled_response, attr))
|
|
|
|
|
|
|
|
# ...and requesting any of those attributes raises an exception
|
|
|
|
for attr in template_attrs:
|
2011-09-30 11:46:23 +00:00
|
|
|
with self.assertRaises(AttributeError):
|
2011-07-29 09:40:50 +00:00
|
|
|
getattr(unpickled_response, attr)
|
|
|
|
|
|
|
|
def test_repickling(self):
|
|
|
|
response = SimpleTemplateResponse(
|
|
|
|
"first/test.html",
|
|
|
|
{
|
2013-10-19 23:33:10 +00:00
|
|
|
"value": 123,
|
|
|
|
"fn": datetime.now,
|
2013-10-18 09:02:43 +00:00
|
|
|
},
|
|
|
|
)
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(ContentNotRenderedError):
|
|
|
|
pickle.dumps(response)
|
2011-07-29 09:40:50 +00:00
|
|
|
|
|
|
|
response.render()
|
|
|
|
pickled_response = pickle.dumps(response)
|
|
|
|
unpickled_response = pickle.loads(pickled_response)
|
2013-10-19 12:31:38 +00:00
|
|
|
pickle.dumps(unpickled_response)
|
2011-01-24 14:24:35 +00:00
|
|
|
|
2020-09-15 10:43:37 +00:00
|
|
|
def test_headers(self):
|
|
|
|
response = TemplateResponse(
|
|
|
|
self.factory.get("/"),
|
|
|
|
"first/test.html",
|
|
|
|
{"value": 123, "fn": datetime.now},
|
|
|
|
headers={"X-Foo": "foo"},
|
|
|
|
)
|
|
|
|
self.assertEqual(response.headers["X-Foo"], "foo")
|
|
|
|
|
2011-01-16 15:38:03 +00:00
|
|
|
|
2019-09-26 17:06:35 +00:00
|
|
|
@modify_settings(
|
|
|
|
MIDDLEWARE={"append": ["template_tests.test_response.custom_urlconf_middleware"]}
|
|
|
|
)
|
2015-11-07 15:12:37 +00:00
|
|
|
@override_settings(ROOT_URLCONF="template_tests.urls")
|
2014-12-03 20:36:17 +00:00
|
|
|
class CustomURLConfTest(SimpleTestCase):
|
2011-01-16 15:38:03 +00:00
|
|
|
def test_custom_urlconf(self):
|
|
|
|
response = self.client.get("/template_response_view/")
|
2011-01-24 14:24:35 +00:00
|
|
|
self.assertContains(response, "This is where you can find the snark: /snark/")
|
|
|
|
|
|
|
|
|
2015-11-07 15:12:37 +00:00
|
|
|
@modify_settings(
|
|
|
|
MIDDLEWARE={
|
|
|
|
"append": [
|
|
|
|
"django.middleware.cache.FetchFromCacheMiddleware",
|
|
|
|
"django.middleware.cache.UpdateCacheMiddleware",
|
|
|
|
],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
@override_settings(
|
2024-01-29 11:16:40 +00:00
|
|
|
CACHE_MIDDLEWARE_SECONDS=2, ROOT_URLCONF="template_tests.alternate_urls"
|
2015-11-07 15:12:37 +00:00
|
|
|
)
|
|
|
|
class CacheMiddlewareTest(SimpleTestCase):
|
|
|
|
def test_middleware_caching(self):
|
|
|
|
response = self.client.get("/template_response_view/")
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
time.sleep(1.0)
|
|
|
|
|
|
|
|
response2 = self.client.get("/template_response_view/")
|
|
|
|
self.assertEqual(response2.status_code, 200)
|
|
|
|
|
|
|
|
self.assertEqual(response.content, response2.content)
|
|
|
|
|
|
|
|
time.sleep(2.0)
|
|
|
|
|
|
|
|
# Let the cache expire and test again
|
|
|
|
response2 = self.client.get("/template_response_view/")
|
|
|
|
self.assertEqual(response2.status_code, 200)
|
|
|
|
|
|
|
|
self.assertNotEqual(response.content, response2.content)
|