"""The web app's obligations: the router, the pages, and MJPEG over one port.""" from __future__ import annotations import logging import socket import threading import time from live_helpers import PHRASE, StubCapture, read_parts from hhstream_live.server import BOUNDARY, HEADER_TIMEOUT_S, LiveServer, RateLimiter WRONG = "no-such-phrase-here-00000" def get(conn, path): conn.request("GET", path) resp = conn.getresponse() body = resp.read() return resp.status, resp, body def test_landing_page_is_a_form_and_names_no_phrase(serve, http_conn): with serve() as (server, _capture): conn = http_conn(server) status, resp, body = get(conn, "/") assert status == 200 assert resp.getheader("Content-Type").startswith("text/html") assert PHRASE not in body.decode() assert "hh-demo" not in body.decode() assert b"phrase" in body.lower() def test_healthz(serve, http_conn): with serve() as (server, _): status, _, body = get(http_conn(server), "/healthz") assert status == 200 and body.strip() == b"ok" def test_unknown_phrase_is_404_with_an_empty_body(serve, http_conn): with serve() as (server, _): status, resp, body = get(http_conn(server), f"/{WRONG}/") assert status == 404 assert body == b"" assert WRONG not in resp.getheader("Content-Type", "") def test_a_wrong_subpath_under_a_valid_phrase_is_404(serve, http_conn): with serve() as (server, _): status, _, body = get(http_conn(server), f"/{PHRASE}/secrets.txt") assert status == 404 and body == b"" def test_viewer_page_names_the_output_and_points_at_the_stream(serve, http_conn): with serve() as (server, _): status, _, body = get(http_conn(server), f"/{PHRASE}/") assert status == 200 page = body.decode() assert f"/{PHRASE}/stream.mjpg" in page assert "hh-demo" in page def test_phrase_is_case_insensitive(serve, http_conn): with serve() as (server, _): status, _, _ = get(http_conn(server), f"/{PHRASE.upper()}/") assert status == 200 def test_status_json_shape(serve, http_conn): import json with serve() as (server, _): status, resp, body = get(http_conn(server), f"/{PHRASE}/status.json") assert status == 200 assert resp.getheader("Content-Type").startswith("application/json") doc = json.loads(body) assert doc["session"] == "demo" assert doc["output"] == "hh-demo" assert doc["fps"] == 5 assert doc["viewers"] == 0 assert doc["stopped"] is False def test_snapshot_is_503_until_someone_has_watched(serve, http_conn): with serve() as (server, _): status, _, body = get(http_conn(server), f"/{PHRASE}/frame.jpg") assert status == 503 and body == b"" def test_stream_is_mjpeg_and_delivers_valid_jpeg_parts(serve, http_conn): with serve(fps=10) as (server, capture): conn = http_conn(server) conn.request("GET", f"/{PHRASE}/stream.mjpg") resp = conn.getresponse() assert resp.status == 200 assert resp.getheader("Content-Type") == ( f"multipart/x-mixed-replace; boundary={BOUNDARY}" ) # The stream has no length and is not chunked, so the only thing that # can delimit it is the close -- which an intermediary is only entitled # to expect if the response says so. assert resp.getheader("Connection") == "close" parts = read_parts(resp, 2) assert len(parts) == 2 for part in parts: assert part.startswith(b"\xff\xd8") # JPEG SOI assert part.endswith(b"\xff\xd9") # JPEG EOI conn.close() def test_snapshot_works_after_a_viewer_has_pulled_a_frame(serve, http_conn): with serve(fps=10) as (server, _): stream_conn = http_conn(server) stream_conn.request("GET", f"/{PHRASE}/stream.mjpg") resp = stream_conn.getresponse() assert read_parts(resp, 1) stream_conn.close() time.sleep(0.05) status, ctype, body = get(http_conn(server), f"/{PHRASE}/frame.jpg") assert status == 200 assert ctype.getheader("Content-Type") == "image/jpeg" assert body.startswith(b"\xff\xd8") def test_two_viewers_both_receive_frames(serve, http_conn): with serve(fps=10) as (server, _): results: dict[str, object] = {} def watch(name: str) -> None: conn = http_conn(server) conn.request("GET", f"/{PHRASE}/stream.mjpg") resp = conn.getresponse() results[name] = read_parts(resp, 1) conn.close() threads = [threading.Thread(target=watch, args=(n,)) for n in ("a", "b")] for t in threads: t.start() for t in threads: t.join(timeout=10) assert results["a"] and results["b"] def test_no_capture_until_a_request_arrives(serve): with serve() as (server, capture): time.sleep(0.4) assert capture.calls == 0 import http.client conn = http.client.HTTPConnection("127.0.0.1", server.port, timeout=10) conn.request("GET", f"/{PHRASE}/stream.mjpg") resp = conn.getresponse() assert read_parts(resp, 1) conn.close() assert capture.calls >= 1 def test_a_silent_client_does_not_pin_a_handler_thread(serve): """A connection that never sends a request line must not hold a thread. The socket timeout is the only thing that ends it; without one the handler sits in `readline` for the life of the process. """ assert HEADER_TIMEOUT_S <= 30 # a cap that never fires is not a cap with serve() as (server, _): before = threading.active_count() sock = socket.create_connection(("127.0.0.1", server.port), timeout=5) try: time.sleep(0.2) assert threading.active_count() > before # a handler is waiting on us sock.settimeout(HEADER_TIMEOUT_S + 10) started = time.monotonic() # The server closes on timeout, which arrives here as EOF. assert sock.recv(4096) == b"" waited = time.monotonic() - started assert waited <= HEADER_TIMEOUT_S + 5, waited finally: sock.close() def test_a_viewer_that_drops_mid_stream_releases_its_slot(serve, http_conn): """A dropped viewer must bring `viewers` back to 0, or grim keeps running.""" import json with serve(fps=10) as (server, _): sock = socket.create_connection(("127.0.0.1", server.port), timeout=10) sock.sendall(f"GET /{PHRASE}/stream.mjpg HTTP/1.1\r\n" f"Host: 127.0.0.1\r\n\r\n".encode("ascii")) assert sock.recv(4096) # headers, and at least the start of a part def viewers() -> int: return json.loads(get(http_conn(server), f"/{PHRASE}/status.json")[2])["viewers"] # The headers can reach us a moment before the handler counts itself in. deadline = time.monotonic() + 10 while viewers() == 0 and time.monotonic() < deadline: time.sleep(0.05) assert viewers() == 1 sock.close() # the phone walked out of range deadline = time.monotonic() + 10 while viewers() and time.monotonic() < deadline: time.sleep(0.1) assert viewers() == 0 def test_the_access_log_never_carries_the_phrase(serve, http_conn, caplog): """Debug logging is on in development, and the request line is the phrase.""" with caplog.at_level(logging.DEBUG, logger="hhstream.live"): with serve() as (server, _): assert get(http_conn(server), f"/{PHRASE}/status.json")[0] == 200 assert get(http_conn(server), f"/{WRONG}/")[0] == 404 messages = [record.getMessage() for record in caplog.records] assert messages, "something must be logged, or this proves nothing" for message in messages: assert PHRASE not in message, message assert WRONG not in message, message def test_the_server_says_when_it_is_bound_to_every_interface(registry): """`url_for` must print something dialable; the bind line tells the truth.""" loopback = LiveServer(registry, host="127.0.0.1", port=9988) assert loopback.describe_bind() == "bound to 127.0.0.1:9988" assert not loopback.binds_everywhere everywhere = LiveServer(registry, host="0.0.0.0", port=9988) assert everywhere.binds_everywhere assert everywhere.address == "127.0.0.1" # still dialable described = everywhere.describe_bind() assert "0.0.0.0:9988" in described and "all interfaces" in described def test_the_rate_limiter_counts_each_client_separately_and_forgets_them(): """One noisy client must not spend another's budget, and must not be kept. `_hits` is keyed by client address, so a table that only grew would be a memory leak any LAN host could drive. """ now = [0.0] limiter = RateLimiter(limit=2, window=60.0, clock=lambda: now[0]) assert limiter.allow("10.0.0.1") assert limiter.allow("10.0.0.1") assert not limiter.allow("10.0.0.1") # that client is out of budget assert limiter.allow("10.0.0.2") # this one is not assert limiter.tracked == 2 now[0] += 61.0 # both windows have passed assert limiter.allow("10.0.0.1") assert limiter.tracked == 1, "a client that went quiet is not kept" def test_repeated_wrong_phrases_are_rate_limited(serve, http_conn): with serve(limit=3) as (server, _): conn = http_conn(server) seen = [get(conn, f"/{WRONG}-{i}/")[0] for i in range(3)] assert seen == [404, 404, 404] assert get(conn, f"/{WRONG}-9/")[0] == 429 def test_the_server_binds_and_reports_its_port(serve): with serve() as (server, _): assert server.ready.is_set() assert server.port > 0 assert server.url_for(PHRASE) == f"http://127.0.0.1:{server.port}/{PHRASE}/"