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 form.{type Schema} |
| 2 | import forms |
| 3 | import reply.{type Reply, Fragment} |
| 4 | import requests.{get, htmx, post, shell, text} |
| 5 | import std/html |
| 6 | import std/test.{assert_equal} |
| 7 | |
| 8 | type Sighting { Sighting(bird: String, count: Int) } |
| 9 | |
| 10 | fn sighting :: -> Schema<Sighting> |
| 11 | { |
| 12 | use bird <- form.field "bird" (form.parse_string ()) |
| 13 | use count <- form.field "count" (form.parse_int ()) |
| 14 | form.success Sighting(bird, count) |
| 15 | } |
| 16 | |
| 17 | fn shown :: seen: Sighting -> Reply |
| 18 | { |
| 19 | Fragment(html.text "{seen.bird} × {seen.count}") |
| 20 | } |
| 21 | |
| 22 | @test |
| 23 | fn a_form_is_decoded_from_the_body :: |
| 24 | { |
| 25 | let req = post "/birds" "bird=jay+%26+crow&count=3" |
| 26 | let resp = |
| 27 | reply.respond req (forms.decoded req (sighting ()) shown) (shell ()) |
| 28 | assert_equal resp.status 200 |
| 29 | assert_equal (text resp) "jay & crow × 3" |
| 30 | } |
| 31 | |
| 32 | @test |
| 33 | fn a_missing_text_field_is_empty :: |
| 34 | { |
| 35 | let req = post "/birds" "count=1" |
| 36 | let resp = |
| 37 | reply.respond req (forms.decoded req (sighting ()) shown) (shell ()) |
| 38 | assert_equal (text resp) " × 1" |
| 39 | } |
| 40 | |
| 41 | @test |
| 42 | fn query_parameters_are_decoded_too :: |
| 43 | { |
| 44 | let req = get "/birds?bird=owl&count=2" |
| 45 | let resp = |
| 46 | reply.respond req (forms.queried req (sighting ()) shown) (shell ()) |
| 47 | assert_equal (text resp) "owl × 2" |
| 48 | } |
| 49 | |
| 50 | @test |
| 51 | fn a_request_the_schema_refuses_is_unreadable :: |
| 52 | { |
| 53 | let req = htmx (post "/birds" "bird=owl&count=many") |
| 54 | let resp = |
| 55 | reply.respond req (forms.decoded req (sighting ()) shown) (shell ()) |
| 56 | assert_equal resp.status 400 |
| 57 | assert_equal (text resp) "<p>unreadable</p>" |
| 58 | } |