import parts.{type Settings, Settings} import requests.{header} import security import std/bytes import std/html.{type Html, Node, Text} import std/http.{type Response} import std/list import std/option import std/process.{type proc} import std/random.{type random} import std/string import std/test.{assert, assert_equal} import std/time.{type clock} import swap fn page :: content: List -> Response { let document = Node("html", [], [Node("head", [], []), Node("body", [], content)]) security.harden (http.html document) } fn settings :: -> Settings { Settings(timeout_ms: 1000, fallback: Text("unavailable"), nonce: nonce ()) } fn nonce :: -> String { "bm9uY2Utb2YtdGhpcy1yZXF1ZXN0" } fn text :: resp: Response -> String / { bytes.to_string_lossy (http.collect_body resp) } fn between :: text: String start: String stop: String -> String { match string.split text start { [_, rest, ..] -> match string.split rest stop { [found, ..] -> found, [] -> "" } , _ -> "" } } fn nonce_of :: resp: Response -> String { between (header resp "Content-Security-Policy") "'nonce-" "'" } fn id_of :: shown: Html -> String { between (html.render shown) "id=\"" "\"" } @test fn a_page_without_parts_is_answered_whole :: { let started = parts.new () let whole = page [Text("plain")] let resp = parts.respond started (settings ()) whole let held = fn r { option.map (http.whole_body r) bytes.to_string_lossy } assert_equal (held resp) (held whole) } @test fn a_part_streams_after_the_page_and_before_its_end :: { let started = parts.new () let shown = parts.start started (Text("…")) fn { Node("ul", [], [Text("labels")]) } let whole = page [shown] let resp = parts.respond started (settings ()) whole let rendered = option.unwrap_or (http.whole_body whole |> option.map bytes.to_string_lossy) "" match swap.opened rendered (nonce ()) { Some((first, last)) -> assert_equal (text resp) (first <> swap.part (id_of shown) "" <> last) , None -> assert False "the page opens" } } @test fn parts_arrive_in_the_order_they_finish :: { let started = parts.new () let slow = parts.start started (Text("…")) fn { process.sleep 50 Text("slow") } let fast = parts.start started (Text("…")) fn { Text("fast") } let body = text (parts.respond started (settings ()) (page [slow, fast])) let both = swap.part (id_of fast) "fast" <> swap.part (id_of slow) "slow" assert (string.contains body both) body } @test fn a_part_that_fails_shows_the_fallback :: { let started = parts.new () let broken = parts.start started (Text("…")) fn { panic "no labels" } let body = text (parts.respond started (settings ()) (page [broken])) assert (string.contains body (swap.part (id_of broken) "unavailable")) body } @test fn a_part_past_the_deadline_shows_the_fallback :: { let started = parts.new () let late = parts.start started (Text("…")) fn { process.sleep 5000 Text("late") } let body = text (parts.respond started (settings ()) (page [late])) assert (string.contains body (swap.part (id_of late) "unavailable")) body assert (!string.contains body "late") body } @test fn parts_of_a_reply_that_is_not_a_page_are_stopped :: { let probe = process.subject () let started = parts.new () let _ = parts.start started (Text("…")) fn { process.sleep 50 process.send probe "still running" Text("never shown") } let resp = parts.respond started (settings ()) (http.text 303 "") assert (option.is_some (http.whole_body resp)) "answered as it was" process.sleep 100 assert_equal (process.receive probe 0) None } @test fn a_page_of_any_status_streams_its_parts :: { let started = parts.new () let shown = parts.start started (Text("…")) fn { Text("labels") } let missing = http.Response(..page [shown], status: 404) let resp = parts.respond started (settings ()) missing assert_equal resp.status 404 let body = text resp assert (string.contains body (swap.part (id_of shown) "labels")) body } @test fn the_streamed_page_carries_the_nonce_of_its_settings :: { let started = parts.new () let shown = parts.start started (Text("…")) fn { Text("labels") } let resp = parts.respond started (settings ()) (page [shown]) assert_equal (nonce_of resp) (nonce ()) let body = text resp assert (string.contains body (swap.script (nonce ()))) body } @test fn a_page_that_allows_the_nonce_already_allows_it_once :: { let started = parts.new () let shown = parts.start started (Text("…")) fn { Text("labels") } let allowed = security.with_script_nonce (page [shown]) (nonce ()) let resp = parts.respond started (settings ()) allowed assert_equal (header resp "Content-Security-Policy") (header allowed "Content-Security-Policy") let _ = text resp () } @test fn a_page_whose_head_holds_the_script_streams_it_once :: { let started = parts.new () let shown = parts.start started (Text("…")) fn { Text("labels") } let script = swap.script (nonce ()) let document = "{script}{html.render shown}" let resp = parts.respond started (settings ()) ( security.harden (http.response 200 "text/html" (bytes.from_string document)) ) let body = text resp assert_equal (list.length (string.split body script)) 2 assert (string.contains body (swap.part (id_of shown) "labels")) body }