oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package equipotest
2
3import (
4 "net/http"
5 "net/http/httptest"
6 "regexp"
7 "strings"
8 "testing"
9
10 equipo "amadan.net/oficina/equipo/internal/equipo"
11)
12
13// hashedStylesheet is the fingerprinted URL shape the index page
14// links: /static/tokens.<16 hex>.css.
15var hashedStylesheet = regexp.MustCompile(`/static/tokens\.[0-9a-f]{16}\.css`)
16
17func TestIndexLinksFingerprintedStylesheet(t *testing.T) {
18 t.Parallel()
19 h := newApp(t)
20 href := hashedStylesheet.FindString(get(t, h, "/signin").Body.String())
21 if href == "" {
22 t.Fatal("sign-in page links no fingerprinted stylesheet")
23 }
24 rec := get(t, h, href)
25 if rec.Code != http.StatusOK {
26 t.Fatalf("GET %s: status %d, want 200", href, rec.Code)
27 }
28 if got := rec.Header().Get("Cache-Control"); got != "public, max-age=31536000, immutable" {
29 t.Errorf("GET %s: Cache-Control = %q, want immutable year", href, got)
30 }
31}
32
33// The bare name keeps working, just never long-cached — so a changed
34// file always shows on an ordinary reload.
35func TestBareAssetNameStaysFresh(t *testing.T) {
36 t.Parallel()
37 rec := get(t, newApp(t), "/static/tokens.css")
38 if rec.Code != http.StatusOK {
39 t.Fatalf("GET /static/tokens.css: status %d, want 200", rec.Code)
40 }
41 if got := rec.Header().Get("Cache-Control"); got != "no-cache" {
42 t.Errorf("Cache-Control = %q, want no-cache", got)
43 }
44}
45
46// ErrorPage is what main.go points Options.ErrorPage at (and the seam
47// to wire into Ctx.ErrorPage as handlers grow); called directly, it
48// pins that a panic's page is the framework's error-page partial, not
49// a stray net/http default.
50func TestErrorPageRendersFrameworkPartial(t *testing.T) {
51 t.Parallel()
52 rec := httptest.NewRecorder()
53 equipo.ErrorPage(rec, httptest.NewRequest(http.MethodGet, "/", nil), http.StatusInternalServerError, "abc123")
54 if rec.Code != http.StatusInternalServerError {
55 t.Fatalf("ErrorPage: status %d, want %d", rec.Code, http.StatusInternalServerError)
56 }
57 body := rec.Body.String()
58 if !strings.Contains(body, "Something went wrong on our side") {
59 t.Errorf("ErrorPage body missing the 500 title:\n%s", body)
60 }
61 if !strings.Contains(body, "abc123") {
62 t.Errorf("ErrorPage body missing the ref:\n%s", body)
63 }
64}
65