packages
The Louise packages, each released on its own
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#31 · main · 4 min 11 s
0Étoiles
0Forks
3Issues
| 1 | import access.{Access} |
| 2 | import client |
| 3 | import log.{type Entry, Debug, Entry, Error, Info, Warn} |
| 4 | import requests.{get, htmx, post} |
| 5 | import std/http.{type Request, type Response} |
| 6 | import std/test.{assert_equal} |
| 7 | import std/time |
| 8 | |
| 9 | /// `req` answered with `status`, through the access log, with no peer |
| 10 | /// (so the client is `"unknown"`) and a clock that stands still. |
| 11 | fn answered :: req: Request status: Int -> (Response, List<Entry>) |
| 12 | { |
| 13 | answered_by (access.defaults ()) req status |
| 14 | } |
| 15 | |
| 16 | fn answered_by :: |
| 17 | rules: access.Access |
| 18 | req: Request |
| 19 | status: Int |
| 20 | -> (Response, List<Entry>) |
| 21 | { |
| 22 | time.with_fixed_clock |
| 23 | (time.from_unix_ms 0) |
| 24 | fn |
| 25 | { |
| 26 | client.behind |
| 27 | [] |
| 28 | fn |
| 29 | { |
| 30 | log.captured |
| 31 | fn { access.logged rules req fn { http.text status "body" } } |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | @test |
| 37 | fn a_page_is_one_info_line :: |
| 38 | { |
| 39 | assert_equal |
| 40 | (answered (get "/leaderboard") 200).1 |
| 41 | [Entry(Info, "GET /leaderboard 200 0ms", [("client", "unknown")])] |
| 42 | } |
| 43 | |
| 44 | @test |
| 45 | fn the_response_passes_through :: |
| 46 | { |
| 47 | let (resp, _) = answered (post "/notes" "a=1") 201 |
| 48 | assert_equal resp.status 201 |
| 49 | assert_equal (requests.text resp) "body" |
| 50 | } |
| 51 | |
| 52 | @test |
| 53 | fn an_asset_that_was_served_is_debug :: |
| 54 | { |
| 55 | assert_equal |
| 56 | (answered (get "/static/app.css") 200).1 |
| 57 | [Entry(Debug, "GET /static/app.css 200 0ms", [("client", "unknown")])] |
| 58 | } |
| 59 | |
| 60 | @test |
| 61 | fn a_redirected_favicon_is_debug :: |
| 62 | { |
| 63 | assert_equal |
| 64 | (access.level (access.defaults ()) (get "/favicon.ico") 303) |
| 65 | Debug |
| 66 | } |
| 67 | |
| 68 | @test |
| 69 | fn a_missing_asset_is_a_warning :: |
| 70 | { |
| 71 | assert_equal |
| 72 | (access.level (access.defaults ()) (get "/static/gone.js") 404) |
| 73 | Warn |
| 74 | } |
| 75 | |
| 76 | @test |
| 77 | fn a_server_error_is_an_error_even_for_an_asset :: |
| 78 | { |
| 79 | assert_equal (access.level (access.defaults ()) (get "/") 500) Error |
| 80 | assert_equal |
| 81 | (access.level (access.defaults ()) (get "/static/app.css") 503) |
| 82 | Error |
| 83 | } |
| 84 | |
| 85 | @test |
| 86 | fn the_assets_are_the_callers :: |
| 87 | { |
| 88 | assert_equal |
| 89 | (access.level Access(["/assets/"], True) (get "/static/app.css") 200) |
| 90 | Info |
| 91 | assert_equal |
| 92 | (access.level Access(["/assets/"], True) (get "/assets/app.css") 200) |
| 93 | Debug |
| 94 | } |
| 95 | |
| 96 | @test |
| 97 | fn the_query_string_is_not_logged :: |
| 98 | { |
| 99 | assert_equal |
| 100 | (answered (get "/search?q=secret") 200).1 |
| 101 | [Entry(Info, "GET /search 200 0ms", [("client", "unknown")])] |
| 102 | } |
| 103 | |
| 104 | @test |
| 105 | fn an_htmx_request_is_marked :: |
| 106 | { |
| 107 | assert_equal |
| 108 | (answered (htmx (get "/leaderboard")) 200).1 |
| 109 | [ Entry( Info |
| 110 | , "GET /leaderboard 200 0ms" |
| 111 | , [("client", "unknown"), ("htmx", "1")] |
| 112 | ) |
| 113 | ] |
| 114 | } |
| 115 | |
| 116 | @test |
| 117 | fn a_line_can_leave_the_client_out :: |
| 118 | { |
| 119 | let quiet = access.without_clients (access.defaults ()) |
| 120 | assert_equal |
| 121 | (answered_by quiet (get "/leaderboard") 200).1 |
| 122 | [Entry(Info, "GET /leaderboard 200 0ms", [])] |
| 123 | assert_equal |
| 124 | (answered_by quiet (htmx (get "/notes")) 200).1 |
| 125 | [Entry(Info, "GET /notes 200 0ms", [("htmx", "1")])] |
| 126 | } |