2015-04-18 05:38:20 +08:00
|
|
|
from django.test import SimpleTestCase, ignore_warnings, override_settings
|
2015-01-27 04:57:10 +08:00
|
|
|
from django.test.utils import require_jinja2
|
2015-06-23 01:54:35 +08:00
|
|
|
from django.utils.deprecation import RemovedInDjango110Warning
|
2010-12-22 01:18:41 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2012-04-09 21:24:57 +08:00
|
|
|
@override_settings(
|
2014-11-23 00:52:12 +08:00
|
|
|
ROOT_URLCONF='shortcuts.urls',
|
2012-04-09 21:24:57 +08:00
|
|
|
)
|
2015-04-18 05:38:20 +08:00
|
|
|
class ShortcutTests(SimpleTestCase):
|
2011-04-02 21:27:40 +08:00
|
|
|
|
2010-12-22 01:18:41 +08:00
|
|
|
def test_render_to_response(self):
|
2014-11-23 00:52:12 +08:00
|
|
|
response = self.client.get('/render_to_response/')
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
2012-08-12 04:29:18 +08:00
|
|
|
self.assertEqual(response.content, b'FOO.BAR..\n')
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')
|
2010-12-22 01:18:41 +08:00
|
|
|
|
2015-01-04 02:28:15 +08:00
|
|
|
def test_render_to_response_with_multiple_templates(self):
|
|
|
|
response = self.client.get('/render_to_response/multiple_templates/')
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.content, b'FOO.BAR..\n')
|
|
|
|
|
2015-06-23 01:54:35 +08:00
|
|
|
@ignore_warnings(category=RemovedInDjango110Warning)
|
2010-12-22 01:18:41 +08:00
|
|
|
def test_render_to_response_with_request_context(self):
|
2014-12-22 04:19:05 +08:00
|
|
|
response = self.client.get('/render_to_response/request_context/')
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
2015-01-13 05:31:44 +08:00
|
|
|
self.assertEqual(response.content, b'FOO.BAR../render_to_response/request_context/\n')
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')
|
2010-12-22 01:18:41 +08:00
|
|
|
|
2013-01-31 20:39:29 +08:00
|
|
|
def test_render_to_response_with_content_type(self):
|
2014-11-23 00:52:12 +08:00
|
|
|
response = self.client.get('/render_to_response/content_type/')
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
2012-08-12 04:29:18 +08:00
|
|
|
self.assertEqual(response.content, b'FOO.BAR..\n')
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response['Content-Type'], 'application/x-rendertest')
|
2010-12-22 01:18:41 +08:00
|
|
|
|
2015-06-23 01:54:35 +08:00
|
|
|
@ignore_warnings(category=RemovedInDjango110Warning)
|
2013-09-16 05:51:24 +08:00
|
|
|
def test_render_to_response_with_dirs(self):
|
2014-12-22 04:19:05 +08:00
|
|
|
response = self.client.get('/render_to_response/dirs/')
|
2013-09-16 05:51:24 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.content, b'spam eggs\n')
|
|
|
|
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')
|
|
|
|
|
2015-01-27 04:57:10 +08:00
|
|
|
def test_render_to_response_with_status(self):
|
|
|
|
response = self.client.get('/render_to_response/status/')
|
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
self.assertEqual(response.content, b'FOO.BAR..\n')
|
|
|
|
|
|
|
|
@require_jinja2
|
|
|
|
def test_render_to_response_with_using(self):
|
|
|
|
response = self.client.get('/render_to_response/using/')
|
|
|
|
self.assertEqual(response.content, b'DTL\n')
|
|
|
|
response = self.client.get('/render_to_response/using/?using=django')
|
|
|
|
self.assertEqual(response.content, b'DTL\n')
|
|
|
|
response = self.client.get('/render_to_response/using/?using=jinja2')
|
|
|
|
self.assertEqual(response.content, b'Jinja2\n')
|
|
|
|
|
2015-06-23 01:54:35 +08:00
|
|
|
@ignore_warnings(category=RemovedInDjango110Warning)
|
2014-11-22 16:22:38 +08:00
|
|
|
def test_render_to_response_with_context_instance_misuse(self):
|
|
|
|
"""
|
|
|
|
For backwards-compatibility, ensure that it's possible to pass a
|
|
|
|
RequestContext instance in the dictionary argument instead of the
|
|
|
|
context_instance argument.
|
|
|
|
"""
|
2014-12-22 04:19:05 +08:00
|
|
|
response = self.client.get('/render_to_response/context_instance_misuse/')
|
2014-11-22 16:22:38 +08:00
|
|
|
self.assertContains(response, 'context processor output')
|
|
|
|
|
2010-12-22 01:18:41 +08:00
|
|
|
def test_render(self):
|
2014-11-23 00:52:12 +08:00
|
|
|
response = self.client.get('/render/')
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
2015-01-13 05:31:44 +08:00
|
|
|
self.assertEqual(response.content, b'FOO.BAR../render/\n')
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')
|
2014-12-15 00:48:51 +08:00
|
|
|
self.assertFalse(hasattr(response.context.request, 'current_app'))
|
2010-12-22 01:18:41 +08:00
|
|
|
|
2015-01-04 02:28:15 +08:00
|
|
|
def test_render_with_multiple_templates(self):
|
|
|
|
response = self.client.get('/render/multiple_templates/')
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2015-01-13 05:31:44 +08:00
|
|
|
self.assertEqual(response.content, b'FOO.BAR../render/multiple_templates/\n')
|
2015-01-04 02:28:15 +08:00
|
|
|
|
2015-06-23 01:54:35 +08:00
|
|
|
@ignore_warnings(category=RemovedInDjango110Warning)
|
2010-12-22 01:18:41 +08:00
|
|
|
def test_render_with_base_context(self):
|
2014-12-22 04:19:05 +08:00
|
|
|
response = self.client.get('/render/base_context/')
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
2012-08-12 04:29:18 +08:00
|
|
|
self.assertEqual(response.content, b'FOO.BAR..\n')
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')
|
2010-12-22 01:18:41 +08:00
|
|
|
|
2010-12-22 12:54:10 +08:00
|
|
|
def test_render_with_content_type(self):
|
2014-11-23 00:52:12 +08:00
|
|
|
response = self.client.get('/render/content_type/')
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
2015-01-13 05:31:44 +08:00
|
|
|
self.assertEqual(response.content, b'FOO.BAR../render/content_type/\n')
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response['Content-Type'], 'application/x-rendertest')
|
2010-12-22 01:18:41 +08:00
|
|
|
|
2010-12-22 12:54:10 +08:00
|
|
|
def test_render_with_status(self):
|
2014-11-23 00:52:12 +08:00
|
|
|
response = self.client.get('/render/status/')
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response.status_code, 403)
|
2015-01-13 05:31:44 +08:00
|
|
|
self.assertEqual(response.content, b'FOO.BAR../render/status/\n')
|
2010-12-22 12:54:10 +08:00
|
|
|
|
2015-01-27 04:57:10 +08:00
|
|
|
@require_jinja2
|
|
|
|
def test_render_with_using(self):
|
|
|
|
response = self.client.get('/render/using/')
|
|
|
|
self.assertEqual(response.content, b'DTL\n')
|
|
|
|
response = self.client.get('/render/using/?using=django')
|
|
|
|
self.assertEqual(response.content, b'DTL\n')
|
|
|
|
response = self.client.get('/render/using/?using=jinja2')
|
|
|
|
self.assertEqual(response.content, b'Jinja2\n')
|
|
|
|
|
2015-06-23 01:54:35 +08:00
|
|
|
@ignore_warnings(category=RemovedInDjango110Warning)
|
2011-01-06 06:41:43 +08:00
|
|
|
def test_render_with_current_app(self):
|
2014-12-22 04:19:05 +08:00
|
|
|
response = self.client.get('/render/current_app/')
|
2014-12-15 00:48:51 +08:00
|
|
|
self.assertEqual(response.context.request.current_app, "foobar_app")
|
2011-01-06 06:41:43 +08:00
|
|
|
|
2015-06-23 01:54:35 +08:00
|
|
|
@ignore_warnings(category=RemovedInDjango110Warning)
|
2013-09-16 05:51:24 +08:00
|
|
|
def test_render_with_dirs(self):
|
2014-12-22 04:19:05 +08:00
|
|
|
response = self.client.get('/render/dirs/')
|
2013-09-16 05:51:24 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.content, b'spam eggs\n')
|
|
|
|
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')
|
|
|
|
|
2015-06-23 01:54:35 +08:00
|
|
|
@ignore_warnings(category=RemovedInDjango110Warning)
|
2011-01-06 06:41:43 +08:00
|
|
|
def test_render_with_current_app_conflict(self):
|
2014-11-23 00:52:12 +08:00
|
|
|
with self.assertRaises(ValueError):
|
2014-12-22 04:19:05 +08:00
|
|
|
self.client.get('/render/current_app_conflict/')
|