File view with blame information shown in the left gutter beside each line.

barerepo / server / e2e/cachestatic_test.go
76 lines · 2.6kb · 133728eaa05486504991b230f9d4c7e986b7defc
log files threads runs releases config jump to file t
133728e barerepo 1mo
1
package e2e
133728e barerepo 1mo
2
133728e barerepo 1mo
3
import (
133728e barerepo 1mo
4
"net/http"
133728e barerepo 1mo
5
"os/exec"
133728e barerepo 1mo
6
"strings"
133728e barerepo 1mo
7
"testing"
133728e barerepo 1mo
8
)
133728e barerepo 1mo
9
133728e barerepo 1mo
10
// A stylesheet a browser cannot keep is refetched on every click, and the page flashes unstyled.
133728e barerepo 1mo
11
func TestTheStylesheetIsKeptBetweenPages(t *testing.T) {
133728e barerepo 1mo
12
if _, err := exec.LookPath("git"); err != nil {
133728e barerepo 1mo
13
t.Skip("git is not installed")
133728e barerepo 1mo
14
}
133728e barerepo 1mo
15
in := newInstance(t)
133728e barerepo 1mo
16
john := in.account("john")
133728e barerepo 1mo
17
seed(t, in, john, "john", "johnbot")
133728e barerepo 1mo
18
133728e barerepo 1mo
19
// Every page asks for the stylesheet under the running version, which is what makes it keepable.
133728e barerepo 1mo
20
_, _, body := get(t, in.http.URL+"/john/johnbot")
133728e barerepo 1mo
21
i := strings.Index(body, "/static/barerepo.css")
133728e barerepo 1mo
22
if i < 0 {
133728e barerepo 1mo
23
t.Fatalf("no stylesheet on the page:\n%s", body)
133728e barerepo 1mo
24
}
133728e barerepo 1mo
25
href := body[i : strings.Index(body[i:], `"`)+i]
133728e barerepo 1mo
26
if !strings.Contains(href, "?v=") {
133728e barerepo 1mo
27
t.Errorf("the stylesheet url carries no version, so it can only be cached by guessing: %s", href)
133728e barerepo 1mo
28
}
133728e barerepo 1mo
29
133728e barerepo 1mo
30
resp, err := http.Get(in.http.URL + href)
133728e barerepo 1mo
31
if err != nil {
133728e barerepo 1mo
32
t.Fatal(err)
133728e barerepo 1mo
33
}
133728e barerepo 1mo
34
defer resp.Body.Close()
133728e barerepo 1mo
35
cc := resp.Header.Get("Cache-Control")
133728e barerepo 1mo
36
if !strings.Contains(cc, "immutable") || !strings.Contains(cc, "max-age=") {
133728e barerepo 1mo
37
t.Errorf("the versioned stylesheet answered Cache-Control %q, so a browser refetches it", cc)
133728e barerepo 1mo
38
}
133728e barerepo 1mo
39
133728e barerepo 1mo
40
// And a bookmarked url with no version still says something, or that request flashes too.
133728e barerepo 1mo
41
plain, err := http.Get(in.http.URL + "/static/barerepo.css")
133728e barerepo 1mo
42
if err != nil {
133728e barerepo 1mo
43
t.Fatal(err)
133728e barerepo 1mo
44
}
133728e barerepo 1mo
45
defer plain.Body.Close()
133728e barerepo 1mo
46
if plain.Header.Get("Cache-Control") == "" {
133728e barerepo 1mo
47
t.Error("an unversioned stylesheet answers with no caching policy at all")
133728e barerepo 1mo
48
}
133728e barerepo 1mo
49
}
133728e barerepo 1mo
50
133728e barerepo 1mo
51
// A url cached for a year must change when the file does, or an edit reaches nobody who has it.
133728e barerepo 1mo
52
func TestTheStylesheetUrlIsNotTheReleaseNumber(t *testing.T) {
133728e barerepo 1mo
53
if _, err := exec.LookPath("git"); err != nil {
133728e barerepo 1mo
54
t.Skip("git is not installed")
133728e barerepo 1mo
55
}
133728e barerepo 1mo
56
in := newInstance(t)
133728e barerepo 1mo
57
john := in.account("john")
133728e barerepo 1mo
58
seed(t, in, john, "john", "johnbot")
133728e barerepo 1mo
59
133728e barerepo 1mo
60
_, _, body := get(t, in.http.URL+"/john/johnbot")
133728e barerepo 1mo
61
i := strings.Index(body, "/static/barerepo.css")
133728e barerepo 1mo
62
href := body[i : strings.Index(body[i:], `"`)+i]
133728e barerepo 1mo
63
_, tag, _ := strings.Cut(href, "?v=")
133728e barerepo 1mo
64
133728e barerepo 1mo
65
// The version does not move between builds, so a stylesheet keyed on it is kept after an edit.
133728e barerepo 1mo
66
if tag == "" || strings.Contains(body, "?v=0.1.0") {
133728e barerepo 1mo
67
t.Errorf("the stylesheet is cached for a year under %q, which the next edit will not change", tag)
133728e barerepo 1mo
68
}
133728e barerepo 1mo
69
// Every page agrees, or one of them hands out a url that pins an old stylesheet.
133728e barerepo 1mo
70
for _, path := range []string{"/john/johnbot/files", "/john/johnbot/config", "/signin"} {
133728e barerepo 1mo
71
_, _, page := get(t, in.http.URL+path)
133728e barerepo 1mo
72
if !strings.Contains(page, "?v="+tag) {
133728e barerepo 1mo
73
t.Errorf("%s asks for a different stylesheet url than the log page does", path)
133728e barerepo 1mo
74
}
133728e barerepo 1mo
75
}
133728e barerepo 1mo
76
}
history · rawbarerepo 0.1.0