| index 1213f231c..f2bd15d41 100644 |
| --- a/internal/edge/compress_test.go |
| +++ b/internal/edge/compress_test.go |
| @@ -805,3 +805,73 @@ func TestHeadKeepsItsContentLength(t *testing.T) { |
| t.Errorf("HEAD Content-Length = %q, want 4096", got) |
| } |
| } |
| + |
| +// A drain's Connection: close, set on the real writer by conns.track, |
| +// survives the gzip writer's header sync on a proxied tenant response the |
| +// client accepts gzip for (W6 final review F9; Go clients and browsers ask |
| +// for gzip by default). Compression itself is unchanged: the body still |
| +// arrives gzipped. Both drains share the switch: the ingress drain's step 1 |
| +// and §25's draining flag (DrainConnections sets it with its barrier). |
| +func TestDrainConnectionCloseSurvivesGzip(t *testing.T) { |
| + for _, drain := range []string{"ingress", "connection-drain"} { |
| + t.Run(drain, func(t *testing.T) { |
| + dir := t.TempDir() |
| + sock := filepath.Join(dir, "app.sock") |
| + ln, err := net.Listen("unix", sock) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + body := strings.Repeat("lifecycle tenant ", 200) |
| + tenant := &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + w.Header().Set("Content-Type", "text/plain") |
| + io.WriteString(w, body) |
| + })} |
| + go tenant.Serve(ln) |
| + t.Cleanup(func() { tenant.Close() }) |
| + reg, err := registry.Open(filepath.Join(dir, "registry.db")) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(func() { reg.Close() }) |
| + if err := reg.Upsert(registry.Route{Host: "app.example.com", Socket: sock}); err != nil { |
| + t.Fatal(err) |
| + } |
| + e, err := New(reg, nil) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + srv := httptest.NewServer(e) |
| + defer srv.Close() |
| + client := &http.Client{Transport: &http.Transport{}} // asks for gzip by itself |
| + defer client.CloseIdleConnections() |
| + get := func() *http.Response { |
| + t.Helper() |
| + req, _ := http.NewRequest("GET", srv.URL+"/", nil) |
| + req.Host = "app.example.com" |
| + resp, err := client.Do(req) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + b, err := io.ReadAll(resp.Body) |
| + resp.Body.Close() |
| + if err != nil || string(b) != body || !resp.Uncompressed { |
| + t.Fatalf("tenant: %d bytes, err %v, gzipped %v", len(b), err, resp.Uncompressed) |
| + } |
| + return resp |
| + } |
| + if get().Close { |
| + t.Fatal("Connection: close before the drain") |
| + } |
| + if drain == "ingress" { |
| + if err := e.BeginIngressDrain(); err != nil { |
| + t.Fatal(err) |
| + } |
| + } else { |
| + e.conns.draining.Store(true) |
| + } |
| + if !get().Close { |
| + t.Fatal("a gzipped response during the drain lost Connection: close") |
| + } |
| + }) |
| + } |
| +} |