2012-08-14 17:57:18 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2011-01-24 22:24:35 +08:00
|
|
|
import pickle
|
|
|
|
import time
|
2015-01-28 20:35:27 +08:00
|
|
|
from datetime import datetime
|
2011-10-14 05:34:56 +08:00
|
|
|
|
2010-12-07 21:57:01 +08:00
|
|
|
from django.conf import settings
|
2015-09-06 00:06:34 +08:00
|
|
|
from django.template import engines
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.template.response import (
|
|
|
|
ContentNotRenderedError, SimpleTemplateResponse, TemplateResponse,
|
|
|
|
)
|
2015-09-06 00:06:34 +08:00
|
|
|
from django.test import RequestFactory, SimpleTestCase, override_settings
|
2015-01-27 04:57:10 +08:00
|
|
|
from django.test.utils import require_jinja2
|
2010-12-07 21:57:01 +08:00
|
|
|
|
2015-02-24 05:30:05 +08:00
|
|
|
from .utils import TEMPLATE_DIR
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2010-12-07 21:57:01 +08:00
|
|
|
def test_processor(request):
|
|
|
|
return {'processors': 'yes'}
|
2013-04-07 02:51:40 +08:00
|
|
|
test_processor_name = 'template_tests.test_response.test_processor'
|
2010-12-07 21:57:01 +08:00
|
|
|
|
2011-01-16 23:38:03 +08:00
|
|
|
|
|
|
|
# A test middleware that installs a temporary URLConf
|
|
|
|
class CustomURLConfMiddleware(object):
|
|
|
|
def process_request(self, request):
|
2013-02-26 20:19:18 +08:00
|
|
|
request.urlconf = 'template_tests.alternate_urls'
|
2011-01-16 23:38:03 +08:00
|
|
|
|
|
|
|
|
2014-12-04 04:36:17 +08:00
|
|
|
class SimpleTemplateResponseTest(SimpleTestCase):
|
2010-12-07 21:57:01 +08:00
|
|
|
|
|
|
|
def _response(self, template='foo', *args, **kwargs):
|
2015-01-10 05:59:00 +08:00
|
|
|
template = engines['django'].from_string(template)
|
|
|
|
return SimpleTemplateResponse(template, *args, **kwargs)
|
2010-12-07 21:57:01 +08:00
|
|
|
|
|
|
|
def test_template_resolving(self):
|
|
|
|
response = SimpleTemplateResponse('first/test.html')
|
|
|
|
response.render()
|
2012-08-14 17:57:18 +08:00
|
|
|
self.assertEqual(response.content, b'First template\n')
|
2010-12-07 21:57:01 +08:00
|
|
|
|
|
|
|
templates = ['foo.html', 'second/test.html', 'first/test.html']
|
|
|
|
response = SimpleTemplateResponse(templates)
|
|
|
|
response.render()
|
2012-08-14 17:57:18 +08:00
|
|
|
self.assertEqual(response.content, b'Second template\n')
|
2010-12-07 21:57:01 +08:00
|
|
|
|
|
|
|
response = self._response()
|
|
|
|
response.render()
|
2012-08-14 17:57:18 +08:00
|
|
|
self.assertEqual(response.content, b'foo')
|
2010-12-07 21:57:01 +08: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 17:57:18 +08:00
|
|
|
self.assertEqual(response.content, b'foo')
|
2010-12-07 21:57:01 +08:00
|
|
|
|
|
|
|
# rebaking doesn't change the rendered content
|
2015-01-10 05:59:00 +08:00
|
|
|
template = engines['django'].from_string('bar{{ baz }}')
|
|
|
|
response.template_name = template
|
2010-12-07 21:57:01 +08:00
|
|
|
response.render()
|
2012-08-14 17:57:18 +08:00
|
|
|
self.assertEqual(response.content, b'foo')
|
2010-12-07 21:57:01 +08:00
|
|
|
|
|
|
|
# but rendered content can be overridden by manually
|
|
|
|
# setting content
|
|
|
|
response.content = 'bar'
|
2012-08-14 17:57:18 +08:00
|
|
|
self.assertEqual(response.content, b'bar')
|
2010-12-07 21:57:01 +08: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 17:57:18 +08:00
|
|
|
self.assertEqual(res, [b'foo'])
|
2010-12-07 21:57:01 +08: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 17:57:18 +08:00
|
|
|
self.assertEqual(response.content, b'foo')
|
2010-12-07 21:57:01 +08:00
|
|
|
|
|
|
|
def test_set_content(self):
|
2013-07-28 09:45:25 +08:00
|
|
|
# content can be overridden
|
2010-12-07 21:57:01 +08:00
|
|
|
response = self._response()
|
|
|
|
self.assertFalse(response.is_rendered)
|
|
|
|
response.content = 'spam'
|
|
|
|
self.assertTrue(response.is_rendered)
|
2012-08-14 17:57:18 +08:00
|
|
|
self.assertEqual(response.content, b'spam')
|
2010-12-07 21:57:01 +08:00
|
|
|
response.content = 'baz'
|
2012-08-14 17:57:18 +08:00
|
|
|
self.assertEqual(response.content, b'baz')
|
2010-12-07 21:57:01 +08: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 17:57:18 +08:00
|
|
|
self.assertEqual(response.content, b'bar')
|
2010-12-07 21:57:01 +08:00
|
|
|
|
|
|
|
def test_kwargs(self):
|
2013-11-03 05:34:05 +08:00
|
|
|
response = self._response(content_type='application/json', status=504)
|
2010-12-07 21:57:01 +08: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-27 04:57:10 +08: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 22:24:35 +08: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 18:21:07 +08:00
|
|
|
|
2011-01-24 22:24:35 +08: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 17:57:18 +08:00
|
|
|
self.assertEqual(response.content, b'First template\n')
|
2013-10-27 03:15:03 +08:00
|
|
|
self.assertEqual(post, ['post1', 'post2'])
|
2011-01-24 22:24:35 +08: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-20 07:33:10 +08:00
|
|
|
'value': 123,
|
|
|
|
'fn': datetime.now,
|
2013-10-18 17:02:43 +08:00
|
|
|
})
|
2011-01-24 22:24:35 +08: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 23:04:39 +08: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 22:24:35 +08:00
|
|
|
|
2014-04-27 01:18:45 +08:00
|
|
|
# ...and the unpickled response doesn't have the
|
2011-01-24 22:24:35 +08:00
|
|
|
# template-related attributes, so it can't be re-rendered
|
2011-07-29 17:40:50 +08: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 19:46:23 +08:00
|
|
|
with self.assertRaises(AttributeError):
|
2011-07-29 17:40:50 +08:00
|
|
|
getattr(unpickled_response, attr)
|
|
|
|
|
|
|
|
def test_repickling(self):
|
|
|
|
response = SimpleTemplateResponse('first/test.html', {
|
2013-10-20 07:33:10 +08:00
|
|
|
'value': 123,
|
|
|
|
'fn': datetime.now,
|
2013-10-18 17:02:43 +08:00
|
|
|
})
|
2011-07-29 17:40:50 +08:00
|
|
|
self.assertRaises(ContentNotRenderedError,
|
|
|
|
pickle.dumps, response)
|
|
|
|
|
|
|
|
response.render()
|
|
|
|
pickled_response = pickle.dumps(response)
|
|
|
|
unpickled_response = pickle.loads(pickled_response)
|
2013-10-19 20:31:38 +08:00
|
|
|
pickle.dumps(unpickled_response)
|
2010-12-07 21:57:01 +08:00
|
|
|
|
2012-11-10 04:07:53 +08:00
|
|
|
def test_pickling_cookie(self):
|
|
|
|
response = SimpleTemplateResponse('first/test.html', {
|
2013-10-20 07:33:10 +08:00
|
|
|
'value': 123,
|
|
|
|
'fn': datetime.now,
|
2013-10-18 17:02:43 +08:00
|
|
|
})
|
2012-11-10 04:07:53 +08: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-18 05:51:42 +08:00
|
|
|
@override_settings(TEMPLATES=[{
|
|
|
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
2015-02-24 05:30:05 +08:00
|
|
|
'DIRS': [TEMPLATE_DIR],
|
2014-12-18 05:51:42 +08:00
|
|
|
'OPTIONS': {
|
|
|
|
'context_processors': [test_processor_name],
|
|
|
|
},
|
|
|
|
}])
|
2014-12-04 04:36:17 +08:00
|
|
|
class TemplateResponseTest(SimpleTestCase):
|
2012-04-09 21:24:57 +08:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.factory = RequestFactory()
|
2010-12-07 21:57:01 +08:00
|
|
|
|
|
|
|
def _response(self, template='foo', *args, **kwargs):
|
2015-01-10 05:59:00 +08:00
|
|
|
self._request = self.factory.get('/')
|
|
|
|
template = engines['django'].from_string(template)
|
|
|
|
return TemplateResponse(self._request, template, *args, **kwargs)
|
2010-12-07 21:57:01 +08:00
|
|
|
|
|
|
|
def test_render(self):
|
|
|
|
response = self._response('{{ foo }}{{ processors }}').render()
|
2012-08-14 17:57:18 +08:00
|
|
|
self.assertEqual(response.content, b'yes')
|
2010-12-07 21:57:01 +08:00
|
|
|
|
|
|
|
def test_render_with_requestcontext(self):
|
|
|
|
response = self._response('{{ foo }}{{ processors }}',
|
|
|
|
{'foo': 'bar'}).render()
|
2012-08-14 17:57:18 +08:00
|
|
|
self.assertEqual(response.content, b'baryes')
|
2010-12-07 21:57:01 +08:00
|
|
|
|
2014-11-10 22:43:40 +08: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 21:57:01 +08:00
|
|
|
def test_kwargs(self):
|
2013-11-03 05:34:05 +08:00
|
|
|
response = self._response(content_type='application/json',
|
2010-12-07 21:57:01 +08: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-06 06:41:43 +08:00
|
|
|
|
2015-01-27 04:57:10 +08: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 22:24:35 +08: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-31 03:11:05 +08:00
|
|
|
}
|
|
|
|
)
|
2011-01-24 22:24:35 +08: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 23:04:39 +08: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 22:24:35 +08:00
|
|
|
|
2014-04-27 01:18:45 +08:00
|
|
|
# ...and the unpickled response doesn't have the
|
2011-01-24 22:24:35 +08:00
|
|
|
# template-related attributes, so it can't be re-rendered
|
2015-09-04 03:52:04 +08:00
|
|
|
template_attrs = (
|
|
|
|
'template_name',
|
|
|
|
'context_data',
|
|
|
|
'_post_render_callbacks',
|
|
|
|
'_request',
|
|
|
|
)
|
2011-07-29 17:40:50 +08: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 19:46:23 +08:00
|
|
|
with self.assertRaises(AttributeError):
|
2011-07-29 17:40:50 +08:00
|
|
|
getattr(unpickled_response, attr)
|
|
|
|
|
|
|
|
def test_repickling(self):
|
|
|
|
response = SimpleTemplateResponse('first/test.html', {
|
2013-10-20 07:33:10 +08:00
|
|
|
'value': 123,
|
|
|
|
'fn': datetime.now,
|
2013-10-18 17:02:43 +08:00
|
|
|
})
|
2011-07-29 17:40:50 +08:00
|
|
|
self.assertRaises(ContentNotRenderedError,
|
|
|
|
pickle.dumps, response)
|
|
|
|
|
|
|
|
response.render()
|
|
|
|
pickled_response = pickle.dumps(response)
|
|
|
|
unpickled_response = pickle.loads(pickled_response)
|
2013-10-19 20:31:38 +08:00
|
|
|
pickle.dumps(unpickled_response)
|
2011-01-24 22:24:35 +08:00
|
|
|
|
2011-01-16 23:38:03 +08:00
|
|
|
|
2013-10-14 21:14:17 +08:00
|
|
|
@override_settings(
|
2015-01-22 00:55:57 +08:00
|
|
|
MIDDLEWARE_CLASSES=settings.MIDDLEWARE_CLASSES + [
|
2013-10-14 21:14:17 +08:00
|
|
|
'template_tests.test_response.CustomURLConfMiddleware'
|
2014-04-05 14:04:46 +08:00
|
|
|
],
|
|
|
|
ROOT_URLCONF='template_tests.urls',
|
2013-10-14 21:14:17 +08:00
|
|
|
)
|
2014-12-04 04:36:17 +08:00
|
|
|
class CustomURLConfTest(SimpleTestCase):
|
2011-01-16 23:38:03 +08:00
|
|
|
|
|
|
|
def test_custom_urlconf(self):
|
|
|
|
response = self.client.get('/template_response_view/')
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2011-01-24 22:24:35 +08:00
|
|
|
self.assertContains(response, 'This is where you can find the snark: /snark/')
|
|
|
|
|
|
|
|
|
2013-10-14 21:14:17 +08:00
|
|
|
@override_settings(
|
|
|
|
CACHE_MIDDLEWARE_SECONDS=2.0,
|
2015-01-22 00:55:57 +08:00
|
|
|
MIDDLEWARE_CLASSES=settings.MIDDLEWARE_CLASSES + [
|
2013-10-14 21:14:17 +08:00
|
|
|
'django.middleware.cache.FetchFromCacheMiddleware',
|
|
|
|
'django.middleware.cache.UpdateCacheMiddleware',
|
2014-04-05 14:04:46 +08:00
|
|
|
],
|
|
|
|
ROOT_URLCONF='template_tests.alternate_urls',
|
2013-10-14 21:14:17 +08:00
|
|
|
)
|
2014-12-04 04:36:17 +08:00
|
|
|
class CacheMiddlewareTest(SimpleTestCase):
|
2011-01-24 22:24:35 +08: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 23:38:03 +08:00
|
|
|
|
2011-01-24 22:24:35 +08:00
|
|
|
# Let the cache expire and test again
|
|
|
|
response2 = self.client.get('/template_response_view/')
|
|
|
|
self.assertEqual(response2.status_code, 200)
|
2011-01-16 23:38:03 +08:00
|
|
|
|
2011-01-24 22:24:35 +08:00
|
|
|
self.assertNotEqual(response.content, response2.content)
|