test_settings.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import os
  2. import shutil
  3. import tempfile
  4. from django import conf
  5. from django.test import SimpleTestCase
  6. from django.test.utils import extend_sys_path
  7. class TestStartProjectSettings(SimpleTestCase):
  8. def setUp(self):
  9. self.temp_dir = tempfile.TemporaryDirectory()
  10. self.addCleanup(self.temp_dir.cleanup)
  11. template_settings_py = os.path.join(
  12. os.path.dirname(conf.__file__),
  13. 'project_template',
  14. 'project_name',
  15. 'settings.py-tpl',
  16. )
  17. test_settings_py = os.path.join(self.temp_dir.name, 'test_settings.py')
  18. shutil.copyfile(template_settings_py, test_settings_py)
  19. def test_middleware_headers(self):
  20. """
  21. Ensure headers sent by the default MIDDLEWARE don't inadvertently
  22. change. For example, we never want "Vary: Cookie" to appear in the list
  23. since it prevents the caching of responses.
  24. """
  25. with extend_sys_path(self.temp_dir.name):
  26. from test_settings import MIDDLEWARE
  27. with self.settings(
  28. MIDDLEWARE=MIDDLEWARE,
  29. ROOT_URLCONF='project_template.urls',
  30. ):
  31. response = self.client.get('/empty/')
  32. headers = sorted(response.serialize_headers().split(b'\r\n'))
  33. self.assertEqual(headers, [
  34. b'Content-Length: 0',
  35. b'Content-Type: text/html; charset=utf-8',
  36. b'X-Frame-Options: SAMEORIGIN',
  37. ])