internal/httpd/anchors_test.go
40 lines · 1504 bytes
1package httpd
2
3import (
4 "net/http"
5 "net/http/httptest"
6 "strings"
7 "testing"
8
9 "gitbay.org/gitbay/internal/config"
10)
11
12// Markdown headings carry ids, so a README section can be deep-linked
13// the way org headings already could (#132).
14func TestMarkdownHeadingAnchors(t *testing.T) {
15 out := string(renderReadme("README.md", []byte("# Getting started\n\n## Two words\n\n- [ ] a task\n")))
16 for _, want := range []string{`id="getting-started"`, `id="two-words"`, `type="checkbox"`} {
17 if !strings.Contains(out, want) {
18 t.Errorf("rendered markdown lacks %s:\n%s", want, out)
19 }
20 }
21}
22
23// The stylesheet carries an ETag and a cache lifetime; a revalidation
24// with the same tag is a 304 with no body (#132).
25func TestStylesheetRevalidates(t *testing.T) {
26 s := New(config.Default(), nil)
27 first := httptest.NewRecorder()
28 s.stylesheet(first, httptest.NewRequest("GET", "/static/style.css", nil))
29 tag := first.Header().Get("ETag")
30 if first.Code != 200 || tag == "" || first.Body.Len() == 0 || !strings.Contains(first.Header().Get("Cache-Control"), "max-age") {
31 t.Fatalf("first fetch: %d etag=%q cc=%q bytes=%d", first.Code, tag, first.Header().Get("Cache-Control"), first.Body.Len())
32 }
33 req := httptest.NewRequest("GET", "/static/style.css", nil)
34 req.Header.Set("If-None-Match", tag)
35 second := httptest.NewRecorder()
36 s.stylesheet(second, req)
37 if second.Code != http.StatusNotModified || second.Body.Len() != 0 {
38 t.Fatalf("revalidation: %d with %d bytes", second.Code, second.Body.Len())
39 }
40}