| 1 | package callstest |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "regexp" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | // hashedStylesheet is the fingerprinted URL shape the index page |
| 11 | // links: /static/tokens.<16 hex>.css. |
| 12 | var hashedStylesheet = regexp.MustCompile(`/static/tokens\.[0-9a-f]{16}\.css`) |
| 13 | |
| 14 | // hashedTheme is the same shape for the vendored theme sheet: |
| 15 | // /static/theme.<16 hex>.css. |
| 16 | var hashedTheme = regexp.MustCompile(`/static/theme\.[0-9a-f]{16}\.css`) |
| 17 | |
| 18 | func TestIndexRenders(t *testing.T) { |
| 19 | c := newHarness(t, "paul@example.com").browser(t) |
| 20 | c.signIn("paul@example.com") |
| 21 | rec := c.get("/") |
| 22 | if rec.Code != http.StatusOK { |
| 23 | t.Fatalf("GET /: status %d, want 200", rec.Code) |
| 24 | } |
| 25 | if !strings.Contains(rec.Body.String(), "Meetings") { |
| 26 | t.Errorf("GET /: page is missing its heading:\n%s", rec.Body.String()) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | // /signin renders the same layout as every other page and needs no |
| 31 | // session, so it is what this test uses to find the fingerprinted |
| 32 | // stylesheet links now that "/" requires one. Both the structural |
| 33 | // tokens.css and the vendored theme.css must be there — a layout |
| 34 | // linking only tokens.css is exactly the unstyled-live-site bug this |
| 35 | // change fixes. |
| 36 | func TestIndexLinksFingerprintedStylesheet(t *testing.T) { |
| 37 | h := newApp(t) |
| 38 | body := get(t, h, "/signin").Body.String() |
| 39 | for _, re := range []*regexp.Regexp{hashedStylesheet, hashedTheme} { |
| 40 | href := re.FindString(body) |
| 41 | if href == "" { |
| 42 | t.Fatalf("signed-out page links no fingerprinted asset matching %s", re) |
| 43 | } |
| 44 | rec := get(t, h, href) |
| 45 | if rec.Code != http.StatusOK { |
| 46 | t.Fatalf("GET %s: status %d, want 200", href, rec.Code) |
| 47 | } |
| 48 | if got := rec.Header().Get("Cache-Control"); got != "public, max-age=31536000, immutable" { |
| 49 | t.Errorf("GET %s: Cache-Control = %q, want immutable year", href, got) |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // The bare name keeps working, just never long-cached — so a changed |
| 55 | // file always shows on an ordinary reload. |
| 56 | func TestBareAssetNameStaysFresh(t *testing.T) { |
| 57 | h := newApp(t) |
| 58 | for _, name := range []string{"/static/tokens.css", "/static/theme.css"} { |
| 59 | rec := get(t, h, name) |
| 60 | if rec.Code != http.StatusOK { |
| 61 | t.Fatalf("GET %s: status %d, want 200", name, rec.Code) |
| 62 | } |
| 63 | if got := rec.Header().Get("Cache-Control"); got != "no-cache" { |
| 64 | t.Errorf("GET %s: Cache-Control = %q, want no-cache", name, got) |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |