2012-08-14 09:57:18 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2011-01-24 14:24:35 +00:00
|
|
|
import pickle
|
|
|
|
import time
|
2015-02-09 18:19:34 +00:00
|
|
|
from datetime import datetime
|
2011-10-13 21:34:56 +00:00
|
|
|
|
2010-12-07 13:57:01 +00:00
|
|
|
from django.conf import settings
|
2015-01-09 21:59:00 +00:00
|
|
|
from django.template import Context, engines
|
2015-02-09 18:19:34 +00:00
|
|
|
from django.template.response import (
|
|
|
|
ContentNotRenderedError, SimpleTemplateResponse, TemplateResponse,
|
|
|
|
)
|
|
|
|
from django.test import (
|
|
|
|
RequestFactory, SimpleTestCase, ignore_warnings, override_settings,
|
|
|
|
)
|
2015-01-26 20:57:10 +00:00
|
|
|
from django.test.utils import require_jinja2
|
2014-12-14 16:48:51 +00:00
|
|
|
from django.utils.deprecation import RemovedInDjango20Warning
|
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'}
|
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
|
|
|
|
class CustomURLConfMiddleware(object):
|
|
|
|
def process_request(self, request):
|
2013-02-26 12:19:18 +00:00
|
|
|
request.urlconf = 'template_tests.alternate_urls'
|
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():
|
|
|
|
for x in response:
|
|
|
|
pass
|
|
|
|
self.assertRaises(ContentNotRenderedError, iteration)
|
|
|
|
self.assertFalse(response.is_rendered)
|
|
|
|
|
|
|
|
def test_iteration_rendered(self):
|
|
|
|
# iteration works for rendered responses
|
|
|
|
response = self._response().render()
|
|
|
|
res = [x for x in response]
|
2012-08-14 09:57:18 +00:00
|
|
|
self.assertEqual(res, [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)
|
|
|
|
self.assertRaises(ContentNotRenderedError, lambda: response.content)
|
|
|
|
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
|
|
|
|
2015-01-09 21:59:00 +00:00
|
|
|
@ignore_warnings(category=RemovedInDjango20Warning)
|
2010-12-07 13:57:01 +00:00
|
|
|
def test_context_instance(self):
|
|
|
|
response = self._response('{{ foo }}{{ processors }}',
|
|
|
|
Context({'foo': 'bar'}))
|
|
|
|
self.assertEqual(response.context_data.__class__, Context)
|
|
|
|
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):
|
2013-11-02 21:34:05 +00:00
|
|
|
response = self._response(content_type='application/json', status=504)
|
2010-12-07 13:57:01 +00:00
|
|
|
self.assertEqual(response['content-type'], 'application/json')
|
|
|
|
self.assertEqual(response.status_code, 504)
|
|
|
|
|
|
|
|
def test_args(self):
|
|
|
|
response = SimpleTemplateResponse('', {}, 'application/json', 504)
|
|
|
|
self.assertEqual(response['content-type'], 'application/json')
|
|
|
|
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
|
|
|
|
# known to be unpickleable (e.g., a function).
|
|
|
|
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
|
|
|
})
|
2011-01-24 14:24:35 +00:00
|
|
|
self.assertRaises(ContentNotRenderedError,
|
|
|
|
pickle.dumps, response)
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
self.assertEqual(unpickled_response['content-type'], response['content-type'])
|
|
|
|
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
|
|
|
})
|
2011-07-29 09:40:50 +00:00
|
|
|
self.assertRaises(ContentNotRenderedError,
|
|
|
|
pickle.dumps, response)
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
|
|
|
|
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],
|
|
|
|
},
|
|
|
|
}])
|
2014-12-03 20:36:17 +00:00
|
|
|
class TemplateResponseTest(SimpleTestCase):
|
2012-04-09 13:24:57 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.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
|
|
|
|
2015-01-09 21:59:00 +00:00
|
|
|
@ignore_warnings(category=RemovedInDjango20Warning)
|
2010-12-07 13:57:01 +00:00
|
|
|
def test_render_with_context(self):
|
|
|
|
response = self._response('{{ foo }}{{ processors }}',
|
|
|
|
Context({'foo': 'bar'})).render()
|
2012-08-14 09:57:18 +00:00
|
|
|
self.assertEqual(response.content, b'bar')
|
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):
|
2013-11-02 21:34:05 +00:00
|
|
|
response = self._response(content_type='application/json',
|
2010-12-07 13:57:01 +00:00
|
|
|
status=504)
|
|
|
|
self.assertEqual(response['content-type'], 'application/json')
|
|
|
|
self.assertEqual(response.status_code, 504)
|
|
|
|
|
|
|
|
def test_args(self):
|
|
|
|
response = TemplateResponse(self.factory.get('/'), '', {},
|
|
|
|
'application/json', 504)
|
|
|
|
self.assertEqual(response['content-type'], 'application/json')
|
|
|
|
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')
|
|
|
|
|
2014-12-21 20:19:05 +00:00
|
|
|
@ignore_warnings(category=RemovedInDjango20Warning)
|
2011-01-05 22:41:43 +00:00
|
|
|
def test_custom_app(self):
|
2015-01-09 21:59:00 +00:00
|
|
|
self._response('{{ foo }}', current_app="foobar")
|
|
|
|
self.assertEqual(self._request.current_app, 'foobar')
|
2011-01-16 15:38:03 +00:00
|
|
|
|
2011-01-24 14:24:35 +00:00
|
|
|
def test_pickling(self):
|
|
|
|
# Create a template response. The context is
|
|
|
|
# known to be unpickleable (e.g., a function).
|
|
|
|
response = TemplateResponse(self.factory.get('/'),
|
|
|
|
'first/test.html', {
|
|
|
|
'value': 123,
|
|
|
|
'fn': datetime.now,
|
2014-03-30 19:11:05 +00:00
|
|
|
}
|
|
|
|
)
|
2011-01-24 14:24:35 +00:00
|
|
|
self.assertRaises(ContentNotRenderedError,
|
|
|
|
pickle.dumps, response)
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
self.assertEqual(unpickled_response['content-type'], response['content-type'])
|
|
|
|
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', '_request', '_current_app')
|
|
|
|
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
|
|
|
})
|
2011-07-29 09:40:50 +00:00
|
|
|
self.assertRaises(ContentNotRenderedError,
|
|
|
|
pickle.dumps, response)
|
|
|
|
|
|
|
|
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
|
|
|
|
2011-01-16 15:38:03 +00:00
|
|
|
|
2013-10-14 13:14:17 +00:00
|
|
|
@override_settings(
|
|
|
|
MIDDLEWARE_CLASSES=list(settings.MIDDLEWARE_CLASSES) + [
|
|
|
|
'template_tests.test_response.CustomURLConfMiddleware'
|
2014-04-05 06:04:46 +00:00
|
|
|
],
|
|
|
|
ROOT_URLCONF='template_tests.urls',
|
2013-10-14 13:14:17 +00:00
|
|
|
)
|
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/')
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2011-01-24 14:24:35 +00:00
|
|
|
self.assertContains(response, 'This is where you can find the snark: /snark/')
|
|
|
|
|
|
|
|
|
2013-10-14 13:14:17 +00:00
|
|
|
@override_settings(
|
|
|
|
CACHE_MIDDLEWARE_SECONDS=2.0,
|
|
|
|
MIDDLEWARE_CLASSES=list(settings.MIDDLEWARE_CLASSES) + [
|
|
|
|
'django.middleware.cache.FetchFromCacheMiddleware',
|
|
|
|
'django.middleware.cache.UpdateCacheMiddleware',
|
2014-04-05 06:04:46 +00:00
|
|
|
],
|
|
|
|
ROOT_URLCONF='template_tests.alternate_urls',
|
2013-10-14 13:14:17 +00:00
|
|
|
)
|
2014-12-03 20:36:17 +00:00
|
|
|
class CacheMiddlewareTest(SimpleTestCase):
|
2011-01-24 14:24:35 +00:00
|
|
|
|
|
|
|
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)
|
2011-01-16 15:38:03 +00:00
|
|
|
|
2011-01-24 14:24:35 +00:00
|
|
|
# Let the cache expire and test again
|
|
|
|
response2 = self.client.get('/template_response_view/')
|
|
|
|
self.assertEqual(response2.status_code, 200)
|
2011-01-16 15:38:03 +00:00
|
|
|
|
2011-01-24 14:24:35 +00:00
|
|
|
self.assertNotEqual(response.content, response2.content)
|