|
| 1 | +""" |
| 2 | +Test for HSTS header presence in responses. |
| 3 | +""" |
| 4 | +import pytest |
| 5 | +from fastapi.testclient import TestClient |
| 6 | +from api.index import app |
| 7 | + |
| 8 | + |
| 9 | +class TestHSTSHeader: |
| 10 | + """Test HSTS security header.""" |
| 11 | + |
| 12 | + @pytest.fixture |
| 13 | + def client(self): |
| 14 | + """Create a test client.""" |
| 15 | + return TestClient(app) |
| 16 | + |
| 17 | + def test_hsts_header_present(self, client): |
| 18 | + """Test that the HSTS header is present in responses.""" |
| 19 | + # Make a request to the root endpoint |
| 20 | + response = client.get("/") |
| 21 | + |
| 22 | + # Verify HSTS header is present |
| 23 | + assert "strict-transport-security" in response.headers |
| 24 | + |
| 25 | + # Verify header value contains required directives |
| 26 | + hsts_header = response.headers["strict-transport-security"] |
| 27 | + assert "max-age=31536000" in hsts_header |
| 28 | + assert "includeSubDomains" in hsts_header |
| 29 | + assert "preload" in hsts_header |
| 30 | + |
| 31 | + def test_hsts_header_on_api_endpoints(self, client): |
| 32 | + """Test that the HSTS header is present on API endpoints.""" |
| 33 | + # Test on graphs endpoint |
| 34 | + response = client.get("/graphs") |
| 35 | + |
| 36 | + # Verify HSTS header is present |
| 37 | + assert "strict-transport-security" in response.headers |
| 38 | + |
| 39 | + # Verify header value contains required directives |
| 40 | + hsts_header = response.headers["strict-transport-security"] |
| 41 | + assert "max-age=31536000" in hsts_header |
| 42 | + assert "includeSubDomains" in hsts_header |
| 43 | + assert "preload" in hsts_header |
0 commit comments