| 1 |
// Copyright 2013 The Gorilla Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package handlers |
| 6 |
|
| 7 |
import ( |
| 8 |
"bufio" |
| 9 |
"bytes" |
| 10 |
"compress/gzip" |
| 11 |
"io" |
| 12 |
"log" |
| 13 |
"net" |
| 14 |
"net/http" |
| 15 |
"net/http/httptest" |
| 16 |
"net/url" |
| 17 |
"os" |
| 18 |
"path/filepath" |
| 19 |
"strconv" |
| 20 |
"testing" |
| 21 |
) |
| 22 |
|
| 23 |
var contentType = "text/plain; charset=utf-8" |
| 24 |
|
| 25 |
func compressedRequest(w *httptest.ResponseRecorder, compression string) { |
| 26 |
CompressHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 27 |
w.Header().Set("Content-Length", strconv.Itoa(9*1024)) |
| 28 |
w.Header().Set("Content-Type", contentType) |
| 29 |
for i := 0; i < 1024; i++ { |
| 30 |
_, err := io.WriteString(w, "Gorilla!\n") |
| 31 |
if err != nil { |
| 32 |
log.Printf("error on writting to http.ResponseWriter: %v\n", err) |
| 33 |
} |
| 34 |
} |
| 35 |
})).ServeHTTP(w, &http.Request{ |
| 36 |
Method: http.MethodGet, |
| 37 |
Header: http.Header{ |
| 38 |
acceptEncoding: []string{compression}, |
| 39 |
}, |
| 40 |
}) |
| 41 |
} |
| 42 |
|
| 43 |
func TestCompressHandlerNoCompression(t *testing.T) { |
| 44 |
w := httptest.NewRecorder() |
| 45 |
compressedRequest(w, "") |
| 46 |
resp := w.Result() |
| 47 |
if enc := resp.Header.Get("Content-Encoding"); enc != "" { |
| 48 |
t.Errorf("wrong content encoding, got %q want %q", enc, "") |
| 49 |
} |
| 50 |
if ct := resp.Header.Get("Content-Type"); ct != contentType { |
| 51 |
t.Errorf("wrong content type, got %q want %q", ct, contentType) |
| 52 |
} |
| 53 |
if w.Body.Len() != 1024*9 { |
| 54 |
t.Errorf("wrong len, got %d want %d", w.Body.Len(), 1024*9) |
| 55 |
} |
| 56 |
if l := resp.Header.Get("Content-Length"); l != "9216" { |
| 57 |
t.Errorf("wrong content-length. got %q expected %d", l, 1024*9) |
| 58 |
} |
| 59 |
if v := resp.Header.Get("Vary"); v != acceptEncoding { |
| 60 |
t.Errorf("wrong vary. got %s expected %s", v, acceptEncoding) |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
func TestAcceptEncodingIsDropped(t *testing.T) { |
| 65 |
tCases := []struct { |
| 66 |
name, |
| 67 |
compression, |
| 68 |
expect string |
| 69 |
isPresent bool |
| 70 |
}{ |
| 71 |
{ |
| 72 |
"accept-encoding-gzip", |
| 73 |
"gzip", |
| 74 |
"", |
| 75 |
false, |
| 76 |
}, |
| 77 |
{ |
| 78 |
"accept-encoding-deflate", |
| 79 |
"deflate", |
| 80 |
"", |
| 81 |
false, |
| 82 |
}, |
| 83 |
{ |
| 84 |
"accept-encoding-gzip,deflate", |
| 85 |
"gzip,deflate", |
| 86 |
"", |
| 87 |
false, |
| 88 |
}, |
| 89 |
{ |
| 90 |
"accept-encoding-gzip,deflate,something", |
| 91 |
"gzip,deflate,something", |
| 92 |
"", |
| 93 |
false, |
| 94 |
}, |
| 95 |
{ |
| 96 |
"accept-encoding-unknown", |
| 97 |
"unknown", |
| 98 |
"unknown", |
| 99 |
true, |
| 100 |
}, |
| 101 |
} |
| 102 |
|
| 103 |
for _, tCase := range tCases { |
| 104 |
ch := CompressHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 105 |
acceptEnc := r.Header.Get(acceptEncoding) |
| 106 |
if acceptEnc == "" && tCase.isPresent { |
| 107 |
t.Fatalf("%s: expected 'Accept-Encoding' header to be present but was not", tCase.name) |
| 108 |
} |
| 109 |
if acceptEnc != "" { |
| 110 |
if !tCase.isPresent { |
| 111 |
t.Fatalf("%s: expected 'Accept-Encoding' header to be dropped but was still present having value %q", tCase.name, acceptEnc) |
| 112 |
} |
| 113 |
if acceptEnc != tCase.expect { |
| 114 |
t.Fatalf("%s: expected 'Accept-Encoding' to be %q but was %q", tCase.name, tCase.expect, acceptEnc) |
| 115 |
} |
| 116 |
} |
| 117 |
})) |
| 118 |
|
| 119 |
w := httptest.NewRecorder() |
| 120 |
ch.ServeHTTP(w, &http.Request{ |
| 121 |
Method: http.MethodGet, |
| 122 |
Header: http.Header{ |
| 123 |
acceptEncoding: []string{tCase.compression}, |
| 124 |
}, |
| 125 |
}) |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
func TestCompressHandlerGzip(t *testing.T) { |
| 130 |
w := httptest.NewRecorder() |
| 131 |
compressedRequest(w, "gzip") |
| 132 |
resp := w.Result() |
| 133 |
if resp.Header.Get("Content-Encoding") != "gzip" { |
| 134 |
t.Errorf("wrong content encoding, got %q want %q", resp.Header.Get("Content-Encoding"), "gzip") |
| 135 |
} |
| 136 |
if resp.Header.Get("Content-Type") != "text/plain; charset=utf-8" { |
| 137 |
t.Errorf("wrong content type, got %s want %s", resp.Header.Get("Content-Type"), "text/plain; charset=utf-8") |
| 138 |
} |
| •139 |
if w.Body.Len() != 68 { |
| 140 |
t.Errorf("wrong len, got %d want %d", w.Body.Len(), 68) |
| 141 |
} |
| 142 |
if l := resp.Header.Get("Content-Length"); l != "" { |
| 143 |
t.Errorf("wrong content-length. got %q expected %q", l, "") |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
func TestCompressHandlerDeflate(t *testing.T) { |
| 148 |
w := httptest.NewRecorder() |
| 149 |
compressedRequest(w, "deflate") |
| 150 |
resp := w.Result() |
| 151 |
if resp.Header.Get("Content-Encoding") != "deflate" { |
| 152 |
t.Fatalf("wrong content encoding, got %q want %q", resp.Header.Get("Content-Encoding"), "deflate") |
| 153 |
} |
| 154 |
if resp.Header.Get("Content-Type") != "text/plain; charset=utf-8" { |
| 155 |
t.Fatalf("wrong content type, got %s want %s", resp.Header.Get("Content-Type"), "text/plain; charset=utf-8") |
| 156 |
} |
| •157 |
if w.Body.Len() != 50 { |
| 158 |
t.Fatalf("wrong len, got %d want %d", w.Body.Len(), 50) |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
func TestCompressHandlerGzipDeflate(t *testing.T) { |
| 163 |
w := httptest.NewRecorder() |
| 164 |
compressedRequest(w, "gzip, deflate ") |
| 165 |
resp := w.Result() |
| 166 |
if resp.Header.Get("Content-Encoding") != "gzip" { |
| 167 |
t.Fatalf("wrong content encoding, got %q want %q", resp.Header.Get("Content-Encoding"), "gzip") |
| 168 |
} |
| 169 |
if resp.Header.Get("Content-Type") != "text/plain; charset=utf-8" { |
| 170 |
t.Fatalf("wrong content type, got %s want %s", resp.Header.Get("Content-Type"), "text/plain; charset=utf-8") |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
// Make sure we can compress and serve an *os.File properly. We need |
| 175 |
// to use a real http server to trigger the net/http sendfile special |
| 176 |
// case. |
| 177 |
func TestCompressFile(t *testing.T) { |
| 178 |
dir, err := os.MkdirTemp("", "gorilla_compress") |
| 179 |
if err != nil { |
| 180 |
t.Fatal(err) |
| 181 |
} |
| 182 |
defer os.RemoveAll(dir) |
| 183 |
|
| 184 |
err = os.WriteFile(filepath.Join(dir, "hello.txt"), []byte("hello"), 0o644) |
| 185 |
if err != nil { |
| 186 |
t.Fatal(err) |
| 187 |
} |
| 188 |
|
| 189 |
s := httptest.NewServer(CompressHandler(http.FileServer(http.Dir(dir)))) |
| 190 |
defer s.Close() |
| 191 |
|
| 192 |
url := &url.URL{Scheme: "http", Host: s.Listener.Addr().String(), Path: "/hello.txt"} |
| 193 |
req, err := http.NewRequest(http.MethodGet, url.String(), nil) |
| 194 |
if err != nil { |
| 195 |
t.Fatal(err) |
| 196 |
} |
| 197 |
req.Header.Set(acceptEncoding, "gzip") |
| 198 |
res, err := http.DefaultClient.Do(req) |
| 199 |
if err != nil { |
| 200 |
t.Fatal(err) |
| 201 |
} |
| 202 |
|
| 203 |
if res.StatusCode != http.StatusOK { |
| 204 |
t.Fatalf("expected OK, got %q", res.Status) |
| 205 |
} |
| 206 |
|
| 207 |
var got bytes.Buffer |
| 208 |
gr, err := gzip.NewReader(res.Body) |
| 209 |
if err != nil { |
| 210 |
t.Fatal(err) |
| 211 |
} |
| 212 |
_, err = io.Copy(&got, gr) |
| 213 |
if err != nil { |
| 214 |
t.Fatal(err) |
| 215 |
} |
| 216 |
|
| 217 |
if got.String() != "hello" { |
| 218 |
t.Errorf("expected hello, got %q", got.String()) |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
type fullyFeaturedResponseWriter struct{} |
| 223 |
|
| 224 |
// Header/Write/WriteHeader implement the http.ResponseWriter interface. |
| 225 |
func (fullyFeaturedResponseWriter) Header() http.Header { |
| 226 |
return http.Header{} |
| 227 |
} |
| 228 |
|
| 229 |
func (fullyFeaturedResponseWriter) Write([]byte) (int, error) { |
| 230 |
return 0, nil |
| 231 |
} |
| 232 |
func (fullyFeaturedResponseWriter) WriteHeader(int) {} |
| 233 |
|
| 234 |
// Flush implements the http.Flusher interface. |
| 235 |
func (fullyFeaturedResponseWriter) Flush() {} |
| 236 |
|
| 237 |
// Hijack implements the http.Hijacker interface. |
| 238 |
func (fullyFeaturedResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) { |
| 239 |
return nil, nil, nil |
| 240 |
} |
| 241 |
|
| 242 |
func TestCompressHandlerPreserveInterfaces(t *testing.T) { |
| 243 |
// Compile time validation fullyFeaturedResponseWriter implements all the |
| 244 |
// interfaces we're asserting in the test case below. |
| 245 |
var ( |
| 246 |
_ http.Flusher = fullyFeaturedResponseWriter{} |
| 247 |
_ http.Hijacker = fullyFeaturedResponseWriter{} |
| 248 |
) |
| 249 |
var h http.Handler = http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { |
| 250 |
comp := r.Header.Get(acceptEncoding) |
| 251 |
if _, ok := rw.(http.Flusher); !ok { |
| 252 |
t.Errorf("ResponseWriter lost http.Flusher interface for %q", comp) |
| 253 |
} |
| 254 |
if _, ok := rw.(http.Hijacker); !ok { |
| 255 |
t.Errorf("ResponseWriter lost http.Hijacker interface for %q", comp) |
| 256 |
} |
| 257 |
}) |
| 258 |
h = CompressHandler(h) |
| 259 |
var rw fullyFeaturedResponseWriter |
| 260 |
r, err := http.NewRequest(http.MethodGet, "/", nil) |
| 261 |
if err != nil { |
| 262 |
t.Fatalf("Failed to create test request: %v", err) |
| 263 |
} |
| 264 |
r.Header.Set(acceptEncoding, "gzip") |
| 265 |
h.ServeHTTP(rw, r) |
| 266 |
|
| 267 |
r.Header.Set(acceptEncoding, "deflate") |
| 268 |
h.ServeHTTP(rw, r) |
| 269 |
} |
| 270 |
|
| 271 |
type paltryResponseWriter struct{} |
| 272 |
|
| 273 |
func (paltryResponseWriter) Header() http.Header { |
| 274 |
return http.Header{} |
| 275 |
} |
| 276 |
|
| 277 |
func (paltryResponseWriter) Write([]byte) (int, error) { |
| 278 |
return 0, nil |
| 279 |
} |
| 280 |
func (paltryResponseWriter) WriteHeader(int) {} |
| 281 |
|
| 282 |
func TestCompressHandlerDoesntInventInterfaces(t *testing.T) { |
| 283 |
var h http.Handler = http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { |
| 284 |
if _, ok := rw.(http.Hijacker); ok { |
| 285 |
t.Error("ResponseWriter shouldn't implement http.Hijacker") |
| 286 |
} |
| 287 |
}) |
| 288 |
|
| 289 |
h = CompressHandler(h) |
| 290 |
|
| 291 |
var rw paltryResponseWriter |
| 292 |
r, err := http.NewRequest(http.MethodGet, "/", nil) |
| 293 |
if err != nil { |
| 294 |
t.Fatalf("Failed to create test request: %v", err) |
| 295 |
} |
| 296 |
r.Header.Set(acceptEncoding, "gzip") |
| 297 |
h.ServeHTTP(rw, r) |
| 298 |
} |