| 1 | package encodescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/ |
| 2 | |
| 3 | import ( |
| 4 | "contextscip-go gomod github.com/golang/go/src go1.25.1 context/" |
| 5 | "ioscip-go gomod github.com/golang/go/src go1.25.1 io/" |
| 6 | "net/httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/" |
| 7 | "net/http/httptestscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/" |
| 8 | "slicesscip-go gomod github.com/golang/go/src go1.25.1 slices/" |
| 9 | "syncscip-go gomod github.com/golang/go/src go1.25.1 sync/" |
| 10 | "testingscip-go gomod github.com/golang/go/src go1.25.1 testing/" |
| 11 | |
| 12 | "github.com/caddyserver/caddy/v2scip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/" |
| 13 | "github.com/caddyserver/caddy/v2/modules/caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/" |
| 14 | ) |
| 15 | |
| 16 | func BenchmarkOpenResponseWriterscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/BenchmarkOpenResponseWriter().(b *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Bscip-go gomod github.com/golang/go/src go1.25.1 testing/B#) { |
| 17 | enc := new(Encodescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/Encode#) |
| 18 | for b.Loopscip-go gomod github.com/golang/go/src go1.25.1 testing/B#Loop().() { |
| 19 | enc.openResponseWriterscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/Encode#openResponseWriter().("test", nil, false) |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | func TestPreferOrderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/TestPreferOrder().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) { |
| 24 | testCases := []struct { |
| 25 | name string |
| 26 | accept string |
| 27 | prefer []string |
| 28 | expected []string |
| 29 | }{ |
| 30 | { |
| 31 | name: "PreferOrder(): 4 accept, 3 prefer", |
| 32 | accept: "deflate, gzip, br, zstd", |
| 33 | prefer: []string{"zstd", "br", "gzip"}, |
| 34 | expected: []string{"zstd", "br", "gzip", "deflate"}, |
| 35 | }, |
| 36 | { |
| 37 | name: "PreferOrder(): 2 accept, 3 prefer", |
| 38 | accept: "deflate, zstd", |
| 39 | prefer: []string{"zstd", "br", "gzip"}, |
| 40 | expected: []string{"zstd", "deflate"}, |
| 41 | }, |
| 42 | { |
| 43 | name: "PreferOrder(): 2 accept (1 empty), 3 prefer", |
| 44 | accept: "gzip,,zstd", |
| 45 | prefer: []string{"zstd", "br", "gzip"}, |
| 46 | expected: []string{"zstd", "gzip", ""}, |
| 47 | }, |
| 48 | { |
| 49 | name: "PreferOrder(): 1 accept, 2 prefer", |
| 50 | accept: "gzip", |
| 51 | prefer: []string{"zstd", "gzip"}, |
| 52 | expected: []string{"gzip"}, |
| 53 | }, |
| 54 | { |
| 55 | name: "PreferOrder(): 4 accept (1 duplicate), 1 prefer", |
| 56 | accept: "deflate, gzip, br, br", |
| 57 | prefer: []string{"br"}, |
| 58 | expected: []string{"br", "br", "deflate", "gzip"}, |
| 59 | }, |
| 60 | { |
| 61 | name: "PreferOrder(): empty accept, 0 prefer", |
| 62 | accept: "", |
| 63 | prefer: []string{}, |
| 64 | expected: []string{}, |
| 65 | }, |
| 66 | { |
| 67 | name: "PreferOrder(): empty accept, 1 prefer", |
| 68 | accept: "", |
| 69 | prefer: []string{"gzip"}, |
| 70 | expected: []string{}, |
| 71 | }, |
| 72 | { |
| 73 | name: "PreferOrder(): with q-factor", |
| 74 | accept: "deflate;q=0.8, gzip;q=0.4, br;q=0.2, zstd", |
| 75 | prefer: []string{"gzip"}, |
| 76 | expected: []string{"zstd", "deflate", "gzip", "br"}, |
| 77 | }, |
| 78 | { |
| 79 | name: "PreferOrder(): with q-factor, no prefer", |
| 80 | accept: "deflate;q=0.8, gzip;q=0.4, br;q=0.2, zstd", |
| 81 | prefer: []string{}, |
| 82 | expected: []string{"zstd", "deflate", "gzip", "br"}, |
| 83 | }, |
| 84 | { |
| 85 | name: "PreferOrder(): q-factor=0 filtered out", |
| 86 | accept: "deflate;q=0.1, gzip;q=0.4, br;q=0.5, zstd;q=0", |
| 87 | prefer: []string{"gzip"}, |
| 88 | expected: []string{"br", "gzip", "deflate"}, |
| 89 | }, |
| 90 | { |
| 91 | name: "PreferOrder(): q-factor=0 filtered out, no prefer", |
| 92 | accept: "deflate;q=0.1, gzip;q=0.4, br;q=0.5, zstd;q=0", |
| 93 | prefer: []string{}, |
| 94 | expected: []string{"br", "gzip", "deflate"}, |
| 95 | }, |
| 96 | { |
| 97 | name: "PreferOrder(): with invalid q-factor", |
| 98 | accept: "br, deflate, gzip;q=2, zstd;q=0.1", |
| 99 | prefer: []string{"zstd", "gzip"}, |
| 100 | expected: []string{"gzip", "br", "deflate", "zstd"}, |
| 101 | }, |
| 102 | { |
| 103 | name: "PreferOrder(): with invalid q-factor, no prefer", |
| 104 | accept: "br, deflate, gzip;q=2, zstd;q=0.1", |
| 105 | prefer: []string{}, |
| 106 | expected: []string{"br", "deflate", "gzip", "zstd"}, |
| 107 | }, |
| 108 | } |
| 109 | |
| 110 | enc := new(Encodescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/Encode#) |
| 111 | r, _ := httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.NewRequestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/NewRequest().("", "", nil) |
| 112 | |
| 113 | for _, test := range testCases { |
| 114 | t.Runscip-go gomod github.com/golang/go/src go1.25.1 testing/T#Run().(test.name, func(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) { |
| 115 | if test.accept == "" { |
| 116 | r.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Header..Delscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#Del().("Accept-Encoding") |
| 117 | } else { |
| 118 | r.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Header..Setscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#Set().("Accept-Encoding", test.accept) |
| 119 | } |
| 120 | enc.Preferscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/Encode#Prefer. = test.prefer |
| 121 | result := AcceptedEncodingsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/AcceptedEncodings().(r, enc.Preferscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/Encode#Prefer.) |
| 122 | if !slicesscip-go gomod github.com/golang/go/src go1.25.1 slices/.Equalscip-go gomod github.com/golang/go/src go1.25.1 slices/Equal().(result, test.expected) { |
| 123 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("AcceptedEncodings() actual: %s expected: %s", |
| 124 | result, |
| 125 | test.expected) |
| 126 | } |
| 127 | }) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | func TestValidatescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/TestValidate().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) { |
| 132 | type testCase struct { |
| 133 | name string |
| 134 | prefer []string |
| 135 | wantErr bool |
| 136 | } |
| 137 | |
| 138 | var err error |
| 139 | var testCases []testCase |
| 140 | enc := new(Encodescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/Encode#) |
| 141 | |
| 142 | enc.writerPoolsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/Encode#writerPools. = map[string]*syncscip-go gomod github.com/golang/go/src go1.25.1 sync/.Poolscip-go gomod github.com/golang/go/src go1.25.1 sync/Pool#{ |
| 143 | "zstd": nil, |
| 144 | "gzip": nil, |
| 145 | "br": nil, |
| 146 | } |
| 147 | testCases = []testCase{ |
| 148 | { |
| 149 | name: "ValidatePrefer (zstd, gzip & br enabled): valid order with all encoder", |
| 150 | prefer: []string{"zstd", "br", "gzip"}, |
| 151 | wantErr: false, |
| 152 | }, |
| 153 | { |
| 154 | name: "ValidatePrefer (zstd, gzip & br enabled): valid order with 2 out of 3 encoders", |
| 155 | prefer: []string{"br", "gzip"}, |
| 156 | wantErr: false, |
| 157 | }, |
| 158 | { |
| 159 | name: "ValidatePrefer (zstd, gzip & br enabled): valid order with 1 out of 3 encoders", |
| 160 | prefer: []string{"gzip"}, |
| 161 | wantErr: false, |
| 162 | }, |
| 163 | { |
| 164 | name: "ValidatePrefer (zstd, gzip & br enabled): 1 duplicated (once) encoder", |
| 165 | prefer: []string{"gzip", "zstd", "gzip"}, |
| 166 | wantErr: true, |
| 167 | }, |
| 168 | { |
| 169 | name: "ValidatePrefer (zstd, gzip & br enabled): 1 not enabled encoder in prefer list", |
| 170 | prefer: []string{"br", "zstd", "gzip", "deflate"}, |
| 171 | wantErr: true, |
| 172 | }, |
| 173 | { |
| 174 | name: "ValidatePrefer (zstd, gzip & br enabled): no prefer list", |
| 175 | prefer: []string{}, |
| 176 | wantErr: false, |
| 177 | }, |
| 178 | } |
| 179 | |
| 180 | for _, test := range testCases { |
| 181 | t.Runscip-go gomod github.com/golang/go/src go1.25.1 testing/T#Run().(test.name, func(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) { |
| 182 | enc.Preferscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/Encode#Prefer. = test.prefer |
| 183 | err = enc.Validatescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/Encode#Validate().() |
| 184 | if (err != nil) != test.wantErr { |
| 185 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Validate() error = %v, wantErr = %v", err, test.wantErr) |
| 186 | } |
| 187 | }) |
| 188 | } |
| 189 | |
| 190 | enc.writerPoolsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/Encode#writerPools. = map[string]*syncscip-go gomod github.com/golang/go/src go1.25.1 sync/.Poolscip-go gomod github.com/golang/go/src go1.25.1 sync/Pool#{ |
| 191 | "zstd": nil, |
| 192 | "gzip": nil, |
| 193 | } |
| 194 | testCases = []testCase{ |
| 195 | { |
| 196 | name: "ValidatePrefer (zstd & gzip enabled): 1 not enabled encoder in prefer list", |
| 197 | prefer: []string{"zstd", "br", "gzip"}, |
| 198 | wantErr: true, |
| 199 | }, |
| 200 | { |
| 201 | name: "ValidatePrefer (zstd & gzip enabled): 2 not enabled encoder in prefer list", |
| 202 | prefer: []string{"br", "zstd", "gzip", "deflate"}, |
| 203 | wantErr: true, |
| 204 | }, |
| 205 | { |
| 206 | name: "ValidatePrefer (zstd & gzip enabled): only not enabled encoder in prefer list", |
| 207 | prefer: []string{"deflate", "br", "gzip"}, |
| 208 | wantErr: true, |
| 209 | }, |
| 210 | { |
| 211 | name: "ValidatePrefer (zstd & gzip enabled): 1 duplicated (once) encoder in prefer list", |
| 212 | prefer: []string{"gzip", "zstd", "gzip"}, |
| 213 | wantErr: true, |
| 214 | }, |
| 215 | { |
| 216 | name: "ValidatePrefer (zstd & gzip enabled): 1 duplicated (twice) encoder in prefer list", |
| 217 | prefer: []string{"gzip", "zstd", "gzip", "gzip"}, |
| 218 | wantErr: true, |
| 219 | }, |
| 220 | { |
| 221 | name: "ValidatePrefer (zstd & gzip enabled): 1 duplicated encoder in prefer list", |
| 222 | prefer: []string{"zstd", "zstd", "gzip", "gzip"}, |
| 223 | wantErr: true, |
| 224 | }, |
| 225 | { |
| 226 | name: "ValidatePrefer (zstd & gzip enabled): 1 duplicated not enabled encoder in prefer list", |
| 227 | prefer: []string{"br", "br", "gzip"}, |
| 228 | wantErr: true, |
| 229 | }, |
| 230 | { |
| 231 | name: "ValidatePrefer (zstd & gzip enabled): 2 duplicated not enabled encoder in prefer list", |
| 232 | prefer: []string{"br", "deflate", "br", "deflate"}, |
| 233 | wantErr: true, |
| 234 | }, |
| 235 | { |
| 236 | name: "ValidatePrefer (zstd & gzip enabled): valid order zstd first", |
| 237 | prefer: []string{"zstd", "gzip"}, |
| 238 | wantErr: false, |
| 239 | }, |
| 240 | { |
| 241 | name: "ValidatePrefer (zstd & gzip enabled): valid order gzip first", |
| 242 | prefer: []string{"gzip", "zstd"}, |
| 243 | wantErr: false, |
| 244 | }, |
| 245 | } |
| 246 | |
| 247 | for _, test := range testCases { |
| 248 | t.Runscip-go gomod github.com/golang/go/src go1.25.1 testing/T#Run().(test.name, func(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) { |
| 249 | enc.Preferscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/Encode#Prefer. = test.prefer |
| 250 | err = enc.Validatescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/Encode#Validate().() |
| 251 | if (err != nil) != test.wantErr { |
| 252 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Validate() error = %v, wantErr = %v", err, test.wantErr) |
| 253 | } |
| 254 | }) |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | func TestIsEncodeAllowedscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/TestIsEncodeAllowed().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) { |
| 259 | testCases := []struct { |
| 260 | name string |
| 261 | headers httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header# |
| 262 | expected bool |
| 263 | }{ |
| 264 | { |
| 265 | name: "Without any headers", |
| 266 | headers: httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#{}, |
| 267 | expected: true, |
| 268 | }, |
| 269 | { |
| 270 | name: "Without Cache-Control HTTP header", |
| 271 | headers: httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#{ |
| 272 | "Accept-Encoding": {"gzip"}, |
| 273 | }, |
| 274 | expected: true, |
| 275 | }, |
| 276 | { |
| 277 | name: "Cache-Control HTTP header ending with no-transform directive", |
| 278 | headers: httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#{ |
| 279 | "Accept-Encoding": {"gzip"}, |
| 280 | "Cache-Control": {"no-cache; no-transform"}, |
| 281 | }, |
| 282 | expected: false, |
| 283 | }, |
| 284 | { |
| 285 | name: "With Cache-Control HTTP header no-transform as Cache-Extension value", |
| 286 | headers: httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#{ |
| 287 | "Accept-Encoding": {"gzip"}, |
| 288 | "Cache-Control": {`no-store; no-cache; community="no-transform"`}, |
| 289 | }, |
| 290 | expected: false, |
| 291 | }, |
| 292 | } |
| 293 | |
| 294 | for _, test := range testCases { |
| 295 | t.Runscip-go gomod github.com/golang/go/src go1.25.1 testing/T#Run().(test.name, func(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) { |
| 296 | if result := isEncodeAllowedscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/isEncodeAllowed().(test.headers); result != test.expected { |
| 297 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("The headers given to the isEncodeAllowed should return %t, %t given.", |
| 298 | result, |
| 299 | test.expected) |
| 300 | } |
| 301 | }) |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | type mockEncoderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/mockEncoder# struct{} |
| 306 | |
| 307 | func (mockEncoderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/mockEncoder#) Writescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/mockEncoder#Write().(p []byte) (n int, err error) { return len(p), nil } |
| 308 | func (mockEncoderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/mockEncoder#) Closescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/mockEncoder#Close().() error { return nil } |
| 309 | func (mockEncoderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/mockEncoder#) Resetscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/mockEncoder#Reset().(w ioscip-go gomod github.com/golang/go/src go1.25.1 io/.Writerscip-go gomod github.com/golang/go/src go1.25.1 io/Writer#) {} |
| 310 | func (mockEncoderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/mockEncoder#) Flushscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/mockEncoder#Flush().() error { return nil } |
| 311 | |
| 312 | func TestServeHTTPDefaultEncodingPreferencescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/TestServeHTTPDefaultEncodingPreference().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) { |
| 313 | enc := new(Encodescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/Encode#) |
| 314 | enc.MinLengthscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/Encode#MinLength. = 1 // compress everything |
| 315 | enc.writerPoolsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/Encode#writerPools. = map[string]*syncscip-go gomod github.com/golang/go/src go1.25.1 sync/.Poolscip-go gomod github.com/golang/go/src go1.25.1 sync/Pool#{ |
| 316 | "gzip": { |
| 317 | Newscip-go gomod github.com/golang/go/src go1.25.1 sync/Pool#New.: func() any { return mockEncoderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/mockEncoder#{} }, |
| 318 | }, |
| 319 | "zstd": { |
| 320 | Newscip-go gomod github.com/golang/go/src go1.25.1 sync/Pool#New.: func() any { return mockEncoderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/mockEncoder#{} }, |
| 321 | }, |
| 322 | } |
| 323 | |
| 324 | // Call Provision() with a valid caddy.Context to exercise the real path |
| 325 | ctx, cancel := caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.NewContextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/NewContext().(caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Contextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#{Contextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#Context.: contextscip-go gomod github.com/golang/go/src go1.25.1 context/.Backgroundscip-go gomod github.com/golang/go/src go1.25.1 context/Background().()}) |
| 326 | defer cancel() |
| 327 | if err := enc.Provisionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/Encode#Provision().(ctx); err != nil { |
| 328 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("Provision failed: %v", err) |
| 329 | } |
| 330 | |
| 331 | // Test default preference: zstd preferred over gzip |
| 332 | r, err := httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.NewRequestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/NewRequest().("GET", "/", nil) |
| 333 | if err != nil { |
| 334 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("error creating request: %v", err) |
| 335 | } |
| 336 | r.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Header..Setscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#Set().("Accept-Encoding", "gzip, deflate, br, zstd") |
| 337 | |
| 338 | w := httptestscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/.NewRecorderscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/NewRecorder().() |
| 339 | w.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/ResponseRecorder#Header().().Setscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#Set().("Content-Type", "text/plain") |
| 340 | |
| 341 | next := caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.HandlerFuncscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerFunc#(func(w httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#, r *httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#) error { |
| 342 | w.WriteHeaderscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#WriteHeader().(httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.StatusOKscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/StatusOK.) |
| 343 | _, err := w.Writescip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#Write().([]byte("Hello, world! This is a long enough string to satisfy min length if it wasn't 1.")) |
| 344 | return err |
| 345 | }) |
| 346 | |
| 347 | err = enc.ServeHTTPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/Encode#ServeHTTP().(w, r, next) |
| 348 | if err != nil { |
| 349 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("ServeHTTP returned error: %v", err) |
| 350 | } |
| 351 | |
| 352 | // ETag suffix or Content-Encoding header should reflect zstd |
| 353 | contentEncoding := w.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/ResponseRecorder#Header().().Getscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#Get().("Content-Encoding") |
| 354 | if contentEncoding != "zstd" { |
| 355 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Expected Content-Encoding to be 'zstd' by default, got '%s'", contentEncoding) |
| 356 | } |
| 357 | |
| 358 | // Test explicit user preference: gzip over zstd |
| 359 | enc.Preferscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/Encode#Prefer. = []string{"gzip", "zstd"} |
| 360 | |
| 361 | w2 := httptestscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/.NewRecorderscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/NewRecorder().() |
| 362 | w2.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/ResponseRecorder#Header().().Setscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#Set().("Content-Type", "text/plain") |
| 363 | err = enc.ServeHTTPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/encode`/Encode#ServeHTTP().(w2, r, next) |
| 364 | if err != nil { |
| 365 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("ServeHTTP returned error: %v", err) |
| 366 | } |
| 367 | |
| 368 | contentEncoding2 := w2.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/ResponseRecorder#Header().().Getscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#Get().("Content-Encoding") |
| 369 | if contentEncoding2 != "gzip" { |
| 370 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Expected Content-Encoding to be 'gzip' when explicitly preferred, got '%s'", contentEncoding2) |
| 371 | } |
| 372 | } |