oficina / sheets Public

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

Plain git — no account needed to clone.

Download

Download this file

1package sheets
2
3import (
4 "net/http"
5 "net/http/httptest"
6 "strings"
7 "testing"
8)
9
10// Every page is parsed at startup. A template that references a
11// nonexistent partial or misspells a field panics there, which is loud —
12// but only if something actually parses the tree, so this asserts the
13// set is non-empty and reachable rather than trusting the package var.
14func TestEveryTemplateParses(t *testing.T) {
15 if len(pages) == 0 {
16 t.Fatal("no pages parsed; the embedded templates directory is empty or the glob is wrong")
17 }
18 for _, want := range []string{"index", "signin"} {
19 if _, ok := pages[want]; !ok {
20 t.Errorf("page %q did not parse", want)
21 }
22 }
23 if _, ok := pages["layout"]; ok {
24 t.Error("layout.html was parsed as a page; it is the shell, not a page")
25 }
26}
27
28func TestStaticAssetsAreServed(t *testing.T) {
29 app := New(Config{})
30 for _, name := range []string{
31 "/static/tokens.css",
32 "/static/theme.css",
33 "/static/rastrillo.js",
34 "/static/sheets.css",
35 } {
36 rec := do(t, app, httptest.NewRequest(http.MethodGet, name, nil))
37 if rec.Code != http.StatusOK {
38 t.Errorf("GET %s = %d, want 200", name, rec.Code)
39 }
40 if rec.Body.Len() == 0 {
41 t.Errorf("GET %s served an empty body", name)
42 }
43 }
44}
45
46// The URL carries the content hash, so a browser can cache it forever
47// and still get the new one after a deploy. A page linking the bare
48// path would be served stale for as long as the cache lived.
49func TestPagesLinkFingerprintedAssets(t *testing.T) {
50 rec := do(t, New(Config{}), httptest.NewRequest(http.MethodGet, "/", nil))
51 body := rec.Body.String()
52 if strings.Contains(body, `href="/static/tokens.css"`) {
53 t.Fatal("the page links tokens.css unfingerprinted; a deploy would not bust the cache")
54 }
55 if !strings.Contains(body, "/static/tokens.") {
56 t.Fatalf("the page does not link tokens.css at all:\n%s", body)
57 }
58}
59
60// A template error halfway through a direct write has already sent a
61// 200 and half a page, and the browser shows a truncated document with
62// no sign anything went wrong.
63func TestRenderOfAnUnknownPageIs500NotAPanic(t *testing.T) {
64 rec := httptest.NewRecorder()
65 req := httptest.NewRequest(http.MethodGet, "/", nil)
66 New(Config{}).render(rec, req, "no-such-page", titled("T"), http.StatusOK, nil)
67 if rec.Code != http.StatusInternalServerError {
68 t.Fatalf("rendering an unknown page = %d, want 500", rec.Code)
69 }
70}
71