A Python port of the Invisible Internet Project (I2P)
at main 78 lines 2.8 kB view raw
1"""Tests for eepsite static file server — TDD: tests before implementation.""" 2 3import pytest 4from pathlib import Path 5 6from i2p_apps.eepsite.server import EepsiteServer, EepsiteConfig 7 8 9class TestEepsiteConfig: 10 def test_defaults(self): 11 cfg = EepsiteConfig() 12 assert cfg.host == "127.0.0.1" 13 assert cfg.port == 7658 14 assert cfg.docroot.name == "docroot" 15 16 def test_custom_port(self): 17 cfg = EepsiteConfig(port=8080) 18 assert cfg.port == 8080 19 20 def test_custom_docroot(self, tmp_path: Path): 21 cfg = EepsiteConfig(docroot=tmp_path / "www") 22 assert cfg.docroot == tmp_path / "www" 23 24 25class TestEepsiteServer: 26 def test_creation(self, tmp_path: Path): 27 docroot = tmp_path / "docroot" 28 docroot.mkdir() 29 cfg = EepsiteConfig(docroot=docroot) 30 server = EepsiteServer(cfg) 31 assert server is not None 32 33 def test_resolve_path_basic(self, tmp_path: Path): 34 docroot = tmp_path / "docroot" 35 docroot.mkdir() 36 (docroot / "index.html").write_text("<h1>Hello</h1>") 37 cfg = EepsiteConfig(docroot=docroot) 38 server = EepsiteServer(cfg) 39 path = server.resolve_path("/index.html") 40 assert path is not None 41 assert path.name == "index.html" 42 43 def test_resolve_path_default_index(self, tmp_path: Path): 44 docroot = tmp_path / "docroot" 45 docroot.mkdir() 46 (docroot / "index.html").write_text("<h1>Hello</h1>") 47 cfg = EepsiteConfig(docroot=docroot) 48 server = EepsiteServer(cfg) 49 path = server.resolve_path("/") 50 assert path is not None 51 assert path.name == "index.html" 52 53 def test_resolve_path_missing(self, tmp_path: Path): 54 docroot = tmp_path / "docroot" 55 docroot.mkdir() 56 cfg = EepsiteConfig(docroot=docroot) 57 server = EepsiteServer(cfg) 58 path = server.resolve_path("/nonexistent.html") 59 assert path is None 60 61 def test_resolve_path_traversal_blocked(self, tmp_path: Path): 62 docroot = tmp_path / "docroot" 63 docroot.mkdir() 64 cfg = EepsiteConfig(docroot=docroot) 65 server = EepsiteServer(cfg) 66 path = server.resolve_path("/../../../etc/passwd") 67 assert path is None 68 69 def test_content_type(self, tmp_path: Path): 70 docroot = tmp_path / "docroot" 71 docroot.mkdir() 72 cfg = EepsiteConfig(docroot=docroot) 73 server = EepsiteServer(cfg) 74 assert server.content_type("test.html") == "text/html" 75 assert server.content_type("style.css") == "text/css" 76 assert server.content_type("script.js") == "application/javascript" 77 assert server.content_type("image.png") == "image/png" 78 assert server.content_type("unknown.xyz") == "application/octet-stream"