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