internal/httpd/anchors_test.go
82 lines · 3093 bytes
1package httpd
2
3import (
4 "net/http"
5 "net/http/httptest"
6 "strings"
7 "testing"
8
9 "gitbay.org/gitbay/internal/config"
10 "gitbay.org/gitbay/internal/store"
11 "gitbay.org/gitbay/internal/web"
12)
13
14// Markdown headings carry ids, so a README section can be deep-linked
15// the way org headings already could (#132).
16func TestMarkdownHeadingAnchors(t *testing.T) {
17 out := string(renderReadme("README.md", []byte("# Getting started\n\n## Two words\n\n- [ ] a task\n")))
18 for _, want := range []string{`id="getting-started"`, `id="two-words"`, `type="checkbox"`} {
19 if !strings.Contains(out, want) {
20 t.Errorf("rendered markdown lacks %s:\n%s", want, out)
21 }
22 }
23}
24
25// The stylesheet carries an ETag and a cache lifetime; a revalidation
26// with the same tag is a 304 with no body (#132).
27func TestStylesheetRevalidates(t *testing.T) {
28 s := New(config.Default(), nil)
29 first := httptest.NewRecorder()
30 s.stylesheet(first, httptest.NewRequest("GET", "/static/style.css", nil))
31 tag := first.Header().Get("ETag")
32 if first.Code != 200 || tag == "" || first.Body.Len() == 0 || !strings.Contains(first.Header().Get("Cache-Control"), "max-age") {
33 t.Fatalf("first fetch: %d etag=%q cc=%q bytes=%d", first.Code, tag, first.Header().Get("Cache-Control"), first.Body.Len())
34 }
35 req := httptest.NewRequest("GET", "/static/style.css", nil)
36 req.Header.Set("If-None-Match", tag)
37 second := httptest.NewRecorder()
38 s.stylesheet(second, req)
39 if second.Code != http.StatusNotModified || second.Body.Len() != 0 {
40 t.Fatalf("revalidation: %d with %d bytes", second.Code, second.Body.Len())
41 }
42}
43
44// The layout stamps the served bytes' hash on the stylesheet URL, so a
45// deploy changes that URL and a browser cannot answer it from a copy of
46// the old sheet. A URL carrying the hash names bytes that cannot change
47// and is served without revalidation; the bare URL keeps its lifetime
48// (#239).
49func TestStylesheetURLCarriesTheBuildHash(t *testing.T) {
50 var sb strings.Builder
51 var base basePage
52 base.Viewer = "alice"
53 err := web.Render(&sb, "dashboard.html", struct {
54 basePage
55 Tab string
56 Pins []pinnedRow
57 Reviews []store.DashboardItem
58 Assigned []store.DashboardItem
59 MRs []store.DashboardItem
60 Issues []store.DashboardItem
61 Feed []feedLine
62 }{base, "dashboard", nil, nil, nil, nil, nil, nil})
63 if err != nil {
64 t.Fatal(err)
65 }
66 want := `href="/static/style.css?v=` + stylesheetHash + `"`
67 if !strings.Contains(sb.String(), want) {
68 t.Errorf("the page does not link %s", want)
69 }
70
71 s := New(config.Default(), nil)
72 versioned := httptest.NewRecorder()
73 s.stylesheet(versioned, httptest.NewRequest("GET", "/static/style.css?v="+stylesheetHash, nil))
74 if cc := versioned.Header().Get("Cache-Control"); !strings.Contains(cc, "immutable") {
75 t.Errorf("versioned URL: Cache-Control = %q, want immutable", cc)
76 }
77 bare := httptest.NewRecorder()
78 s.stylesheet(bare, httptest.NewRequest("GET", "/static/style.css", nil))
79 if cc := bare.Header().Get("Cache-Control"); !strings.Contains(cc, "must-revalidate") {
80 t.Errorf("bare URL: Cache-Control = %q, want must-revalidate", cc)
81 }
82}