| index 9e5189081..92c6a45bb 100644 |
| --- a/internal/edge/forwardha_test.go |
| +++ b/internal/edge/forwardha_test.go |
| @@ -53,6 +53,9 @@ const ( |
| fwPassword = "correct horse" |
| ) |
| |
| +// fwBigBody is a page large and repetitive enough for the edge to gzip. |
| +var fwBigBody = strings.Repeat("<p>forwarded and compressible</p>\n", 2048) |
| + |
| var fwHosts = []string{fwHost, fwGatedHost, fwAssetHost, fwRateHost, fwLegacyHost} |
| |
| // fwAct is a box's activator: it wakes nothing (the route's TCP upstream |
| @@ -71,7 +74,16 @@ type fwNote struct { |
| |
| var _ HAForwarder = (*fwAct)(nil) |
| |
| -func (a *fwAct) ServeHTTP(http.ResponseWriter, *http.Request, registry.Route, http.Handler) bool { |
| +func (a *fwAct) ServeHTTP(w http.ResponseWriter, r *http.Request, _ registry.Route, _ http.Handler) bool { |
| + // The platform's own refusal of a forwarded request (Task 3's answer): |
| + // written by the activator, never by the app. |
| + if r.URL.Path == "/platform-held" { |
| + w.Header().Set(holderHeader, r.URL.Query().Get("ref")) |
| + w.Header().Set("Retry-After", "1") |
| + MarkAnswered(r) |
| + http.Error(w, "instance is waking elsewhere; retry shortly", http.StatusServiceUnavailable) |
| + return true |
| + } |
| return false |
| } |
| func (a *fwAct) RetireExcept(map[string]bool) {} |
| @@ -101,7 +113,8 @@ func (fwPlainAct) RetireExcept(map[string]bool) {} |
| |
| // fwApp is one box's app behind every forward host. Its ordinary body names |
| // the box, the host and the path; /missing is a 404; /holder answers 503 with |
| -// X-Carlos-Holder set to its ref query (none when the query is empty); |
| +// X-Carlos-Holder set to its ref query (none when the query is empty), or |
| +// the status query's status; /big is a large compressible page; |
| // /assets/ is a cacheable script, and /assets/held.js waits on the first |
| // request until the test releases it; an upgrade echoes with the box's name. |
| type fwAppServer struct { |
| @@ -130,8 +143,15 @@ func fwApp(t *testing.T, box string) *fwAppServer { |
| if ref := r.URL.Query().Get("ref"); ref != "" { |
| w.Header().Set(holderHeader, ref) |
| } |
| + status := http.StatusServiceUnavailable |
| + if r.URL.Query().Get("status") == "200" { |
| + status = http.StatusOK |
| + } |
| w.Header().Set("Retry-After", "1") |
| - http.Error(w, "held", http.StatusServiceUnavailable) |
| + http.Error(w, "held", status) |
| + case r.URL.Path == "/big": |
| + w.Header().Set("Content-Type", "text/html; charset=utf-8") |
| + io.WriteString(w, fwBigBody) |
| case r.URL.Path == "/assets/held.js": |
| a.heldRequests.Add(1) |
| if a.first() { |
| @@ -302,7 +322,29 @@ func fwDialTo(t *testing.T, addr string) { |
| |
| func (x *fwScene) fetch(t *testing.T, host, path string, hdr ...string) (*http.Response, string) { |
| t.Helper() |
| - return x.get(t, x.a, "https://"+host+path, e2eHeaders(hdr), true) |
| + return x.do(t, http.MethodGet, host, path, hdr...) |
| +} |
| + |
| +// do is fetch with the method given, through annie's client. |
| +func (x *fwScene) do(t *testing.T, method, host, path string, hdr ...string) (*http.Response, string) { |
| + t.Helper() |
| + req, err := http.NewRequest(method, "https://"+host+path, nil) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for k, v := range e2eHeaders(hdr) { |
| + req.Header.Set(k, v) |
| + } |
| + resp, err := x.a.client.Do(req) |
| + if err != nil { |
| + t.Fatalf("%s %s%s: %v", method, host, path, err) |
| + } |
| + b, err := io.ReadAll(resp.Body) |
| + resp.Body.Close() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return resp, string(b) |
| } |
| |
| func (x *fwScene) fetchAsync(host, path string) <-chan e2eResult { |
| @@ -442,27 +484,33 @@ func TestFrontForwardRunsNoLocalStep(t *testing.T) { |
| }) |
| |
| t.Run("b assets", func(t *testing.T) { |
| - step := func(name, path string, wantStatus int, hdr ...string) *http.Response { |
| + step := func(name, method, path string, wantStatus int, hdr ...string) *http.Response { |
| t.Helper() |
| - resp, body := x.fetch(t, fwAssetHost, path, hdr...) |
| + resp, body := x.do(t, method, fwAssetHost, path, hdr...) |
| if resp.StatusCode != wantStatus { |
| t.Fatalf("%s = %d %q, want %d", name, resp.StatusCode, body, wantStatus) |
| } |
| + if method == http.MethodHead && body != "" { |
| + t.Fatalf("HEAD body = %q, want none", body) |
| + } |
| annie, betty := x.take(t, fwAssetHost) |
| wantNothing(t, name, annie) |
| wantOneDelivery(t, name, betty, len(body), wantStatus) |
| + if got := betty.obs[0].Method; got != method { |
| + t.Fatalf("%s: the holder observed a %s", name, got) |
| + } |
| return resp |
| } |
| // Carlos-Cache does not cross a hop (scrubBackhaulResponse), so the |
| // catch is read off betty's store and app. |
| - get := step("GET", "/assets/app.js", http.StatusOK) |
| + get := step("GET", http.MethodGet, "/assets/app.js", http.StatusOK) |
| appKey := assetcache.Key{Account: "abc", App: "shop", Gen: laGen, Host: fwAssetHost, Path: "/assets/app.js"} |
| if _, ok := x.bStore.Get(appKey); !ok { |
| t.Fatal("betty's asset cache did not catch the GET") |
| } |
| - step("HEAD", "/assets/app.js", http.StatusOK) |
| - step("range", "/assets/app.js", http.StatusPartialContent, "Range", "bytes=0-4") |
| - step("conditional", "/assets/app.js", http.StatusNotModified, "If-None-Match", get.Header.Get("ETag")) |
| + step("HEAD", http.MethodHead, "/assets/app.js", http.StatusOK) |
| + step("range", http.MethodGet, "/assets/app.js", http.StatusPartialContent, "Range", "bytes=0-4") |
| + step("conditional", http.MethodGet, "/assets/app.js", http.StatusNotModified, "If-None-Match", get.Header.Get("ETag")) |
| if n := x.bApp.assetRequests.Load(); n != 1 { |
| t.Fatalf("betty's app saw %d asset requests, want the one fill", n) |
| } |
| @@ -777,18 +825,70 @@ func TestForwardHADialFailureIs503RetryTen(t *testing.T) { |
| |
| func TestForwardHAReadsHolderHeader(t *testing.T) { |
| x := newFwScene(t, fwOpts{}) |
| - for i, ref := range []string{"carol", "none"} { |
| - resp, _ := x.fetch(t, fwHost, "/holder?ref="+ref) |
| + n := 0 |
| + note := func(t *testing.T, want ForwardResult) { |
| + t.Helper() |
| + n++ |
| + notes := x.aAct.noted() |
| + if len(notes) != n || notes[n-1] != (fwNote{fwHost, want}) { |
| + t.Fatalf("notes = %+v, want %+v last", notes, want) |
| + } |
| + } |
| + // The platform's refusal on the holder keeps its header, to the client |
| + // and to the note. |
| + for _, ref := range []string{"carol", "none"} { |
| + resp, _ := x.fetch(t, fwHost, "/platform-held?ref="+ref) |
| if resp.StatusCode != http.StatusServiceUnavailable || resp.Header.Get(holderHeader) != ref { |
| - t.Fatalf("GET = %d %s %q, want betty's 503 naming %s", resp.StatusCode, holderHeader, resp.Header.Get(holderHeader), ref) |
| + t.Fatalf("platform refusal = %d %s %q, want betty's 503 naming %s", resp.StatusCode, holderHeader, resp.Header.Get(holderHeader), ref) |
| } |
| - notes := x.aAct.noted() |
| - if len(notes) != i+1 || notes[i] != (fwNote{fwHost, ForwardResult{Outcome: ForwardDone, Status: http.StatusServiceUnavailable, Holder: ref}}) { |
| - t.Fatalf("notes = %+v, want ForwardDone 503 %q", notes, ref) |
| + note(t, ForwardResult{Outcome: ForwardDone, Status: http.StatusServiceUnavailable, Holder: ref}) |
| + } |
| + // An app's own X-Carlos-Holder never leaves the holder: not to the |
| + // client, not into the note. |
| + for _, tc := range []struct { |
| + query string |
| + status int |
| + }{{"ref=carol", http.StatusServiceUnavailable}, {"ref=none&status=200", http.StatusOK}} { |
| + resp, body := x.fetch(t, fwHost, "/holder?"+tc.query) |
| + if resp.StatusCode != tc.status || body != "held\n" { |
| + t.Fatalf("app %s = %d %q, want the app's %d", tc.query, resp.StatusCode, body, tc.status) |
| + } |
| + if v := resp.Header.Values(holderHeader); len(v) != 0 { |
| + t.Fatalf("app %s: the client received %s %q", tc.query, holderHeader, v) |
| } |
| + note(t, ForwardResult{Outcome: ForwardDone, Status: tc.status}) |
| } |
| } |
| |
| +// The forward relays the encoding the client asked for, so the bytes the |
| +// holder meters are the bytes the client receives: a client that asks for |
| +// no compression gets identity bytes from both boxes. |
| +func TestForwardHAHolderMetersTheBytesDelivered(t *testing.T) { |
| + x := newFwScene(t, fwOpts{}) |
| + tr := x.a.client.Transport.(*http.Transport).Clone() |
| + tr.DisableCompression = true |
| + client := &http.Client{Timeout: 30 * time.Second, Transport: tr} |
| + req, err := http.NewRequest(http.MethodGet, "https://"+fwHost+"/big", nil) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + resp, err := client.Do(req) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + b, err := io.ReadAll(resp.Body) |
| + resp.Body.Close() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if resp.StatusCode != http.StatusOK || string(b) != fwBigBody || resp.Header.Get("Content-Encoding") != "" { |
| + t.Fatalf("GET = %d, %d bytes, Content-Encoding %q; want the identity page", resp.StatusCode, len(b), resp.Header.Get("Content-Encoding")) |
| + } |
| + annie, betty := x.take(t, fwHost) |
| + wantNothing(t, "a large page", annie) |
| + wantOneDelivery(t, "a large page", betty, len(b), http.StatusOK) |
| +} |
| + |
| func TestForwardHAUpgradeSwitchesProtocols(t *testing.T) { |
| x := newFwScene(t, fwOpts{}) |
| raw, err := net.DialTimeout("tcp", x.a.tlsAddr, 10*time.Second) |