package e2e import ( "net/http" "os/exec" "strings" "testing" ) // A stylesheet a browser cannot keep is refetched on every click, and the page flashes unstyled. func TestTheStylesheetIsKeptBetweenPages(t *testing.T) { if _, err := exec.LookPath("git"); err != nil { t.Skip("git is not installed") } in := newInstance(t) john := in.account("john") seed(t, in, john, "john", "johnbot") // Every page asks for the stylesheet under the running version, which is what makes it keepable. _, _, body := get(t, in.http.URL+"/john/johnbot") i := strings.Index(body, "/static/barerepo.css") if i < 0 { t.Fatalf("no stylesheet on the page:\n%s", body) } href := body[i : strings.Index(body[i:], `"`)+i] if !strings.Contains(href, "?v=") { t.Errorf("the stylesheet url carries no version, so it can only be cached by guessing: %s", href) } resp, err := http.Get(in.http.URL + href) if err != nil { t.Fatal(err) } defer resp.Body.Close() cc := resp.Header.Get("Cache-Control") if !strings.Contains(cc, "immutable") || !strings.Contains(cc, "max-age=") { t.Errorf("the versioned stylesheet answered Cache-Control %q, so a browser refetches it", cc) } // And a bookmarked url with no version still says something, or that request flashes too. plain, err := http.Get(in.http.URL + "/static/barerepo.css") if err != nil { t.Fatal(err) } defer plain.Body.Close() if plain.Header.Get("Cache-Control") == "" { t.Error("an unversioned stylesheet answers with no caching policy at all") } } // A url cached for a year must change when the file does, or an edit reaches nobody who has it. func TestTheStylesheetUrlIsNotTheReleaseNumber(t *testing.T) { if _, err := exec.LookPath("git"); err != nil { t.Skip("git is not installed") } in := newInstance(t) john := in.account("john") seed(t, in, john, "john", "johnbot") _, _, body := get(t, in.http.URL+"/john/johnbot") i := strings.Index(body, "/static/barerepo.css") href := body[i : strings.Index(body[i:], `"`)+i] _, tag, _ := strings.Cut(href, "?v=") // The version does not move between builds, so a stylesheet keyed on it is kept after an edit. if tag == "" || strings.Contains(body, "?v=0.1.0") { t.Errorf("the stylesheet is cached for a year under %q, which the next edit will not change", tag) } // Every page agrees, or one of them hands out a url that pins an old stylesheet. for _, path := range []string{"/john/johnbot/files", "/john/johnbot/config", "/signin"} { _, _, page := get(t, in.http.URL+path) if !strings.Contains(page, "?v="+tag) { t.Errorf("%s asks for a different stylesheet url than the log page does", path) } } }