|
| 1 | +// Copyright (c) 2025-2026 Netresearch DTT GmbH |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package web_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "compress/gzip" |
| 8 | + "encoding/json" |
| 9 | + "io" |
| 10 | + "net/http" |
| 11 | + "net/http/httptest" |
| 12 | + "strings" |
| 13 | + "testing" |
| 14 | + |
| 15 | + "github.com/klauspost/compress/zstd" |
| 16 | + "github.com/netresearch/ofelia/core" |
| 17 | + webpkg "github.com/netresearch/ofelia/web" |
| 18 | +) |
| 19 | + |
| 20 | +// TestResponseCompression pins the response compression: clients |
| 21 | +// advertising a supported codec get compressed pages and API payloads |
| 22 | +// (with Content-Length dropped and Vary set), clients advertising none |
| 23 | +// get identity responses untouched. This function covers the gzip |
| 24 | +// branch — Safari and other clients without zstd; TestZstdCompression |
| 25 | +// covers what the mainstream browsers actually receive. |
| 26 | +func TestResponseCompression(t *testing.T) { |
| 27 | + t.Parallel() |
| 28 | + |
| 29 | + sched := &core.Scheduler{Jobs: []core.Job{}, Logger: stubDiscardLogger()} |
| 30 | + srv := webpkg.NewServer("", sched, nil, nil) |
| 31 | + handler := srv.HTTPServer().Handler |
| 32 | + |
| 33 | + get := func(url string, acceptGzip bool) *httptest.ResponseRecorder { |
| 34 | + t.Helper() |
| 35 | + req := httptest.NewRequest(http.MethodGet, url, nil) |
| 36 | + if acceptGzip { |
| 37 | + req.Header.Set("Accept-Encoding", "gzip, deflate, br") |
| 38 | + } |
| 39 | + w := httptest.NewRecorder() |
| 40 | + handler.ServeHTTP(w, req) |
| 41 | + if w.Code != http.StatusOK { |
| 42 | + t.Fatalf("%s: unexpected status %d", url, w.Code) |
| 43 | + } |
| 44 | + return w |
| 45 | + } |
| 46 | + |
| 47 | + // The rendered page, compressed. |
| 48 | + w := get("/", true) |
| 49 | + if enc := w.Header().Get("Content-Encoding"); enc != "gzip" { |
| 50 | + t.Fatalf("expected gzip encoding, got %q", enc) |
| 51 | + } |
| 52 | + if vary := w.Header().Get("Vary"); !strings.Contains(vary, "Accept-Encoding") { |
| 53 | + t.Fatalf("Vary must include Accept-Encoding, got %q", vary) |
| 54 | + } |
| 55 | + if cl := w.Header().Get("Content-Length"); cl != "" { |
| 56 | + t.Fatalf("stale Content-Length %q on a compressed response", cl) |
| 57 | + } |
| 58 | + zr, err := gzip.NewReader(w.Body) |
| 59 | + if err != nil { |
| 60 | + t.Fatalf("gzip reader: %v", err) |
| 61 | + } |
| 62 | + body, err := io.ReadAll(zr) |
| 63 | + if err != nil { |
| 64 | + t.Fatalf("gunzip: %v", err) |
| 65 | + } |
| 66 | + if !strings.Contains(string(body), "<title>Ofelia</title>") { |
| 67 | + t.Fatalf("gunzipped page does not look like the UI") |
| 68 | + } |
| 69 | + |
| 70 | + // An API response, compressed and still valid JSON. |
| 71 | + w = get("/api/dashboard", true) |
| 72 | + if enc := w.Header().Get("Content-Encoding"); enc != "gzip" { |
| 73 | + t.Fatalf("expected gzip on /api/dashboard, got %q", enc) |
| 74 | + } |
| 75 | + zr, err = gzip.NewReader(w.Body) |
| 76 | + if err != nil { |
| 77 | + t.Fatalf("gzip reader: %v", err) |
| 78 | + } |
| 79 | + var dash map[string]json.RawMessage |
| 80 | + if err := json.NewDecoder(zr).Decode(&dash); err != nil { |
| 81 | + t.Fatalf("decode gunzipped dashboard: %v", err) |
| 82 | + } |
| 83 | + if _, ok := dash["jobs"]; !ok { |
| 84 | + t.Fatalf("gunzipped dashboard missing jobs section") |
| 85 | + } |
| 86 | + |
| 87 | + // No Accept-Encoding: identity, readable directly. |
| 88 | + w = get("/", false) |
| 89 | + if enc := w.Header().Get("Content-Encoding"); enc != "" { |
| 90 | + t.Fatalf("client without gzip support got Content-Encoding %q", enc) |
| 91 | + } |
| 92 | + if !strings.Contains(w.Body.String(), "<title>Ofelia</title>") { |
| 93 | + t.Fatalf("identity response does not look like the UI") |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// TestZstdCompression pins what real browsers receive. Chrome, Edge and |
| 98 | +// Firefox send "gzip, deflate, br, zstd"; the wrapper enables zstd and |
| 99 | +// prefers it at equal q-values, so those clients get zstd, not gzip. |
| 100 | +// Dropping zstd from the header must fall back to gzip — that is the |
| 101 | +// Safari path. |
| 102 | +func TestZstdCompression(t *testing.T) { |
| 103 | + t.Parallel() |
| 104 | + |
| 105 | + sched := &core.Scheduler{Jobs: []core.Job{}, Logger: stubDiscardLogger()} |
| 106 | + srv := webpkg.NewServer("", sched, nil, nil) |
| 107 | + handler := srv.HTTPServer().Handler |
| 108 | + |
| 109 | + get := func(url, acceptEncoding string) *httptest.ResponseRecorder { |
| 110 | + t.Helper() |
| 111 | + req := httptest.NewRequest(http.MethodGet, url, nil) |
| 112 | + req.Header.Set("Accept-Encoding", acceptEncoding) |
| 113 | + w := httptest.NewRecorder() |
| 114 | + handler.ServeHTTP(w, req) |
| 115 | + if w.Code != http.StatusOK { |
| 116 | + t.Fatalf("%s: unexpected status %d", url, w.Code) |
| 117 | + } |
| 118 | + return w |
| 119 | + } |
| 120 | + |
| 121 | + // The header every mainstream browser sends. |
| 122 | + w := get("/", "gzip, deflate, br, zstd") |
| 123 | + if enc := w.Header().Get("Content-Encoding"); enc != "zstd" { |
| 124 | + t.Fatalf("expected zstd for a browser Accept-Encoding, got %q", enc) |
| 125 | + } |
| 126 | + if vary := w.Header().Get("Vary"); !strings.Contains(vary, "Accept-Encoding") { |
| 127 | + t.Fatalf("Vary must include Accept-Encoding, got %q", vary) |
| 128 | + } |
| 129 | + if cl := w.Header().Get("Content-Length"); cl != "" { |
| 130 | + t.Fatalf("stale Content-Length %q on a compressed response", cl) |
| 131 | + } |
| 132 | + zr, err := zstd.NewReader(w.Body) |
| 133 | + if err != nil { |
| 134 | + t.Fatalf("zstd reader: %v", err) |
| 135 | + } |
| 136 | + defer zr.Close() |
| 137 | + body, err := io.ReadAll(zr) |
| 138 | + if err != nil { |
| 139 | + t.Fatalf("zstd decode: %v", err) |
| 140 | + } |
| 141 | + if !strings.Contains(string(body), "<title>Ofelia</title>") { |
| 142 | + t.Fatalf("decoded page does not look like the UI") |
| 143 | + } |
| 144 | + |
| 145 | + // An API payload over zstd is still valid JSON. |
| 146 | + w = get("/api/dashboard", "gzip, deflate, br, zstd") |
| 147 | + if enc := w.Header().Get("Content-Encoding"); enc != "zstd" { |
| 148 | + t.Fatalf("expected zstd on /api/dashboard, got %q", enc) |
| 149 | + } |
| 150 | + dr, err := zstd.NewReader(w.Body) |
| 151 | + if err != nil { |
| 152 | + t.Fatalf("zstd reader: %v", err) |
| 153 | + } |
| 154 | + defer dr.Close() |
| 155 | + var dash map[string]json.RawMessage |
| 156 | + if err := json.NewDecoder(dr).Decode(&dash); err != nil { |
| 157 | + t.Fatalf("decode zstd dashboard: %v", err) |
| 158 | + } |
| 159 | + if _, ok := dash["jobs"]; !ok { |
| 160 | + t.Fatalf("decoded dashboard missing jobs section") |
| 161 | + } |
| 162 | + |
| 163 | + // Without zstd in the header the client gets gzip. |
| 164 | + w = get("/", "gzip, deflate, br") |
| 165 | + if enc := w.Header().Get("Content-Encoding"); enc != "gzip" { |
| 166 | + t.Fatalf("client without zstd got %q, want gzip", enc) |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +// TestCompressNegotiationEdgeCases pins the paths where compression must |
| 171 | +// NOT happen and the sniffing of a Content-Type from uncompressed bytes. |
| 172 | +func TestCompressNegotiationEdgeCases(t *testing.T) { |
| 173 | + t.Parallel() |
| 174 | + |
| 175 | + sched := &core.Scheduler{Jobs: []core.Job{}, Logger: stubDiscardLogger()} |
| 176 | + srv := webpkg.NewServer("", sched, nil, nil) |
| 177 | + // The probe routes (/live) exist only after health registration. |
| 178 | + srv.RegisterHealthEndpoints(webpkg.NewHealthChecker(nil, nil, "test")) |
| 179 | + handler := srv.HTTPServer().Handler |
| 180 | + |
| 181 | + do := func(mutate func(*http.Request)) *httptest.ResponseRecorder { |
| 182 | + t.Helper() |
| 183 | + req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 184 | + mutate(req) |
| 185 | + w := httptest.NewRecorder() |
| 186 | + handler.ServeHTTP(w, req) |
| 187 | + return w |
| 188 | + } |
| 189 | + |
| 190 | + // "gzip;q=0" is an explicit refusal, not consent. |
| 191 | + w := do(func(r *http.Request) { r.Header.Set("Accept-Encoding", "gzip;q=0") }) |
| 192 | + if enc := w.Header().Get("Content-Encoding"); enc != "" { |
| 193 | + t.Fatalf("client refusing gzip via q=0 got Content-Encoding %q", enc) |
| 194 | + } |
| 195 | + if !strings.Contains(w.Body.String(), "<title>Ofelia</title>") { |
| 196 | + t.Fatalf("q=0 response is not readable identity") |
| 197 | + } |
| 198 | + |
| 199 | + // A bare wildcard permits gzip but does not name it; gzhttp answers |
| 200 | + // identity, which is always RFC-compliant (compression is optional — |
| 201 | + // the hard requirement is only the refusal direction below). This |
| 202 | + // pin documents the library's behavior, not a contract of ours. |
| 203 | + w = do(func(r *http.Request) { r.Header.Set("Accept-Encoding", "*") }) |
| 204 | + if enc := w.Header().Get("Content-Encoding"); enc != "" { |
| 205 | + t.Fatalf("bare wildcard got Content-Encoding %q, want identity", enc) |
| 206 | + } |
| 207 | + |
| 208 | + // An explicitly named refusal must never receive gzip, wildcard or |
| 209 | + // not (RFC 9110 §12.5.3). |
| 210 | + w = do(func(r *http.Request) { r.Header.Set("Accept-Encoding", "*, gzip;q=0") }) |
| 211 | + if enc := w.Header().Get("Content-Encoding"); enc != "" { |
| 212 | + t.Fatalf("wildcard followed by gzip;q=0 got Content-Encoding %q", enc) |
| 213 | + } |
| 214 | + |
| 215 | + // Range requests pass through identity: http.FileServer slices |
| 216 | + // identity bytes and gzipping the slice would corrupt the download. |
| 217 | + w = do(func(r *http.Request) { |
| 218 | + r.URL.Path = "/app.js" |
| 219 | + r.Header.Set("Accept-Encoding", "gzip") |
| 220 | + r.Header.Set("Range", "bytes=0-9") |
| 221 | + }) |
| 222 | + if w.Code != http.StatusPartialContent { |
| 223 | + t.Fatalf("expected 206 for ranged asset, got %d", w.Code) |
| 224 | + } |
| 225 | + if enc := w.Header().Get("Content-Encoding"); enc != "" { |
| 226 | + t.Fatalf("ranged response must stay identity, got Content-Encoding %q", enc) |
| 227 | + } |
| 228 | + if w.Body.Len() != 10 { |
| 229 | + t.Fatalf("expected 10 identity bytes, got %d", w.Body.Len()) |
| 230 | + } |
| 231 | + |
| 232 | + // A handler that sets no Content-Type must be sniffed on the |
| 233 | + // uncompressed bytes — not answer application/x-gzip. |
| 234 | + w = do(func(r *http.Request) { |
| 235 | + r.URL.Path = "/live" |
| 236 | + r.Header.Set("Accept-Encoding", "gzip") |
| 237 | + }) |
| 238 | + if w.Code != http.StatusOK { |
| 239 | + t.Fatalf("/live status %d", w.Code) |
| 240 | + } |
| 241 | + if ct := w.Header().Get("Content-Type"); !strings.HasPrefix(ct, "text/plain") { |
| 242 | + t.Fatalf("/live under gzip answered Content-Type %q", ct) |
| 243 | + } |
| 244 | +} |
0 commit comments