-
Notifications
You must be signed in to change notification settings - Fork 1
/
httpgzip_test.go
84 lines (67 loc) · 2.45 KB
/
httpgzip_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package httpgzip
import (
"compress/gzip"
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
const lorem = `Lorem ipsum dolor sit amet, consectetur adipisicing
elit, sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.`
func handlerFunc(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Write([]byte(lorem))
}
func TestHTTPGzip(t *testing.T) {
handler := http.HandlerFunc(handlerFunc)
wrapped := Handler(handler)
// A call against the regular handler
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, &http.Request{})
// A call against the wrapped handler without gzip
rec2 := httptest.NewRecorder()
wrapped.ServeHTTP(rec2, &http.Request{})
if rec.Code != rec2.Code {
t.Fatalf("Expected status code (%d) != actual (%d)", rec.Code, rec2.Code)
}
if !reflect.DeepEqual(rec.HeaderMap, rec2.HeaderMap) {
t.Fatalf("Expected header map (%#v) != actual (%#v)", rec.HeaderMap, rec2.HeaderMap)
}
if rec.Body.String() != rec2.Body.String() {
t.Fatal("Expected body != actual")
}
// A call against the wrapped handler with gzip on
rec3 := httptest.NewRecorder()
req := &http.Request{
Header: map[string][]string{},
}
req.Header.Set("Accept-Encoding", "gzip")
wrapped.ServeHTTP(rec3, req)
if rec.Code != rec3.Code {
t.Fatalf("Expected status code (%d) != actual (%d)", rec.Code, rec3.Code)
}
if rec.HeaderMap.Get("Content-Type") != rec3.HeaderMap.Get("Content-Type") {
t.Fatal("Expected Content-Type header (%s) != actual (%s)", rec.HeaderMap.Get("Content-Type"), rec3.HeaderMap.Get("Content-Type"))
}
if rec3.HeaderMap.Get("Vary") != "Accept-Encoding" {
t.Fatal("Missing Vary: Accept-Encoding header")
}
if rec3.HeaderMap.Get("Content-Encoding") != "gzip" {
t.Fatal("Missing Content-Encoding: gzip header")
}
if rec.Body.Len() < rec3.Body.Len() {
t.Fatalf("Payload actual size (%d) wasn't as small as expected (%d)", rec3.Body.Len(), rec.Body.Len())
}
gr, _ := gzip.NewReader(rec3.Body)
defer gr.Close()
data, _ := ioutil.ReadAll(gr)
if string(data) != rec.Body.String() {
t.Fatal("Expected body != actual when uncompressed")
}
}