Skip to content

Commit 6e9e52a

Browse files
committed
Address Copilot CORS review feedback
1 parent 196cd67 commit 6e9e52a

3 files changed

Lines changed: 13 additions & 26 deletions

File tree

src/nodenorm/handlers/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
import tornado.web
55

66
import nodenorm
7-
from nodenorm.handlers.conflations import ValidConflationsHandler
8-
from nodenorm.handlers.health import NodeNormHealthHandler
9-
from nodenorm.handlers.normalized_nodes import NormalizedNodesHandler
10-
from nodenorm.handlers.semantic_types import SemanticTypeHandler
11-
from nodenorm.handlers.set_identifiers import SetIdentifierHandler
12-
from nodenorm.handlers.version import VersionHandler
137

148

159
def build_handlers() -> dict[str, tuple[str, Callable]]:
1610
"""Generate our handler mapping for the nodenorm API."""
11+
from nodenorm.handlers.conflations import ValidConflationsHandler
12+
from nodenorm.handlers.health import NodeNormHealthHandler
13+
from nodenorm.handlers.normalized_nodes import NormalizedNodesHandler
14+
from nodenorm.handlers.semantic_types import SemanticTypeHandler
15+
from nodenorm.handlers.set_identifiers import SetIdentifierHandler
16+
from nodenorm.handlers.version import VersionHandler
1717

1818
handler_collection = [
1919
(r"/get_allowed_conflations?", ValidConflationsHandler),

src/nodenorm/handlers/base.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
class NodeNormalizationBaseHandler(BaseHandler):
77
"""Base handler that keeps the lightweight BioThings handler plus CORS."""
88

9+
cors_origin = "*"
10+
cors_allow_credentials = "false"
911
cors_methods = "GET, POST, HEAD, OPTIONS"
1012
cors_max_age = "600"
1113

@@ -16,12 +18,11 @@ def set_default_headers(self):
1618

1719
requested_headers = self.request.headers.get("Access-Control-Request-Headers")
1820

19-
self.set_header("Access-Control-Allow-Origin", origin)
20-
self.set_header("Access-Control-Allow-Credentials", "true")
21+
self.set_header("Access-Control-Allow-Origin", self.cors_origin)
22+
self.set_header("Access-Control-Allow-Credentials", self.cors_allow_credentials)
2123
self.set_header("Access-Control-Allow-Methods", self.cors_methods)
2224
self.set_header("Access-Control-Allow-Headers", requested_headers or "*")
2325
self.set_header("Access-Control-Max-Age", self.cors_max_age)
24-
self.set_header("Vary", "Origin")
2526

2627
def options(self, *args, **kwargs):
2728
self.finish()

tests/test_cors.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,9 @@
1-
import importlib.util
2-
from pathlib import Path
3-
41
import tornado.web
52
from tornado.testing import AsyncHTTPTestCase
63

4+
from nodenorm.handlers.base import NodeNormalizationBaseHandler
75

86
ORIGIN = "https://translatorsri.github.io"
9-
BASE_HANDLER_PATH = Path(__file__).parents[1] / "src" / "nodenorm" / "handlers" / "base.py"
10-
11-
12-
def load_base_handler():
13-
spec = importlib.util.spec_from_file_location("_nodenorm_base_handler_under_test", BASE_HANDLER_PATH)
14-
module = importlib.util.module_from_spec(spec)
15-
spec.loader.exec_module(module)
16-
return module.NodeNormalizationBaseHandler
17-
18-
19-
NodeNormalizationBaseHandler = load_base_handler()
207

218

229
class PreflightHandler(NodeNormalizationBaseHandler):
@@ -25,12 +12,11 @@ async def get(self):
2512

2613

2714
def assert_cors_headers(headers, allowed_headers="*"):
28-
assert headers["Access-Control-Allow-Origin"] == ORIGIN
29-
assert headers["Access-Control-Allow-Credentials"] == "true"
15+
assert headers["Access-Control-Allow-Origin"] == "*"
16+
assert headers["Access-Control-Allow-Credentials"] == "false"
3017
assert headers["Access-Control-Allow-Methods"] == "GET, POST, HEAD, OPTIONS"
3118
assert headers["Access-Control-Allow-Headers"] == allowed_headers
3219
assert headers["Access-Control-Max-Age"] == "600"
33-
assert headers["Vary"] == "Origin"
3420

3521

3622
class TestCorsHeaders(AsyncHTTPTestCase):

0 commit comments

Comments
 (0)