Buckets:
| """ | |
| Tests for P8s middleware module. | |
| """ | |
| from unittest.mock import AsyncMock, MagicMock, patch | |
| import pytest | |
| class TestMiddlewareImport: | |
| """Test middleware module imports.""" | |
| def test_module_imports(self): | |
| """Test that middleware module can be imported.""" | |
| from p8s import middleware | |
| assert middleware is not None | |
| class TestCORSMiddleware: | |
| """Test CORS middleware configuration.""" | |
| def test_cors_can_be_configured(self): | |
| """Test that CORS settings are configurable.""" | |
| # This tests that CORS is set up via the FastAPI/Starlette middleware | |
| cors_config = { | |
| "allow_origins": ["http://localhost:3000"], | |
| "allow_credentials": True, | |
| "allow_methods": ["*"], | |
| "allow_headers": ["*"], | |
| } | |
| assert "allow_origins" in cors_config | |
| assert "allow_credentials" in cors_config | |
| class TestTimingMiddleware: | |
| """Test request timing middleware.""" | |
| def test_timing_header_concept(self): | |
| """Test timing header concept (X-Response-Time).""" | |
| import time | |
| start = time.perf_counter() | |
| # Simulate some work | |
| time.sleep(0.001) | |
| elapsed = time.perf_counter() - start | |
| # Should be very fast (under 1 second for a mock) | |
| assert elapsed < 1.0 | |
| def test_timing_format(self): | |
| """Test timing format is in milliseconds.""" | |
| elapsed_seconds = 0.0123 # ~12.3ms | |
| elapsed_ms = elapsed_seconds * 1000 | |
| # Format as "12.30ms" | |
| formatted = f"{elapsed_ms:.2f}ms" | |
| assert formatted == "12.30ms" | |
| class TestSecurityHeaders: | |
| """Test security headers middleware.""" | |
| def test_security_headers_config(self): | |
| """Test expected security headers.""" | |
| expected_headers = { | |
| "X-Content-Type-Options": "nosniff", | |
| "X-Frame-Options": "DENY", | |
| "X-XSS-Protection": "1; mode=block", | |
| "Referrer-Policy": "strict-origin-when-cross-origin", | |
| } | |
| # All standard security headers should be defined | |
| assert "X-Content-Type-Options" in expected_headers | |
| assert "X-Frame-Options" in expected_headers | |
| assert "X-XSS-Protection" in expected_headers | |
| def test_csp_header_format(self): | |
| """Test Content-Security-Policy format.""" | |
| csp_directives = [ | |
| "default-src 'self'", | |
| "script-src 'self' 'unsafe-inline'", | |
| "style-src 'self' 'unsafe-inline'", | |
| ] | |
| csp_header = "; ".join(csp_directives) | |
| assert "default-src 'self'" in csp_header | |
| assert "script-src 'self'" in csp_header | |
| class TestRequestIDMiddleware: | |
| """Test request ID middleware.""" | |
| def test_request_id_generation(self): | |
| """Test unique request ID generation.""" | |
| import uuid | |
| id1 = str(uuid.uuid4()) | |
| id2 = str(uuid.uuid4()) | |
| assert id1 != id2 | |
| assert len(id1) == 36 # UUID format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | |
| def test_request_id_header_name(self): | |
| """Test expected request ID header name.""" | |
| header_name = "X-Request-ID" | |
| assert header_name.startswith("X-") | |
| assert "Request" in header_name | |
| class TestGzipMiddleware: | |
| """Test Gzip compression middleware.""" | |
| def test_gzip_content_types(self): | |
| """Test which content types should be compressed.""" | |
| compressible = [ | |
| "text/html", | |
| "text/css", | |
| "text/javascript", | |
| "application/javascript", | |
| "application/json", | |
| ] | |
| non_compressible = [ | |
| "image/png", | |
| "image/jpeg", | |
| "application/octet-stream", | |
| ] | |
| for ct in compressible: | |
| assert "text" in ct or "json" in ct or "javascript" in ct | |
| def test_minimum_size_threshold(self): | |
| """Test minimum size for compression.""" | |
| # Don't compress small responses (overhead not worth it) | |
| min_size = 500 # bytes | |
| small_response = "Hello" | |
| large_response = "x" * 1000 | |
| assert len(small_response) < min_size | |
| assert len(large_response) > min_size | |
| class TestExceptionHandlerMiddleware: | |
| """Test exception handling middleware.""" | |
| def test_http_exception_response_format(self): | |
| """Test HTTP exception response format.""" | |
| error_response = { | |
| "error": True, | |
| "message": "Not Found", | |
| "status_code": 404, | |
| } | |
| assert error_response["error"] is True | |
| assert "message" in error_response | |
| assert "status_code" in error_response | |
| def test_validation_error_format(self): | |
| """Test validation error response format.""" | |
| validation_error = { | |
| "error": True, | |
| "message": "Validation Error", | |
| "status_code": 422, | |
| "details": [ | |
| {"field": "email", "message": "Invalid email format"}, | |
| ], | |
| } | |
| assert validation_error["status_code"] == 422 | |
| assert "details" in validation_error | |
| assert len(validation_error["details"]) > 0 | |
| def test_internal_error_hides_details(self): | |
| """Test that internal errors don't expose sensitive details in production.""" | |
| # In production, we should not expose stack traces | |
| production_error = { | |
| "error": True, | |
| "message": "Internal Server Error", | |
| "status_code": 500, | |
| } | |
| # Should NOT contain stack trace or internal details | |
| assert "traceback" not in production_error | |
| assert "file" not in production_error | |
| class TestRateLimitMiddleware: | |
| """Test rate limiting middleware.""" | |
| def test_rate_limit_import(self): | |
| """Test RateLimitMiddleware can be imported.""" | |
| from p8s.middleware import RateLimitMiddleware | |
| assert RateLimitMiddleware is not None | |
| def test_rate_parsing(self): | |
| """Test rate string parsing.""" | |
| from p8s.middleware import RateLimitMiddleware | |
| middleware = RateLimitMiddleware(rate="100/minute") | |
| assert middleware.limit == 100 | |
| assert middleware.period == 60 | |
| middleware = RateLimitMiddleware(rate="1000/hour") | |
| assert middleware.limit == 1000 | |
| assert middleware.period == 3600 | |
| def test_rate_parsing_invalid(self): | |
| """Test invalid rate format raises error.""" | |
| from p8s.middleware import RateLimitMiddleware | |
| with pytest.raises(ValueError): | |
| RateLimitMiddleware(rate="invalid") | |
| with pytest.raises(ValueError): | |
| RateLimitMiddleware(rate="100/invalid_period") | |
| def test_exempt_paths_config(self): | |
| """Test exempt paths configuration.""" | |
| from p8s.middleware import RateLimitMiddleware | |
| middleware = RateLimitMiddleware( | |
| rate="100/minute", | |
| exempt_paths=["/health", "/metrics"], | |
| ) | |
| assert "/health" in middleware.exempt_paths | |
| assert "/metrics" in middleware.exempt_paths | |
| def test_rate_limit_headers(self): | |
| """Test expected rate limit headers.""" | |
| expected_headers = [ | |
| "X-RateLimit-Limit", | |
| "X-RateLimit-Remaining", | |
| "X-RateLimit-Reset", | |
| "Retry-After", | |
| ] | |
| # Verify header names are valid | |
| for header in expected_headers: | |
| assert header.startswith("X-") or header == "Retry-After" | |
Xet Storage Details
- Size:
- 7.44 kB
- Xet hash:
- b7083e4e261115842249cd02b5a74cb1c16c6678f89cc167aa270915ee12e89c
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.