import access.{Access} import client import log.{type Entry, Debug, Entry, Error, Info, Warn} import requests.{get, htmx, post} import std/http.{type Request, type Response} import std/test.{assert_equal} import std/time /// `req` answered with `status`, through the access log, with no peer /// (so the client is `"unknown"`) and a clock that stands still. fn answered :: req: Request status: Int -> (Response, List) { answered_by (access.defaults ()) req status } fn answered_by :: rules: access.Access req: Request status: Int -> (Response, List) { time.with_fixed_clock (time.from_unix_ms 0) fn { client.behind [] fn { log.captured fn { access.logged rules req fn { http.text status "body" } } } } } @test fn a_page_is_one_info_line :: { assert_equal (answered (get "/leaderboard") 200).1 [Entry(Info, "GET /leaderboard 200 0ms", [("client", "unknown")])] } @test fn the_response_passes_through :: { let (resp, _) = answered (post "/notes" "a=1") 201 assert_equal resp.status 201 assert_equal (requests.text resp) "body" } @test fn an_asset_that_was_served_is_debug :: { assert_equal (answered (get "/static/app.css") 200).1 [Entry(Debug, "GET /static/app.css 200 0ms", [("client", "unknown")])] } @test fn a_redirected_favicon_is_debug :: { assert_equal (access.level (access.defaults ()) (get "/favicon.ico") 303) Debug } @test fn a_missing_asset_is_a_warning :: { assert_equal (access.level (access.defaults ()) (get "/static/gone.js") 404) Warn } @test fn a_server_error_is_an_error_even_for_an_asset :: { assert_equal (access.level (access.defaults ()) (get "/") 500) Error assert_equal (access.level (access.defaults ()) (get "/static/app.css") 503) Error } @test fn the_assets_are_the_callers :: { assert_equal (access.level Access(["/assets/"], True) (get "/static/app.css") 200) Info assert_equal (access.level Access(["/assets/"], True) (get "/assets/app.css") 200) Debug } @test fn the_query_string_is_not_logged :: { assert_equal (answered (get "/search?q=secret") 200).1 [Entry(Info, "GET /search 200 0ms", [("client", "unknown")])] } @test fn an_htmx_request_is_marked :: { assert_equal (answered (htmx (get "/leaderboard")) 200).1 [ Entry( Info , "GET /leaderboard 200 0ms" , [("client", "unknown"), ("htmx", "1")] ) ] } @test fn a_line_can_leave_the_client_out :: { let quiet = access.without_clients (access.defaults ()) assert_equal (answered_by quiet (get "/leaderboard") 200).1 [Entry(Info, "GET /leaderboard 200 0ms", [])] assert_equal (answered_by quiet (htmx (get "/notes")) 200).1 [Entry(Info, "GET /notes 200 0ms", [("htmx", "1")])] }