| 1 | package theme |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | "encoding/hex" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestFontHandlerServesOnlyItsTwoFiles(t *testing.T) { |
| 12 | h := FontHandler() |
| 13 | for path, want := range map[string]int{ |
| 14 | FontPrefix + "fonts.css": 200, |
| 15 | FontPrefix + "onest-latin.woff2": 200, |
| 16 | FontPrefix + "onest.woff2": 404, |
| 17 | FontPrefix + "../theme/estilo.css": 404, |
| 18 | FontPrefix: 404, |
| 19 | "/static/other/onest-latin.woff2": 404, |
| 20 | } { |
| 21 | rec := httptest.NewRecorder() |
| 22 | h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, path, nil)) |
| 23 | if rec.Code != want { |
| 24 | t.Errorf("GET %s = %d, want %d", path, rec.Code, want) |
| 25 | } |
| 26 | } |
| 27 | rec := httptest.NewRecorder() |
| 28 | h.ServeHTTP(rec, httptest.NewRequest(http.MethodPost, FontPrefix+"fonts.css", nil)) |
| 29 | if rec.Code != http.StatusMethodNotAllowed { |
| 30 | t.Errorf("POST = %d, want 405", rec.Code) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | // The embedded font is the one the provenance record names. |
| 35 | func TestOnestIsTheRecordedFile(t *testing.T) { |
| 36 | body, err := fontFS.ReadFile("fonts/onest-latin.woff2") |
| 37 | if err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | sum := sha256.Sum256(body) |
| 41 | if got := hex.EncodeToString(sum[:]); got != "cc61cbd632bb677cd55f0eec9ebdd3e1525c5f30904cbf0769e2ac50d0fba992" { |
| 42 | t.Fatalf("onest-latin.woff2 is not the file credits/notices/onest-provenance.json records (sha256 %s)", got) |
| 43 | } |
| 44 | } |
| 45 | |