Skip to content

Instantly share code, notes, and snippets.

@niconoe
Last active March 4, 2022 10:31
Show Gist options
  • Save niconoe/3337869efdb9a734b6dfee99f6840483 to your computer and use it in GitHub Desktop.
Save niconoe/3337869efdb9a734b6dfee99f6840483 to your computer and use it in GitHub Desktop.
Test CORS is enabled via the Django test client
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