Spaces:
Running
Running
Download tests/api/test_security_headers.py from Arrechenash/stocks: direct link, hf CLI and curl.
- Browser
- Download file 1.58 kB
-
https://huggingface.co/spaces/Arrechenash/stocks/resolve/main/tests/api/test_security_headers.py
- Command line
-
hf download hf://spaces/Arrechenash/stocks/tests/api/test_security_headers.py
-
curl -L -o test_security_headers.py https://huggingface.co/spaces/Arrechenash/stocks/resolve/main/tests/api/test_security_headers.py
1.58 kB
| """Security headers on every response (app/middleware/security_headers.py). | |
| These are defense-in-depth against content sniffing, clickjacking and | |
| referrer leakage — the assertions pin presence and value, not placement. | |
| """ | |
| import pytest | |
| from fastapi import FastAPI | |
| from fastapi.testclient import TestClient | |
| from app.middleware.security_headers import SecurityHeadersMiddleware | |
| def client() -> TestClient: | |
| app = FastAPI() | |
| app.add_middleware(SecurityHeadersMiddleware) | |
| def anything() -> dict[str, bool]: | |
| return {"ok": True} | |
| return TestClient(app) | |
| def test_headers_present_on_json_response(client: TestClient) -> None: | |
| resp = client.get("/anything") | |
| assert resp.headers["X-Content-Type-Options"] == "nosniff" | |
| assert resp.headers["X-Frame-Options"] == "DENY" | |
| assert resp.headers["Referrer-Policy"] == "strict-origin-when-cross-origin" | |
| def test_headers_do_not_override_existing_values(client: TestClient) -> None: | |
| app = FastAPI() | |
| app.add_middleware(SecurityHeadersMiddleware) | |
| def preset() -> dict[str, bool]: | |
| return {"ok": True} | |
| async def set_frame_header(request, call_next): # type: ignore[no-untyped-def] | |
| response = await call_next(request) | |
| # An endpoint that deliberately frames itself keeps its value. | |
| response.headers["X-Frame-Options"] = "SAMEORIGIN" | |
| return response | |
| with TestClient(app) as c: | |
| assert c.get("/preset").headers["X-Frame-Options"] == "SAMEORIGIN" | |