Last active
March 4, 2022 10:31
-
-
Save niconoe/3337869efdb9a734b6dfee99f6840483 to your computer and use it in GitHub Desktop.
Test CORS is enabled via the Django test client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.test import TestCase | |
class ApiTests(TestCase): | |
def test_cors_enabled(self): | |
"""Make sure CORS is enabled a given endpoint | |
# Technique inspired from https://stackoverflow.com/a/47609921 | |
""" | |
request_headers = { | |
"HTTP_ACCESS_CONTROL_REQUEST_METHOD": "GET", | |
"HTTP_ORIGIN": "http://somethingelse.com", | |
} | |
response = self.client.get( | |
"/my-api-endpoint", {}, **request_headers | |
) | |
self.assertEqual(response.headers["Access-Control-Allow-Origin"], "*") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment