"""HTTP tests for the dashboard server.""" from http.server import ThreadingHTTPServer import json import threading import unittest from urllib.error import HTTPError from urllib.request import urlopen from app import AppHandler class AppHandlerTests(unittest.TestCase): @classmethod def setUpClass(cls): cls.server = ThreadingHTTPServer(("127.0.0.1", 0), AppHandler) cls.thread = threading.Thread(target=cls.server.serve_forever, daemon=True) cls.thread.start() cls.base_url = f"http://127.0.0.1:{cls.server.server_port}" @classmethod def tearDownClass(cls): cls.server.shutdown() cls.server.server_close() cls.thread.join(timeout=2) def test_health_endpoint_returns_json(self): with urlopen(f"{self.base_url}/health") as response: self.assertEqual(response.status, 200) self.assertEqual(json.load(response), {"status": "ok"}) self.assertEqual(response.headers["X-Content-Type-Options"], "nosniff") def test_dashboard_is_english(self): with urlopen(self.base_url) as response: body = response.read().decode("utf-8") self.assertEqual(response.status, 200) self.assertIn('', body) self.assertIn("Start annual health check", body) self.assertNotIn("Ã…rleg", body) def test_unknown_path_returns_not_found(self): try: with urlopen(f"{self.base_url}/missing") as response: self.fail(f"Expected 404, received {response.status}") except HTTPError as error: try: self.assertEqual(error.code, 404) error.read() finally: error.close() if __name__ == "__main__": unittest.main()