|
|
@@ -1,12 +1,15 @@
|
|
|
package main
|
|
|
|
|
|
import (
|
|
|
+ "crypto/sha256"
|
|
|
+ "encoding/hex"
|
|
|
"encoding/xml"
|
|
|
"io"
|
|
|
"net/http"
|
|
|
"net/http/httptest"
|
|
|
"os"
|
|
|
"path/filepath"
|
|
|
+ "regexp"
|
|
|
"strings"
|
|
|
"testing"
|
|
|
)
|
|
|
@@ -143,6 +146,34 @@ func TestStatic(t *testing.T) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func TestStylesheetCacheBusting(t *testing.T) {
|
|
|
+ srv, _ := newTestServer(t, nil)
|
|
|
+ _, _, page := get(t, srv, "/")
|
|
|
+ m := regexp.MustCompile(`<link rel="stylesheet" href="(/static/style\.css\?v=([0-9a-f]{10}))">`).FindStringSubmatch(page)
|
|
|
+ if m == nil {
|
|
|
+ t.Fatalf("no hashed stylesheet link in:\n%s", page)
|
|
|
+ }
|
|
|
+ b, err := staticFS.ReadFile("static/style.css")
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+ sum := sha256.Sum256(b)
|
|
|
+ if want := hex.EncodeToString(sum[:])[:10]; m[2] != want {
|
|
|
+ t.Errorf("hash = %s, want %s", m[2], want)
|
|
|
+ }
|
|
|
+
|
|
|
+ for path, want := range map[string]string{
|
|
|
+ m[1]: "public, max-age=31536000, immutable",
|
|
|
+ "/static/style.css": "public, max-age=3600",
|
|
|
+ "/static/style.css?v=0000000000": "public, max-age=3600",
|
|
|
+ } {
|
|
|
+ status, h, _ := get(t, srv, path)
|
|
|
+ if status != 200 || h.Get("Cache-Control") != want {
|
|
|
+ t.Errorf("%s: status = %d, Cache-Control = %q, want %q", path, status, h.Get("Cache-Control"), want)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
func TestFeed(t *testing.T) {
|
|
|
srv, _ := newTestServer(t, sample)
|
|
|
status, h, body := get(t, srv, "/feed.xml")
|