static_test.go
1package pages
2
3import (
4 "io"
5 "net/http"
6 "net/http/httptest"
7 "testing"
8)
9
10func TestStaticAssetsHashed(t *testing.T) {
11 server := httptest.NewServer(http.FileServerFS(StaticFS))
12 defer server.Close()
13
14 for _, path := range []string{
15 "/static/favicon.svg",
16 "/static/404.jpg",
17 } {
18 hashed := hashStatic(path)
19 resp, err := http.Get(server.URL + hashed)
20 if err != nil {
21 t.Fatalf("GET %s (hashed: %s): %v", path, hashed, err)
22 }
23 defer resp.Body.Close()
24
25 if resp.StatusCode != 200 {
26 t.Errorf("expected 200 for %s (hashed: %s), got %d", path, hashed, resp.StatusCode)
27 }
28
29 body, err := io.ReadAll(resp.Body)
30 if err != nil {
31 t.Fatalf("reading body for %s (hashed: %s): %v", path, hashed, err)
32 }
33 if len(body) == 0 {
34 t.Errorf("expected non-empty body for %s (hashed: %s)", path, hashed)
35 }
36 }
37}
38
39func TestHashStaticRoundTrip(t *testing.T) {
40 for _, path := range []string{
41 "/static/favicon.svg",
42 "/static/404.jpg",
43 "/static/401.jpg",
44 "/static/500.jpg",
45 } {
46 hashed := hashStatic(path)
47 unhashed := unhashStatic(hashed)
48 if unhashed != path {
49 t.Errorf("unhashStatic(hashStatic(%q)) = %q, want %q", path, unhashed, path)
50 }
51 }
52}
53
54func TestStaticFSUnhashing(t *testing.T) {
55 for _, path := range []string{
56 "/static/favicon.svg",
57 "/static/404.jpg",
58 } {
59 hashed := hashStatic(path)
60 file, err := StaticFS.Open(hashed)
61 if err != nil {
62 t.Errorf("StaticFS.Open(%q) (hashed from %q): %v", hashed, path, err)
63 continue
64 }
65 file.Close()
66 }
67}