Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /nijaru_zenith /tests /unit /test_static_files.py
| """ | |
| Comprehensive tests for static file serving. | |
| Tests static file serving, security features, caching headers, | |
| SPA support, and various edge cases. | |
| """ | |
| import tempfile | |
| from pathlib import Path | |
| from unittest.mock import patch | |
| import pytest | |
| from starlette.responses import Response | |
| from zenith.web.static import ( | |
| SPAStaticFiles, | |
| StaticFileConfig, | |
| ZenithStaticFiles, | |
| create_static_route, | |
| serve_css_js, | |
| serve_images, | |
| serve_spa_files, | |
| serve_uploads, | |
| ) | |
| class TestStaticFileConfig: | |
| """Test StaticFileConfig configuration.""" | |
| def test_default_config(self): | |
| """Test default configuration values.""" | |
| config = StaticFileConfig(directory="public") | |
| assert config.directory == "public" | |
| assert config.max_age == 3600 | |
| assert config.etag is True | |
| assert config.last_modified is True | |
| assert config.allow_hidden is False | |
| assert config.allowed_extensions is None | |
| def test_custom_config(self): | |
| """Test custom configuration values.""" | |
| config = StaticFileConfig( | |
| directory="assets", | |
| max_age=86400, | |
| etag=False, | |
| allow_hidden=True, | |
| allowed_extensions=[".css", ".js"], | |
| ) | |
| assert config.directory == "assets" | |
| assert config.max_age == 86400 | |
| assert config.etag is False | |
| assert config.allow_hidden is True | |
| assert config.allowed_extensions == [".css", ".js"] | |
| class TestZenithStaticFiles: | |
| """Test ZenithStaticFiles enhanced static file handler.""" | |
| def create_test_file( | |
| self, directory: Path, filename: str, content: str = "test" | |
| ) -> Path: | |
| """Helper to create a test file.""" | |
| file_path = directory / filename | |
| file_path.parent.mkdir(parents=True, exist_ok=True) | |
| file_path.write_text(content) | |
| return file_path | |
| def test_file_response_basic(self): | |
| """Test basic file response creation.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| # Create a test file | |
| test_file = self.create_test_file(Path(tmpdir), "test.txt") | |
| config = StaticFileConfig(directory=tmpdir) | |
| static_files = ZenithStaticFiles(config) | |
| # Mock stat result | |
| stat_result = Path(test_file).stat() | |
| scope = {"type": "http", "method": "GET"} | |
| response = static_files.file_response(str(test_file), stat_result, scope) | |
| assert response.status_code == 200 | |
| assert "cache-control" in response.headers | |
| assert "etag" in response.headers | |
| assert "last-modified" in response.headers | |
| assert response.headers.get("x-content-type-options") == "nosniff" | |
| def test_hidden_file_blocked(self): | |
| """Test hidden files are blocked by default.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| # Create a hidden file | |
| test_file = self.create_test_file(Path(tmpdir), ".hidden.txt") | |
| config = StaticFileConfig(directory=tmpdir, allow_hidden=False) | |
| static_files = ZenithStaticFiles(config) | |
| stat_result = Path(test_file).stat() | |
| scope = {"type": "http", "method": "GET"} | |
| response = static_files.file_response(str(test_file), stat_result, scope) | |
| assert response.status_code == 404 | |
| def test_hidden_file_allowed(self): | |
| """Test hidden files can be allowed.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| # Create a hidden file | |
| test_file = self.create_test_file(Path(tmpdir), ".env") | |
| config = StaticFileConfig(directory=tmpdir, allow_hidden=True) | |
| static_files = ZenithStaticFiles(config) | |
| stat_result = Path(test_file).stat() | |
| scope = {"type": "http", "method": "GET"} | |
| response = static_files.file_response(str(test_file), stat_result, scope) | |
| assert response.status_code == 200 | |
| def test_extension_filtering(self): | |
| """Test file extension filtering.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| # Create files with different extensions | |
| css_file = self.create_test_file(Path(tmpdir), "style.css") | |
| php_file = self.create_test_file(Path(tmpdir), "script.php") | |
| config = StaticFileConfig( | |
| directory=tmpdir, allowed_extensions=[".css", ".js", ".png"] | |
| ) | |
| static_files = ZenithStaticFiles(config) | |
| scope = {"type": "http", "method": "GET"} | |
| # CSS file should be allowed | |
| css_response = static_files.file_response( | |
| str(css_file), Path(css_file).stat(), scope | |
| ) | |
| assert css_response.status_code == 200 | |
| # PHP file should be blocked | |
| php_response = static_files.file_response( | |
| str(php_file), Path(php_file).stat(), scope | |
| ) | |
| assert php_response.status_code == 404 | |
| def test_cache_headers(self): | |
| """Test cache control headers.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| test_file = self.create_test_file(Path(tmpdir), "app.js") | |
| # Test with custom max_age | |
| config = StaticFileConfig(directory=tmpdir, max_age=7200) | |
| static_files = ZenithStaticFiles(config) | |
| stat_result = Path(test_file).stat() | |
| scope = {"type": "http", "method": "GET"} | |
| response = static_files.file_response(str(test_file), stat_result, scope) | |
| assert response.headers.get("cache-control") == "public, max-age=7200" | |
| def test_no_cache(self): | |
| """Test disabling cache.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| test_file = self.create_test_file(Path(tmpdir), "dynamic.json") | |
| config = StaticFileConfig(directory=tmpdir, max_age=0) | |
| static_files = ZenithStaticFiles(config) | |
| stat_result = Path(test_file).stat() | |
| scope = {"type": "http", "method": "GET"} | |
| response = static_files.file_response(str(test_file), stat_result, scope) | |
| # No cache-control header when max_age is 0 | |
| assert "cache-control" not in response.headers | |
| def test_etag_generation(self): | |
| """Test ETag generation.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| test_file = self.create_test_file(Path(tmpdir), "data.json") | |
| config = StaticFileConfig(directory=tmpdir, etag=True) | |
| static_files = ZenithStaticFiles(config) | |
| stat_result = Path(test_file).stat() | |
| scope = {"type": "http", "method": "GET"} | |
| response = static_files.file_response(str(test_file), stat_result, scope) | |
| assert "etag" in response.headers | |
| # ETag should be quoted | |
| assert response.headers["etag"].startswith('"') | |
| assert response.headers["etag"].endswith('"') | |
| def test_no_etag(self): | |
| """Test disabling ETag in config (note: FileResponse may still add its own).""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| test_file = self.create_test_file(Path(tmpdir), "file.txt") | |
| config = StaticFileConfig(directory=tmpdir, etag=False) | |
| static_files = ZenithStaticFiles(config) | |
| stat_result = Path(test_file).stat() | |
| scope = {"type": "http", "method": "GET"} | |
| static_files.file_response(str(test_file), stat_result, scope) | |
| # When etag=False in config, we don't add our own etag | |
| # but Starlette's FileResponse may add its own | |
| # The test should verify our config is respected | |
| assert config.etag is False | |
| def test_last_modified_header(self): | |
| """Test Last-Modified header.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| test_file = self.create_test_file(Path(tmpdir), "doc.pdf") | |
| config = StaticFileConfig(directory=tmpdir, last_modified=True) | |
| static_files = ZenithStaticFiles(config) | |
| stat_result = Path(test_file).stat() | |
| scope = {"type": "http", "method": "GET"} | |
| response = static_files.file_response(str(test_file), stat_result, scope) | |
| assert "last-modified" in response.headers | |
| # Should be in HTTP date format | |
| assert "GMT" in response.headers["last-modified"] | |
| def test_no_last_modified(self): | |
| """Test disabling Last-Modified in config (note: FileResponse may still add its own).""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| test_file = self.create_test_file(Path(tmpdir), "file.txt") | |
| config = StaticFileConfig(directory=tmpdir, last_modified=False) | |
| static_files = ZenithStaticFiles(config) | |
| stat_result = Path(test_file).stat() | |
| scope = {"type": "http", "method": "GET"} | |
| static_files.file_response(str(test_file), stat_result, scope) | |
| # When last_modified=False in config, we don't add our own header | |
| # but Starlette's FileResponse may add its own | |
| # The test should verify our config is respected | |
| assert config.last_modified is False | |
| class TestSPAStaticFiles: | |
| """Test SPA static file serving.""" | |
| def test_spa_fallback_to_index(self): | |
| """Test SPA fallback to index.html.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| # Create index.html | |
| index_file = Path(tmpdir) / "index.html" | |
| index_file.write_text("<html>SPA</html>") | |
| config = StaticFileConfig(directory=tmpdir, html=True) | |
| spa_files = SPAStaticFiles(config) | |
| # Test that fallback works for non-existent route | |
| assert spa_files._should_fallback("/app/route") | |
| def test_spa_exclude_patterns(self): | |
| """Test SPA exclusion patterns.""" | |
| config = StaticFileConfig(directory=".", html=True) | |
| spa_files = SPAStaticFiles(config, exclude=["/api/*", "/admin/*", "/health"]) | |
| # These should not fallback | |
| assert not spa_files._should_fallback("/api/users") | |
| assert not spa_files._should_fallback("/api/products/123") | |
| assert not spa_files._should_fallback("/admin/dashboard") | |
| assert not spa_files._should_fallback("/health") | |
| # These should fallback | |
| assert spa_files._should_fallback("/app/dashboard") | |
| assert spa_files._should_fallback("/login") | |
| assert spa_files._should_fallback("/products/123") | |
| def test_spa_custom_index(self): | |
| """Test SPA with custom index file.""" | |
| config = StaticFileConfig(directory=".", html=True) | |
| spa_files = SPAStaticFiles(config, index="app.html") | |
| assert spa_files.index == "app.html" | |
| async def test_spa_get_response_fallback(self): | |
| """Test SPA get_response with fallback.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| # Create index.html | |
| index_file = Path(tmpdir) / "index.html" | |
| index_file.write_text("<html>SPA App</html>") | |
| config = StaticFileConfig(directory=tmpdir, html=True) | |
| spa_files = SPAStaticFiles(config) | |
| scope = {"type": "http", "method": "GET"} | |
| # Mock the parent class to simulate 404 | |
| with patch.object(ZenithStaticFiles, "get_response") as mock_get: | |
| # First call returns 404, second returns index | |
| mock_get.side_effect = [ | |
| Response(status_code=404), | |
| Response(content=b"<html>SPA App</html>", status_code=200), | |
| ] | |
| response = await spa_files.get_response("/app/route", scope) | |
| assert response.status_code == 200 | |
| async def test_spa_no_fallback_for_excluded(self): | |
| """Test SPA doesn't fallback for excluded paths.""" | |
| config = StaticFileConfig(directory=".", html=True) | |
| spa_files = SPAStaticFiles(config, exclude=["/api/*"]) | |
| scope = {"type": "http", "method": "GET"} | |
| with patch.object(ZenithStaticFiles, "get_response") as mock_get: | |
| mock_get.return_value = Response(status_code=404) | |
| response = await spa_files.get_response("/api/users", scope) | |
| assert response.status_code == 404 | |
| # Should only call once, no fallback attempt | |
| assert mock_get.call_count == 1 | |
| class TestStaticRouteCreation: | |
| """Test static route creation helpers.""" | |
| def test_create_static_route(self): | |
| """Test creating a static route.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| route = create_static_route( | |
| path="/static", directory=tmpdir, name="static", max_age=7200 | |
| ) | |
| assert route.path == "/static" | |
| assert route.name == "static" | |
| def test_serve_css_js(self): | |
| """Test CSS/JS serving helper.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| route = serve_css_js(directory=tmpdir) | |
| assert route.path == "/assets" | |
| assert route.name == "assets" | |
| def test_serve_images(self): | |
| """Test image serving helper.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| route = serve_images(directory=tmpdir) | |
| assert route.path == "/images" | |
| assert route.name == "images" | |
| def test_serve_uploads(self): | |
| """Test upload serving helper.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| route = serve_uploads(directory=tmpdir) | |
| assert route.path == "/uploads" | |
| assert route.name == "uploads" | |
| def test_serve_spa_files(self): | |
| """Test SPA file serving helper.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| spa_app = serve_spa_files( | |
| directory=tmpdir, index="app.html", exclude=["/api/*"], max_age=300 | |
| ) | |
| assert isinstance(spa_app, SPAStaticFiles) | |
| assert spa_app.index == "app.html" | |
| assert spa_app.exclude_patterns == ["/api/*"] | |
| assert spa_app.config.max_age == 300 | |
| class TestSecurityFeatures: | |
| """Test security features of static file serving.""" | |
| def test_path_traversal_protection(self): | |
| """Test protection against path traversal attacks.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| # Create a file outside the static directory | |
| secret_file = Path(tmpdir).parent / "secret.txt" | |
| if not secret_file.parent.exists(): | |
| secret_file.parent.mkdir(parents=True, exist_ok=True) | |
| secret_file.write_text("secret data") | |
| config = StaticFileConfig(directory=tmpdir) | |
| ZenithStaticFiles(config) | |
| # Attempt to access file outside directory with path traversal | |
| # This should be handled by the underlying StaticFiles implementation | |
| # Our code adds additional security headers | |
| def test_security_headers(self): | |
| """Test security headers are added.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| test_file = Path(tmpdir) / "test.js" | |
| test_file.write_text("console.log('test');") | |
| config = StaticFileConfig(directory=tmpdir) | |
| static_files = ZenithStaticFiles(config) | |
| stat_result = Path(test_file).stat() | |
| scope = {"type": "http", "method": "GET"} | |
| response = static_files.file_response(str(test_file), stat_result, scope) | |
| # Check for security headers | |
| assert response.headers.get("x-content-type-options") == "nosniff" | |
| def test_extension_validation_normalization(self): | |
| """Test extension validation with different formats.""" | |
| config = StaticFileConfig( | |
| directory=".", | |
| allowed_extensions=["css", "js", ".png", ".JPG"], # Mixed formats | |
| ) | |
| static_files = ZenithStaticFiles(config) | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| # Test normalization handles both with/without dots | |
| css_file = Path(tmpdir) / "style.css" | |
| css_file.write_text("body{}") | |
| jpg_file = Path(tmpdir) / "image.JPG" | |
| jpg_file.write_text("") | |
| scope = {"type": "http", "method": "GET"} | |
| # CSS should work (no dot in config) | |
| response = static_files.file_response( | |
| str(css_file), Path(css_file).stat(), scope | |
| ) | |
| assert response.status_code == 200 | |
| # JPG should work (uppercase in config) | |
| response = static_files.file_response( | |
| str(jpg_file), Path(jpg_file).stat(), scope | |
| ) | |
| assert response.status_code == 200 | |
| class TestEdgeCases: | |
| """Test edge cases and error handling.""" | |
| def test_empty_directory(self): | |
| """Test serving from empty directory.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| config = StaticFileConfig(directory=tmpdir) | |
| static_files = ZenithStaticFiles(config) | |
| # Should handle empty directory gracefully | |
| assert static_files.config.directory == tmpdir | |
| def test_nonexistent_directory(self): | |
| """Test configuration with non-existent directory.""" | |
| # Should not fail on creation, only on actual serving | |
| config = StaticFileConfig( | |
| directory="/nonexistent/path", | |
| check_dir=False, # Disable directory check | |
| ) | |
| static_files = ZenithStaticFiles(config) | |
| assert static_files.config.directory == "/nonexistent/path" | |
| def test_large_file_handling(self): | |
| """Test handling of large files.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| # Create a large file (1MB) | |
| large_file = Path(tmpdir) / "large.bin" | |
| large_file.write_bytes(b"x" * (1024 * 1024)) | |
| config = StaticFileConfig(directory=tmpdir) | |
| static_files = ZenithStaticFiles(config) | |
| stat_result = Path(large_file).stat() | |
| scope = {"type": "http", "method": "GET"} | |
| response = static_files.file_response(str(large_file), stat_result, scope) | |
| assert response.status_code == 200 | |
| def test_special_characters_in_filename(self): | |
| """Test handling files with special characters.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| # Create file with special characters (that are valid in filesystem) | |
| special_file = Path(tmpdir) / "file-with-dashes_and_underscores.txt" | |
| special_file.write_text("content") | |
| config = StaticFileConfig(directory=tmpdir) | |
| static_files = ZenithStaticFiles(config) | |
| stat_result = Path(special_file).stat() | |
| scope = {"type": "http", "method": "GET"} | |
| response = static_files.file_response(str(special_file), stat_result, scope) | |
| assert response.status_code == 200 | |
| def test_mime_type_detection(self): | |
| """Test MIME type detection for various file types.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| files = { | |
| "test.html": "text/html", | |
| "style.css": "text/css", | |
| "script.js": [ | |
| "application/javascript", | |
| "text/javascript", | |
| ], # Both are valid | |
| "data.json": "application/json", | |
| "image.png": "image/png", | |
| "document.pdf": "application/pdf", | |
| } | |
| config = StaticFileConfig(directory=tmpdir) | |
| static_files = ZenithStaticFiles(config) | |
| scope = {"type": "http", "method": "GET"} | |
| for filename, expected_mime in files.items(): | |
| file_path = Path(tmpdir) / filename | |
| file_path.write_text("test content") | |
| response = static_files.file_response( | |
| str(file_path), Path(file_path).stat(), scope | |
| ) | |
| # mimetypes module should detect the correct type | |
| if response.headers.get("content-type"): | |
| content_type = response.headers["content-type"] | |
| if isinstance(expected_mime, list): | |
| # Any of the expected types is valid | |
| assert any(mime in content_type for mime in expected_mime) | |
| else: | |
| assert expected_mime in content_type | |
Xet Storage Details
- Size:
- 20.6 kB
- Xet hash:
- f863fb7e9056213dd3471bb84b619a8f2f552c117a7a7d9df3844417a4fd18c4
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.