-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #194 from Materials-Consortia/add_CORS-middleware
Add CORSMiddleware to both server implementations. Use "default" settings, i.e., allow origin from anywhere (`'*'`). Allow _only_ method `GET`.
- Loading branch information
Showing
3 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# pylint: disable=relative-beyond-top-level | ||
import unittest | ||
|
||
from .utils import SetClient | ||
|
||
|
||
class CORSMiddlewareTest(SetClient, unittest.TestCase): | ||
|
||
server = "regular" | ||
|
||
def test_regular_CORS_request(self): | ||
response = self.client.get("/info", headers={"Origin": "http://example.org"}) | ||
self.assertIn( | ||
("access-control-allow-origin", "*"), | ||
tuple(response.headers.items()), | ||
msg=f"Access-Control-Allow-Origin header not found in response headers: {response.headers}", | ||
) | ||
|
||
def test_preflight_CORS_request(self): | ||
headers = { | ||
"Origin": "http://example.org", | ||
"Access-Control-Request-Method": "GET", | ||
} | ||
response = self.client.options("/info", headers=headers) | ||
for response_header in ( | ||
"Access-Control-Allow-Origin", | ||
"Access-Control-Allow-Methods", | ||
): | ||
self.assertIn( | ||
response_header.lower(), | ||
list(response.headers.keys()), | ||
msg=f"{response_header} header not found in response headers: {response.headers}", | ||
) | ||
|
||
|
||
class IndexCORSMiddlewareTest(SetClient, unittest.TestCase): | ||
|
||
server = "index" | ||
|
||
def test_regular_CORS_request(self): | ||
response = self.client.get("/info", headers={"Origin": "http://example.org"}) | ||
self.assertIn( | ||
("access-control-allow-origin", "*"), | ||
tuple(response.headers.items()), | ||
msg=f"Access-Control-Allow-Origin header not found in response headers: {response.headers}", | ||
) | ||
|
||
def test_preflight_CORS_request(self): | ||
headers = { | ||
"Origin": "http://example.org", | ||
"Access-Control-Request-Method": "GET", | ||
} | ||
response = self.client.options("/info", headers=headers) | ||
for response_header in ( | ||
"Access-Control-Allow-Origin", | ||
"Access-Control-Allow-Methods", | ||
): | ||
self.assertIn( | ||
response_header.lower(), | ||
list(response.headers.keys()), | ||
msg=f"{response_header} header not found in response headers: {response.headers}", | ||
) |