| new file mode 100644 |
| index 000000000..39b6bb38c |
| --- /dev/null |
| +++ b/internal/edge/assetadmit_test.go |
| @@ -0,0 +1,338 @@ |
| +package edge |
| + |
| +import ( |
| + "net/http" |
| + "strings" |
| + "testing" |
| + "time" |
| +) |
| + |
| +// Not acceptance: these pin the pure admission, expiry and stored-header |
| +// functions only. The wired fill is exercised by the plan's acceptance tests. |
| + |
| +var admitT0 = time.Unix(1_800_000_000, 0) |
| + |
| +// admitHeaders is a response that stores: every rule passes. |
| +func admitHeaders() http.Header { |
| + h := http.Header{} |
| + h.Set("Cache-Control", "public, max-age=31536000, immutable") |
| + h.Set("Content-Type", "text/css; charset=utf-8") |
| + return h |
| +} |
| + |
| +func TestAssetAdmitTable(t *testing.T) { |
| + const year = 365 * 24 * time.Hour |
| + tests := []struct { |
| + name string |
| + status int |
| + mutate func(h http.Header) |
| + declared bool |
| + store bool |
| + reason string |
| + lifetime time.Duration |
| + }{ |
| + {name: "immutable css stores", status: 200, declared: true, store: true, lifetime: year}, |
| + {name: "s-maxage beats max-age", status: 200, declared: true, store: true, lifetime: 60 * time.Second, |
| + mutate: func(h http.Header) { h.Set("Cache-Control", "public, max-age=31536000, s-maxage=60") }}, |
| + {name: "max-age over a year is capped", status: 200, declared: true, store: true, lifetime: year, |
| + mutate: func(h http.Header) { h.Set("Cache-Control", "max-age=999999999") }}, |
| + {name: "vary accept-encoding stores, any case and spacing", status: 200, declared: true, store: true, lifetime: year, |
| + mutate: func(h http.Header) { h.Set("Vary", " accept-ENCODING ") }}, |
| + {name: "identity content-encoding stores", status: 200, declared: true, store: true, lifetime: year, |
| + mutate: func(h http.Header) { h.Set("Content-Encoding", "identity") }}, |
| + {name: "allow-origin star stores", status: 200, declared: true, store: true, lifetime: year, |
| + mutate: func(h http.Header) { h.Set("Access-Control-Allow-Origin", "*") }}, |
| + {name: "content-length at the cap stores", status: 200, declared: true, store: true, lifetime: year, |
| + mutate: func(h http.Header) { h.Set("Content-Length", "33554432") }}, |
| + |
| + {name: "206 refused", status: 206, declared: true, reason: "status"}, |
| + {name: "404 refused", status: 404, declared: true, reason: "status"}, |
| + {name: "set-cookie refused", status: 200, declared: true, reason: "set-cookie", |
| + mutate: func(h http.Header) { h.Add("Set-Cookie", "a=b") }}, |
| + {name: "no cache-control refused", status: 200, declared: true, reason: "no-cache-control", |
| + mutate: func(h http.Header) { h.Del("Cache-Control") }}, |
| + {name: "private refused", status: 200, declared: true, reason: "cache-control", |
| + mutate: func(h http.Header) { h.Set("Cache-Control", "private, max-age=600") }}, |
| + {name: "no-store refused", status: 200, declared: true, reason: "cache-control", |
| + mutate: func(h http.Header) { h.Set("Cache-Control", "no-store, max-age=600") }}, |
| + {name: "no-cache refused", status: 200, declared: true, reason: "cache-control", |
| + mutate: func(h http.Header) { h.Set("Cache-Control", "no-cache, max-age=600") }}, |
| + {name: "private on a second line refused", status: 200, declared: true, reason: "cache-control", |
| + mutate: func(h http.Header) { h.Add("Cache-Control", "private") }}, |
| + {name: "no lifetime refused", status: 200, declared: true, reason: "no-lifetime", |
| + mutate: func(h http.Header) { h.Set("Cache-Control", "public") }}, |
| + {name: "zero lifetime refused", status: 200, declared: true, reason: "no-lifetime", |
| + mutate: func(h http.Header) { h.Set("Cache-Control", "max-age=0") }}, |
| + {name: "zero s-maxage refused", status: 200, declared: true, reason: "no-lifetime", |
| + mutate: func(h http.Header) { h.Set("Cache-Control", "max-age=600, s-maxage=0") }}, |
| + {name: "malformed max-age refused", status: 200, declared: true, reason: "no-lifetime", |
| + mutate: func(h http.Header) { h.Set("Cache-Control", "max-age=soon") }}, |
| + {name: "vary origin refused", status: 200, declared: true, reason: "vary", |
| + mutate: func(h http.Header) { h.Set("Vary", "Origin") }}, |
| + {name: "vary cookie refused", status: 200, declared: true, reason: "vary", |
| + mutate: func(h http.Header) { h.Set("Vary", "Accept-Encoding, Cookie") }}, |
| + {name: "vary star refused", status: 200, declared: true, reason: "vary", |
| + mutate: func(h http.Header) { h.Set("Vary", "*") }}, |
| + {name: "vary on a second line refused", status: 200, declared: true, reason: "vary", |
| + mutate: func(h http.Header) { h.Set("Vary", "Accept-Encoding"); h.Add("Vary", "Accept") }}, |
| + {name: "gzip content-encoding refused", status: 200, declared: true, reason: "content-encoding", |
| + mutate: func(h http.Header) { h.Set("Content-Encoding", "gzip") }}, |
| + {name: "no content-type refused", status: 200, declared: true, reason: "content-type", |
| + mutate: func(h http.Header) { h.Del("Content-Type") }}, |
| + {name: "blank content-type refused", status: 200, declared: true, reason: "content-type", |
| + mutate: func(h http.Header) { h.Set("Content-Type", " ") }}, |
| + {name: "html refused", status: 200, declared: true, reason: "html", |
| + mutate: func(h http.Header) { h.Set("Content-Type", "text/html; charset=utf-8") }}, |
| + {name: "html in any case refused", status: 200, declared: true, reason: "html", |
| + mutate: func(h http.Header) { h.Set("Content-Type", " Text/HTML") }}, |
| + {name: "xhtml refused", status: 200, declared: true, reason: "html", |
| + mutate: func(h http.Header) { h.Set("Content-Type", "application/xhtml+xml") }}, |
| + {name: "allow-origin specific refused", status: 200, declared: true, reason: "allow-origin", |
| + mutate: func(h http.Header) { h.Set("Access-Control-Allow-Origin", "https://example.com") }}, |
| + {name: "undeclared refused", status: 200, declared: false, reason: "undeclared"}, |
| + {name: "content-length over the cap refused", status: 200, declared: true, reason: "too-large", |
| + mutate: func(h http.Header) { h.Set("Content-Length", "33554433") }}, |
| + {name: "unreadable content-length refused", status: 200, declared: true, reason: "content-length", |
| + mutate: func(h http.Header) { h.Set("Content-Length", "lots") }}, |
| + {name: "already stale refused", status: 200, declared: true, reason: "expired", |
| + mutate: func(h http.Header) { h.Set("Cache-Control", "max-age=30"); h.Set("Age", "30") }}, |
| + } |
| + for _, tc := range tests { |
| + t.Run(tc.name, func(t *testing.T) { |
| + h := admitHeaders() |
| + if tc.mutate != nil { |
| + tc.mutate(h) |
| + } |
| + v := assetAdmit(tc.status, h, admitT0, admitT0, tc.declared) |
| + if v.Store != tc.store { |
| + t.Fatalf("Store = %v (reason %q), want %v", v.Store, v.Reason, tc.store) |
| + } |
| + if !tc.store { |
| + if v.Reason != tc.reason { |
| + t.Fatalf("Reason = %q, want %q", v.Reason, tc.reason) |
| + } |
| + if !v.Expires.IsZero() { |
| + t.Fatalf("a refusal carries Expires %v", v.Expires) |
| + } |
| + return |
| + } |
| + if v.Reason != "" { |
| + t.Fatalf("a stored verdict carries Reason %q", v.Reason) |
| + } |
| + if v.Lifetime != tc.lifetime { |
| + t.Fatalf("Lifetime = %v, want %v", v.Lifetime, tc.lifetime) |
| + } |
| + if want := admitT0.Add(tc.lifetime); !v.Expires.Equal(want) { |
| + t.Fatalf("Expires = %v, want %v", v.Expires, want) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestAssetAdmitGzipFollowsCompressible(t *testing.T) { |
| + h := admitHeaders() |
| + h.Set("Content-Length", "100000") |
| + if v := assetAdmit(200, h, admitT0, admitT0, true); !v.Store || !v.Gzip { |
| + t.Fatalf("large css: Store=%v Gzip=%v, want both", v.Store, v.Gzip) |
| + } |
| + h.Set("Content-Type", "image/png") |
| + if v := assetAdmit(200, h, admitT0, admitT0, true); !v.Store || v.Gzip { |
| + t.Fatalf("png: Store=%v Gzip=%v, want stored and not gzip", v.Store, v.Gzip) |
| + } |
| + h = admitHeaders() |
| + h.Set("Content-Length", "100000") |
| + h.Set("Cache-Control", "public, max-age=600, no-transform") |
| + if v := assetAdmit(200, h, admitT0, admitT0, true); !v.Store || v.Gzip { |
| + t.Fatalf("no-transform: Store=%v Gzip=%v, want stored and not gzip", v.Store, v.Gzip) |
| + } |
| +} |
| + |
| +func TestAssetAdmitRefusesALongLivedHTMLFallback(t *testing.T) { |
| + h := http.Header{} |
| + h.Set("Content-Type", "text/html; charset=utf-8") |
| + h.Set("Cache-Control", "public, max-age=31536000") |
| + v := assetAdmit(200, h, admitT0, admitT0, true) |
| + if v.Store || v.Reason != "html" { |
| + t.Fatalf("Store=%v Reason=%q, want an html refusal", v.Store, v.Reason) |
| + } |
| +} |
| + |
| +func TestAssetAdmitRefusesNoContentType(t *testing.T) { |
| + h := http.Header{} |
| + h.Set("Cache-Control", "public, max-age=31536000") |
| + v := assetAdmit(200, h, admitT0, admitT0, true) |
| + if v.Store || v.Reason != "content-type" { |
| + t.Fatalf("Store=%v Reason=%q, want a content-type refusal", v.Store, v.Reason) |
| + } |
| +} |
| + |
| +func TestAssetAdmitRefusesAnUndeclaringBuild(t *testing.T) { |
| + v := assetAdmit(200, admitHeaders(), admitT0, admitT0, false) |
| + if v.Store || v.Reason != "undeclared" { |
| + t.Fatalf("Store=%v Reason=%q, want an undeclared refusal", v.Store, v.Reason) |
| + } |
| +} |
| + |
| +func TestExpiryIsFromResponseTime(t *testing.T) { |
| + // The request left at 0 and the answer arrived at 20 with Age: 0. The |
| + // corrected initial age is the 20 s response delay, so a 30 s lifetime |
| + // ends at 30, not at 50. |
| + h := admitHeaders() |
| + h.Set("Cache-Control", "max-age=30") |
| + h.Set("Age", "0") |
| + req := admitT0 |
| + resp := admitT0.Add(20 * time.Second) |
| + v := assetAdmit(200, h, req, resp, true) |
| + if !v.Store { |
| + t.Fatalf("not stored: %q", v.Reason) |
| + } |
| + if want := admitT0.Add(30 * time.Second); !v.Expires.Equal(want) { |
| + t.Fatalf("Expires = %v, want %v", v.Expires, want) |
| + } |
| + if v.Lifetime != 30*time.Second { |
| + t.Fatalf("Lifetime = %v, want 30s", v.Lifetime) |
| + } |
| +} |
| + |
| +func TestAnAlreadyExpiredResponseIsNotStored(t *testing.T) { |
| + h := admitHeaders() |
| + h.Set("Cache-Control", "max-age=30") |
| + h.Set("Age", "45") |
| + v := assetAdmit(200, h, admitT0, admitT0, true) |
| + if v.Store || v.Reason != "expired" { |
| + t.Fatalf("Store=%v Reason=%q, want an expired refusal", v.Store, v.Reason) |
| + } |
| +} |
| + |
| +func TestCorrectedInitialAge(t *testing.T) { |
| + req := admitT0 |
| + resp := admitT0.Add(4 * time.Second) |
| + tests := []struct { |
| + name string |
| + set func(h http.Header) |
| + want time.Duration |
| + }{ |
| + {name: "no Age, no Date: the response delay", want: 4 * time.Second}, |
| + {name: "Age plus delay beats a small Age", want: 14 * time.Second, |
| + set: func(h http.Header) { h.Set("Age", "10") }}, |
| + {name: "apparent age beats Age plus delay", want: 60 * time.Second, |
| + set: func(h http.Header) { |
| + h.Set("Age", "1") |
| + h.Set("Date", resp.Add(-60*time.Second).UTC().Format(http.TimeFormat)) |
| + }}, |
| + {name: "a Date in the future is not negative age", want: 4 * time.Second, |
| + set: func(h http.Header) { h.Set("Date", resp.Add(time.Hour).UTC().Format(http.TimeFormat)) }}, |
| + {name: "a garbled Age counts as zero", want: 4 * time.Second, |
| + set: func(h http.Header) { h.Set("Age", "old") }}, |
| + {name: "a negative Age counts as zero", want: 4 * time.Second, |
| + set: func(h http.Header) { h.Set("Age", "-5") }}, |
| + {name: "a huge Age is clamped, not wrapped", want: maxAssetAge + 4*time.Second, |
| + set: func(h http.Header) { h.Set("Age", "99999999999999999999") }}, |
| + } |
| + for _, tc := range tests { |
| + t.Run(tc.name, func(t *testing.T) { |
| + h := http.Header{} |
| + if tc.set != nil { |
| + tc.set(h) |
| + } |
| + if got := correctedInitialAge(h, req, resp); got != tc.want { |
| + t.Fatalf("correctedInitialAge = %v, want %v", got, tc.want) |
| + } |
| + }) |
| + } |
| + // A response time before the request time (a clock step) is a zero delay. |
| + if got := correctedInitialAge(http.Header{}, resp, req); got != 0 { |
| + t.Fatalf("backwards clock: correctedInitialAge = %v, want 0", got) |
| + } |
| +} |
| + |
| +func TestStoredHeaderKeepsCSPAndCORP(t *testing.T) { |
| + in := admitHeaders() |
| + in.Set("Content-Security-Policy", "default-src 'self'") |
| + in.Set("Cross-Origin-Resource-Policy", "same-site") |
| + in.Set("ETag", `"abc"`) |
| + in.Set("Last-Modified", "Mon, 02 Jan 2006 15:04:05 GMT") |
| + in.Add("Link", "</a>; rel=preload") |
| + in.Add("Link", "</b>; rel=preload") |
| + got := assetStoredHeader(in) |
| + for _, name := range []string{"Content-Security-Policy", "Cross-Origin-Resource-Policy", "ETag", "Last-Modified", "Cache-Control", "Content-Type"} { |
| + if got.Get(name) != in.Get(name) || got.Get(name) == "" { |
| + t.Errorf("%s = %q, want %q", name, got.Get(name), in.Get(name)) |
| + } |
| + } |
| + if len(got.Values("Link")) != 2 { |
| + t.Errorf("Link values = %v, want both kept", got.Values("Link")) |
| + } |
| + got.Set("Content-Security-Policy", "changed") |
| + if in.Get("Content-Security-Policy") != "default-src 'self'" { |
| + t.Error("the stored header shares storage with its input") |
| + } |
| +} |
| + |
| +func TestStoredHeaderDropsHopAndPlatformHeaders(t *testing.T) { |
| + in := admitHeaders() |
| + in.Set("Connection", "close, X-Hop-Custom") |
| + in.Set("X-Hop-Custom", "1") |
| + in.Set("Keep-Alive", "timeout=5") |
| + in.Set("Transfer-Encoding", "chunked") |
| + in.Set("Upgrade", "h2c") |
| + in.Set("Te", "trailers") |
| + in.Set("Trailer", "Expires") |
| + in.Set("Proxy-Authenticate", "Basic") |
| + in.Set("Proxy-Connection", "keep-alive") |
| + in.Set("Date", "Mon, 02 Jan 2006 15:04:05 GMT") |
| + in.Set("Age", "12") |
| + in.Set("Content-Length", "123") |
| + in.Set("Carlos-Purge", "x") |
| + in.Set("Carlos-Purge-Host", "shop.example.com") |
| + in.Set("Carlos-Cache", "miss") |
| + in.Set("X-Carlos-Asset-Expires", "99") |
| + in.Set("X-Carlos-Whatever", "1") |
| + in.Set("X-Custom", "keep me") |
| + // A header map built by hand need not be canonical. |
| + in["x-carlos-lower"] = []string{"1"} |
| + got := assetStoredHeader(in) |
| + for _, name := range []string{ |
| + "Connection", "X-Hop-Custom", "Keep-Alive", "Transfer-Encoding", "Upgrade", "Te", "Trailer", |
| + "Proxy-Authenticate", "Proxy-Connection", "Date", "Age", "Content-Length", |
| + "Carlos-Purge", "Carlos-Purge-Host", "Carlos-Cache", "X-Carlos-Asset-Expires", "X-Carlos-Whatever", |
| + } { |
| + if _, ok := got[name]; ok { |
| + t.Errorf("%s survived: %v", name, got[name]) |
| + } |
| + } |
| + if _, ok := got["x-carlos-lower"]; ok { |
| + t.Error("a non-canonical x-carlos-* key survived") |
| + } |
| + if got.Get("X-Custom") != "keep me" || got.Get("Content-Type") == "" || got.Get("Cache-Control") == "" { |
| + t.Errorf("end-to-end headers lost: %v", got) |
| + } |
| + if in.Get("Connection") == "" || in.Get("Date") == "" { |
| + t.Error("the input was modified") |
| + } |
| +} |
| + |
| +func TestTrailerVetoAnnouncedAndUnannounced(t *testing.T) { |
| + tests := []struct { |
| + name string |
| + t http.Header |
| + want bool |
| + }{ |
| + {name: "nil", t: nil, want: false}, |
| + {name: "empty", t: http.Header{}, want: false}, |
| + {name: "other trailer", t: http.Header{"Expires": {"x"}}, want: false}, |
| + {name: "announced with a value", t: http.Header{"Set-Cookie": {"a=b"}}, want: true}, |
| + {name: "announced and empty", t: http.Header{"Set-Cookie": nil}, want: true}, |
| + {name: "announced, lower case", t: http.Header{"set-cookie": {"a=b"}}, want: true}, |
| + {name: "unannounced", t: http.Header{http.TrailerPrefix + "Set-Cookie": {"a=b"}}, want: true}, |
| + {name: "unannounced, odd case", t: http.Header{strings.ToLower(http.TrailerPrefix) + "set-cookie": {"a=b"}}, want: true}, |
| + {name: "unannounced other", t: http.Header{http.TrailerPrefix + "Expires": {"x"}}, want: false}, |
| + } |
| + for _, tc := range tests { |
| + t.Run(tc.name, func(t *testing.T) { |
| + if got := assetTrailerVeto(tc.t); got != tc.want { |
| + t.Fatalf("assetTrailerVeto(%v) = %v, want %v", tc.t, got, tc.want) |
| + } |
| + }) |
| + } |
| +} |