tobogganSe connecter

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.

90 lignes · 2121 octetsBrut · Historique
1import async.{type async, later}
2import parts.{type Settings, Settings}
3import requests.{header}
4import security
5import std/bytes
6import std/html.{type Html, Node, Text}
7import std/http.{type Response}
8import std/process.{type proc}
9import std/random.{type random}
10import std/string
11import std/test.{assert, assert_equal}
12import std/time.{type clock}
13import swap
14
15effect greeting
16{
17 fn greeting :: -> String
18}
19
20fn 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
30fn within :: render: fn() -> Html / <greeting> -> Html
31{
32 greeting_is "hello from the part actor" fn { render () }
33}
34
35fn 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
43fn settings :: -> Settings
44{
45 Settings( timeout_ms: 1000
46 , fallback: Text("unavailable")
47 , nonce: "c3RyZWFtZWQtbm9uY2U="
48 )
49}
50
51fn text :: resp: Response -> String / <io, proc, clock, random, ffi>
52{
53 bytes.to_string_lossy (http.collect_body resp)
54}
55
56fn served :: -> Response / <async<<greeting>>>
57{
58 page ()
59}
60
61@test
62fn 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
71fn 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}