class TestCrossOriginResourceSharingMiddleware(TestCase): def setUp(self): self.middleware = CrossOriginResourceSharingMiddleware() self.request = HttpRequest() self.response = HttpResponse() def test_match(self): self.request.path = '/foo/bar/baz/' cors_urls = {r'^/foo/bar': '*'} with self.settings(CORS_URLS=cors_urls): self.middleware.process_response(self.request, self.response) self.assertEqual(self.response['Access-Control-Allow-Origin'], '*') def test_middle_match(self): # Ensure that matches in the middle of the URL work. self.request.path = '/foo/bar/baz/' cors_urls = {r'/bar': '*'} with self.settings(CORS_URLS=cors_urls): self.middleware.process_response(self.request, self.response) self.assertEqual(self.response['Access-Control-Allow-Origin'], '*') def test_no_match(self): self.request.path = '/foo/bar/baz/' cors_urls = {r'^/biff/bak': '*'} with self.settings(CORS_URLS=cors_urls): self.middleware.process_response(self.request, self.response) self.assertFalse('Access-Control-Allow-Origin' in self.response)
def setUp(self): self.middleware = CrossOriginResourceSharingMiddleware() self.request = HttpRequest() self.response = HttpResponse()