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#28 · main · 3 min 34 s
0Étoiles
0Forks
3Issues
| 1 | import async.{type async, later} |
| 2 | import parts.{type Settings, Settings} |
| 3 | import requests.{header} |
| 4 | import security |
| 5 | import std/bytes |
| 6 | import std/html.{type Html, Node, Text} |
| 7 | import std/http.{type Response} |
| 8 | import std/process.{type proc} |
| 9 | import std/random.{type random} |
| 10 | import std/string |
| 11 | import std/test.{assert, assert_equal} |
| 12 | import std/time.{type clock} |
| 13 | import swap |
| 14 | |
| 15 | effect greeting |
| 16 | { |
| 17 | fn greeting :: -> String |
| 18 | } |
| 19 | |
| 20 | fn greeting_is :: word: String action: fn() -> a / <greeting | e> -> a / <e> |
| 21 | { |
| 22 | handle |
| 23 | { |
| 24 | action () |
| 25 | } |
| 26 | with |
| 27 | { greeting -> word } |
| 28 | } |
| 29 | |
| 30 | fn within :: render: fn() -> Html / <greeting> -> Html |
| 31 | { |
| 32 | greeting_is "hello from the part actor" fn { render () } |
| 33 | } |
| 34 | |
| 35 | fn page :: -> Response / <async<<greeting | r>> | r2> |
| 36 | { |
| 37 | let part = later (Text("…")) fn { Node("p", [], [Text(greeting ())]) } |
| 38 | let document = |
| 39 | Node("html", [], [Node("head", [], []), Node("body", [], [part])]) |
| 40 | security.harden (http.html document) |
| 41 | } |
| 42 | |
| 43 | fn settings :: -> Settings |
| 44 | { |
| 45 | Settings( timeout_ms: 1000 |
| 46 | , fallback: Text("unavailable") |
| 47 | , nonce: "c3RyZWFtZWQtbm9uY2U=" |
| 48 | ) |
| 49 | } |
| 50 | |
| 51 | fn text :: resp: Response -> String / <io, proc, clock, random, ffi> |
| 52 | { |
| 53 | bytes.to_string_lossy (http.collect_body resp) |
| 54 | } |
| 55 | |
| 56 | fn served :: -> Response / <async<<greeting>>> |
| 57 | { |
| 58 | page () |
| 59 | } |
| 60 | |
| 61 | @test |
| 62 | fn inline_parts_render_in_place_with_the_handlers_around_them :: |
| 63 | { |
| 64 | let resp = greeting_is "hello in place" fn { async.inline fn { page () } } |
| 65 | assert_equal |
| 66 | (text resp) |
| 67 | "<html><head></head><body><p>hello in place</p></body></html>" |
| 68 | } |
| 69 | |
| 70 | @test |
| 71 | fn streamed_parts_render_in_actors_under_the_handlers_within_installs :: |
| 72 | { |
| 73 | let resp = |
| 74 | async.streamed (settings ()) served fn render { within fn { render () } } |
| 75 | let body = text resp |
| 76 | assert |
| 77 | ( string.contains |
| 78 | body |
| 79 | "<template><p>hello from the part actor</p></template>" |
| 80 | ) |
| 81 | body |
| 82 | assert (string.ends_with body "</body></html>") body |
| 83 | assert (string.contains body (swap.script "c3RyZWFtZWQtbm9uY2U=")) body |
| 84 | assert |
| 85 | ( string.contains |
| 86 | (header resp "Content-Security-Policy") |
| 87 | "'nonce-c3RyZWFtZWQtbm9uY2U='" |
| 88 | ) |
| 89 | "the policy allows the settings' nonce" |
| 90 | } |