Skip to content

Commit 8c528bf

Browse files
Merge branch 'main' into feature/hexkit_v6_GSI-1712
2 parents 7fff39e + db184a1 commit 8c528bf

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/ghga_service_commons/api/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ def configure_app(app: FastAPI, config: ApiConfigBase):
361361
if config.cors_allow_credentials is not None:
362362
kwargs["allow_credentials"] = config.cors_allow_credentials
363363
if config.cors_exposed_headers is not None:
364-
kwargs["exposed_headers"] = config.cors_exposed_headers
364+
kwargs["expose_headers"] = config.cors_exposed_headers
365365

366366
app.add_middleware(RequestLoggingMiddleware)
367367
app.add_middleware(CorrelationIdMiddleware, config.generate_correlation_id)

tests/integration/test_api.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,55 @@ async def test_run_server():
6161
assert response.json() == GREETING
6262

6363

64+
async def test_configure_cors_headers():
65+
"""Test configuring the CORS headers."""
66+
# Configure the FastAPI app with nontrivial CORS settings
67+
cors_config = ApiConfigBase(
68+
cors_allowed_origins=["http://test.dev"],
69+
cors_allowed_methods=["GET"],
70+
cors_allowed_headers=["X-Custom-Request-Header"],
71+
cors_exposed_headers=["X-Custom-Response-Header"],
72+
)
73+
74+
app = FastAPI()
75+
configure_app(app, cors_config)
76+
77+
# Check that the app is configured properly
78+
client = AsyncTestClient(app)
79+
80+
# Add a simple endpoint to test CORS
81+
@app.get("/test")
82+
async def test_endpoint():
83+
return {"message": "test"}
84+
85+
# Test preflight request
86+
response = await client.options(
87+
"/test",
88+
headers={
89+
"Origin": "http://test.dev",
90+
"Access-Control-Request-Method": "GET",
91+
"Access-Control-Request-Headers": "X-Custom-Request-Header",
92+
},
93+
)
94+
95+
# Verify CORS headers are set correctly
96+
assert response.status_code == 200
97+
headers = response.headers
98+
assert headers["access-control-allow-origin"] == "http://test.dev"
99+
assert headers["access-control-allow-methods"] == "GET"
100+
allow_headers = headers["access-control-allow-headers"].split(", ")
101+
assert "X-Custom-Request-Header" in allow_headers
102+
103+
# Test actual request with CORS headers
104+
response = await client.get("/test", headers={"Origin": "http://test.dev"})
105+
106+
assert response.status_code == 200
107+
headers = response.headers
108+
assert headers["access-control-allow-origin"] == "http://test.dev"
109+
expose_headers = headers["access-control-expose-headers"].split(", ")
110+
assert "X-Custom-Response-Header" in expose_headers
111+
112+
64113
async def test_configure_exception_handler():
65114
"""Test the exception handler configuration of a FastAPI app."""
66115
# example params for an http exception

0 commit comments

Comments
 (0)