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 assets |
| 2 | import requests.{header, text} |
| 3 | import std/http.{type Response} |
| 4 | import std/string |
| 5 | import std/test.{assert, assert_equal} |
| 6 | |
| 7 | fn served :: segments: List<String> -> Response / <io> |
| 8 | { |
| 9 | assets.serve "test/static" vendored segments |
| 10 | } |
| 11 | |
| 12 | fn vendored :: segments: List<String> -> String |
| 13 | { |
| 14 | match segments { ["vendor", ..] -> "public, max-age=86400", _ -> "no-cache" } |
| 15 | } |
| 16 | |
| 17 | @test |
| 18 | fn a_stylesheet_is_served_with_its_type :: -> () / <io> |
| 19 | { |
| 20 | let resp = served ["app.css"] |
| 21 | assert_equal resp.status 200 |
| 22 | assert_equal (header resp "Content-Type") "text/css; charset=utf-8" |
| 23 | assert (string.contains (text resp) ":root") "the stylesheet" |
| 24 | } |
| 25 | |
| 26 | @test |
| 27 | fn the_caching_rule_is_the_callers :: -> () / <io> |
| 28 | { |
| 29 | let resp = served ["vendor", "lib.js"] |
| 30 | assert_equal resp.status 200 |
| 31 | assert_equal (header resp "Content-Type") "text/javascript; charset=utf-8" |
| 32 | assert_equal (header resp "Cache-Control") "public, max-age=86400" |
| 33 | assert_equal (header (served ["app.css"]) "Cache-Control") "no-cache" |
| 34 | } |
| 35 | |
| 36 | @test |
| 37 | fn a_missing_file_is_not_found :: -> () / <io> |
| 38 | { |
| 39 | assert_equal ((served ["nope.css"]).status) 404 |
| 40 | } |
| 41 | |
| 42 | @test |
| 43 | fn paths_cannot_leave_the_root :: -> () / <io> |
| 44 | { |
| 45 | assert_equal ((served ["..", "assets_test.lse"]).status) 404 |
| 46 | assert_equal ((served ["vendor", "..", "..", "assets_test.lse"]).status) 404 |
| 47 | assert_equal ((served ["../assets_test.lse"]).status) 404 |
| 48 | assert_equal ((served [".hidden"]).status) 404 |
| 49 | assert_equal ((served []).status) 404 |
| 50 | } |
| 51 | |
| 52 | @test |
| 53 | fn images_manifests_and_json_are_served_with_their_types :: -> () / <io> |
| 54 | { |
| 55 | let typed = fn file { header (served [file]) "Content-Type" } |
| 56 | assert_equal (typed "avatar.webp") "image/webp" |
| 57 | assert_equal (typed "photo.jpg") "image/jpeg" |
| 58 | assert_equal (typed "photo.jpeg") "image/jpeg" |
| 59 | assert_equal (typed "photo.avif") "image/avif" |
| 60 | assert_equal (typed "moving.gif") "image/gif" |
| 61 | assert_equal (typed "manifest.webmanifest") "application/manifest+json" |
| 62 | assert_equal (typed "data.json") "application/json" |
| 63 | } |