|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from django.test import TestCase |
| 4 | +from django.test.utils import modify_settings |
| 5 | +from django.test.utils import override_settings |
| 6 | + |
| 7 | +from corsheaders.middleware import ACCESS_CONTROL_ALLOW_ORIGIN |
| 8 | + |
| 9 | + |
| 10 | +@modify_settings( |
| 11 | + MIDDLEWARE={ |
| 12 | + "remove": "corsheaders.middleware.CorsMiddleware", |
| 13 | + } |
| 14 | +) |
| 15 | +@override_settings(CORS_ALLOWED_ORIGINS=["https://example.com"]) |
| 16 | +class CorsDecoratorsTestCase(TestCase): |
| 17 | + def test_get_no_origin(self): |
| 18 | + resp = self.client.get("/decorated/hello/") |
| 19 | + assert ACCESS_CONTROL_ALLOW_ORIGIN not in resp |
| 20 | + assert resp.content == b"Decorated: hello" |
| 21 | + |
| 22 | + def test_get_not_in_allowed_origins(self): |
| 23 | + resp = self.client.get( |
| 24 | + "/decorated/hello/", |
| 25 | + HTTP_ORIGIN="https://example.net", |
| 26 | + ) |
| 27 | + assert ACCESS_CONTROL_ALLOW_ORIGIN not in resp |
| 28 | + assert resp.content == b"Decorated: hello" |
| 29 | + |
| 30 | + def test_get_in_allowed_origins_preflight(self): |
| 31 | + resp = self.client.options( |
| 32 | + "/decorated/hello/", |
| 33 | + HTTP_ORIGIN="https://example.com", |
| 34 | + HTTP_ACCESS_CONTROL_REQUEST_METHOD="GET", |
| 35 | + ) |
| 36 | + assert resp[ACCESS_CONTROL_ALLOW_ORIGIN] == "https://example.com" |
| 37 | + assert resp.content == b"" |
| 38 | + |
| 39 | + def test_get_in_allowed_origins(self): |
| 40 | + resp = self.client.get( |
| 41 | + "/decorated/hello/", |
| 42 | + HTTP_ORIGIN="https://example.com", |
| 43 | + ) |
| 44 | + assert resp[ACCESS_CONTROL_ALLOW_ORIGIN] == "https://example.com" |
| 45 | + assert resp.content == b"Decorated: hello" |
| 46 | + |
| 47 | + async def test_async_get_not_in_allowed_origins(self): |
| 48 | + resp = await self.async_client.get( |
| 49 | + "/async-decorated/hello/", |
| 50 | + origin="https://example.org", |
| 51 | + ) |
| 52 | + assert ACCESS_CONTROL_ALLOW_ORIGIN not in resp |
| 53 | + assert resp.content == b"Async Decorated: hello" |
| 54 | + |
| 55 | + async def test_async_get_in_allowed_origins_preflight(self): |
| 56 | + resp = await self.async_client.options( |
| 57 | + "/async-decorated/hello/", |
| 58 | + origin="https://example.com", |
| 59 | + access_control_request_method="GET", |
| 60 | + ) |
| 61 | + assert resp[ACCESS_CONTROL_ALLOW_ORIGIN] == "https://example.com" |
| 62 | + assert resp.content == b"" |
| 63 | + |
| 64 | + async def test_async_get_in_allowed_origins(self): |
| 65 | + resp = await self.async_client.get( |
| 66 | + "/async-decorated/hello/", |
| 67 | + origin="https://example.com", |
| 68 | + ) |
| 69 | + assert resp[ACCESS_CONTROL_ALLOW_ORIGIN] == "https://example.com" |
| 70 | + assert resp.content == b"Async Decorated: hello" |
| 71 | + |
| 72 | + |
| 73 | +class CorsDecoratorsWithConfTestCase(TestCase): |
| 74 | + def test_get_no_origin(self): |
| 75 | + resp = self.client.get("/decorated-with-conf/hello/") |
| 76 | + assert ACCESS_CONTROL_ALLOW_ORIGIN not in resp |
| 77 | + assert resp.content == b"Decorated (with conf): hello" |
| 78 | + |
| 79 | + def test_get_not_in_allowed_origins(self): |
| 80 | + resp = self.client.get( |
| 81 | + "/decorated-with-conf/hello/", HTTP_ORIGIN="https://example.net" |
| 82 | + ) |
| 83 | + assert ACCESS_CONTROL_ALLOW_ORIGIN not in resp |
| 84 | + assert resp.content == b"Decorated (with conf): hello" |
| 85 | + |
| 86 | + def test_get_in_allowed_origins_preflight(self): |
| 87 | + resp = self.client.options( |
| 88 | + "/decorated-with-conf/hello/", |
| 89 | + HTTP_ORIGIN="https://example.com", |
| 90 | + HTTP_ACCESS_CONTROL_REQUEST_METHOD="GET", |
| 91 | + ) |
| 92 | + assert resp[ACCESS_CONTROL_ALLOW_ORIGIN] == "https://example.com" |
| 93 | + assert resp.content == b"Decorated (with conf): hello" |
| 94 | + |
| 95 | + def test_get_in_allowed_origins(self): |
| 96 | + resp = self.client.get( |
| 97 | + "/decorated-with-conf/hello/", |
| 98 | + HTTP_ORIGIN="https://example.com", |
| 99 | + ) |
| 100 | + assert resp[ACCESS_CONTROL_ALLOW_ORIGIN] == "https://example.com" |
| 101 | + assert resp.content == b"Decorated (with conf): hello" |
| 102 | + |
| 103 | + async def test_async_get_not_in_allowed_origins(self): |
| 104 | + resp = await self.async_client.get( |
| 105 | + "/async-decorated-with-conf/hello/", |
| 106 | + origin="https://example.org", |
| 107 | + ) |
| 108 | + assert ACCESS_CONTROL_ALLOW_ORIGIN not in resp |
| 109 | + assert resp.content == b"Async Decorated (with conf): hello" |
| 110 | + |
| 111 | + async def test_async_get_in_allowed_origins_preflight(self): |
| 112 | + resp = await self.async_client.options( |
| 113 | + "/async-decorated-with-conf/hello/", |
| 114 | + origin="https://example.com", |
| 115 | + access_control_request_method="GET", |
| 116 | + ) |
| 117 | + assert resp[ACCESS_CONTROL_ALLOW_ORIGIN] == "https://example.com" |
| 118 | + assert resp.content == b"" |
| 119 | + |
| 120 | + async def test_async_get_in_allowed_origins(self): |
| 121 | + resp = await self.async_client.get( |
| 122 | + "/async-decorated-with-conf/hello/", |
| 123 | + origin="https://example.com", |
| 124 | + ) |
| 125 | + assert resp[ACCESS_CONTROL_ALLOW_ORIGIN] == "https://example.com" |
| 126 | + assert resp.content == b"Async Decorated (with conf): hello" |
0 commit comments