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.

181 lignes · 4359 octetsBrut · Historique
1import reply.
2{ Fragment
3, Header
4, Missing
5, Page
6, Redirect
7, Status
8, Trigger
9, Unreadable
10, WithCookie
11}
12import requests.{get, header, htmx, post, shell, text}
13import std/http/cookie
14import std/test.{assert_equal}
15
16@test
17fn a_page_is_rendered_inside_the_frame ::
18{
19 let resp = reply.respond (get "/") Page("Notes", <p>hello</p>) (shell ())
20 assert_equal resp.status 200
21 assert_equal (header resp "Content-Type") "text/html; charset=utf-8"
22 assert_equal
23 (text resp)
24 "<!doctype html><main data-title=\"Notes\"><p>hello</p></main>"
25}
26
27@test
28fn a_fragment_is_rendered_alone ::
29{
30 let resp = reply.respond (htmx (get "/")) Fragment(<p>hello</p>) (shell ())
31 assert_equal resp.status 200
32 assert_equal (text resp) "<p>hello</p>"
33}
34
35@test
36fn a_redirect_is_a_see_other_for_a_browser ::
37{
38 let resp = reply.respond (post "/notes" "") Redirect("/archive") (shell ())
39 assert_equal resp.status 303
40 assert_equal (header resp "Location") "/archive"
41}
42
43@test
44fn a_redirect_is_a_client_redirect_for_htmx ::
45{
46 let resp =
47 reply.respond (htmx (post "/notes" "")) Redirect("/archive") (shell ())
48 assert_equal resp.status 200
49 assert_equal (header resp "HX-Redirect") "/archive"
50}
51
52@test
53fn a_status_changes_the_code_of_what_it_wraps ::
54{
55 let resp = reply.respond (get "/") Status(422, Fragment(<p>no</p>)) (shell ())
56 assert_equal resp.status 422
57 assert_equal (text resp) "<p>no</p>"
58}
59
60@test
61fn a_cookie_is_set_on_what_it_wraps ::
62{
63 let resp =
64 reply.respond
65 (post "/login" "")
66 WithCookie(cookie.new "session" "abc", Redirect("/"))
67 (shell ())
68 assert_equal resp.status 303
69 assert_equal
70 (header resp "Set-Cookie")
71 "session=abc; Path=/; Secure; HttpOnly; SameSite=Lax"
72}
73
74@test
75fn missing_is_the_shells_not_found_page ::
76{
77 let resp = reply.respond (get "/nowhere") Missing (shell ())
78 assert_equal resp.status 404
79 assert_equal
80 (text resp)
81 "<!doctype html><main data-title=\"Not found\"><p>nothing here</p></main>"
82}
83
84@test
85fn missing_is_a_fragment_for_htmx ::
86{
87 let resp = reply.respond (htmx (get "/nowhere")) Missing (shell ())
88 assert_equal resp.status 404
89 assert_equal (text resp) "<p>nothing here</p>"
90}
91
92@test
93fn unreadable_is_the_shells_bad_request ::
94{
95 let resp = reply.respond (get "/") Unreadable (shell ())
96 assert_equal resp.status 400
97 assert_equal
98 (text resp)
99 "<!doctype html><main data-title=\"Bad request\"><p>unreadable</p></main>"
100 assert_equal
101 (text (reply.respond (htmx (get "/")) Unreadable (shell ())))
102 "<p>unreadable</p>"
103}
104
105@test
106fn requests_from_htmx_are_recognised ::
107{
108 assert_equal (reply.from_htmx (htmx (get "/"))) True
109 assert_equal (reply.from_htmx (get "/")) False
110}
111
112@test
113fn a_view_is_a_fragment_for_htmx_and_a_page_otherwise ::
114{
115 let viewed = fn req
116 {
117 text (reply.respond req (reply.view req "Notes" (<p>hi</p>)) (shell ()))
118 }
119 assert_equal (viewed (htmx (get "/"))) "<p>hi</p>"
120 assert_equal
121 (viewed (get "/"))
122 "<!doctype html><main data-title=\"Notes\"><p>hi</p></main>"
123}
124
125@test
126fn otherwise_tries_the_next_only_when_missing ::
127{
128 let status = fn first
129 {
130 ( reply.respond
131 (get "/")
132 (reply.otherwise first fn { Redirect("/next") })
133 (shell ())
134 ).status
135 }
136 assert_equal (status Missing) 303
137 assert_equal (status Fragment(<p>first</p>)) 200
138 assert_equal (status Unreadable) 400
139}
140
141@test
142fn a_trigger_fires_its_event_on_the_body ::
143{
144 let resp =
145 reply.respond
146 (htmx (post "/labels" ""))
147 Trigger("labels-changed", Fragment(<p>ok</p>))
148 (shell ())
149 assert_equal
150 (header resp "HX-Trigger")
151 "\{\"labels-changed\":\{\"target\":\"body\"\}\}"
152 assert_equal (text resp) "<p>ok</p>"
153}
154
155@test
156fn a_header_is_added_to_what_it_wraps ::
157{
158 let resp =
159 reply.respond
160 (get "/")
161 Header("Retry-After", "7", Status(503, Fragment(<p>later</p>)))
162 (shell ())
163 assert_equal resp.status 503
164 assert_equal (header resp "Retry-After") "7"
165 assert_equal (text resp) "<p>later</p>"
166}
167
168@test
169fn too_many_is_a_429_with_its_retry_delay ::
170{
171 let resp =
172 reply.respond
173 (get "/login")
174 (reply.too_many Page("Log in", <p>form</p>) 60)
175 (shell ())
176 assert_equal resp.status 429
177 assert_equal (header resp "Retry-After") "60"
178 assert_equal
179 (text resp)
180 "<!doctype html><main data-title=\"Log in\"><p>form</p></main>"
181}