-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhttpgzip_test.go
163 lines (154 loc) · 4.43 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package httpgzip_test
import (
"bytes"
"github.com/daaku/go.httpgzip"
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func stubHandler(response string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(response))
})
}
func TestWithoutGzip(t *testing.T) {
const resp = "hello"
handler := httpgzip.NewHandler(stubHandler(resp))
writer := httptest.NewRecorder()
handler.ServeHTTP(writer, &http.Request{Method: "GET"})
if writer.Body == nil {
t.Fatal("expected a body")
}
if l := writer.Body.Len(); l != len(resp) {
t.Fatalf("invalid body length, got %d", l)
}
vary := writer.Header()["Vary"]
if !reflect.DeepEqual(vary, []string{"Accept-Encoding"}) {
t.Fatalf("expected a Vary header with value Accept-Encoding, got %v", vary)
}
}
func TestWithoutGzipWithMultipleVaryHeaders(t *testing.T) {
const resp = "hello"
handler := httpgzip.NewHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Vary", "Foo")
w.Write([]byte(resp))
}))
writer := httptest.NewRecorder()
handler.ServeHTTP(writer, &http.Request{Method: "GET"})
if writer.Body == nil {
t.Fatal("expected a body")
}
if l := writer.Body.Len(); l != len(resp) {
t.Fatalf("invalid body length, got %d", l)
}
vary := writer.Header()["Vary"]
if !reflect.DeepEqual(vary, []string{"Accept-Encoding", "Foo"}) {
t.Fatalf("invalid Vary headers, got %#v", vary)
}
}
func TestWithGzip(t *testing.T) {
handler := httpgzip.NewHandler(stubHandler("hello"))
writer := httptest.NewRecorder()
handler.ServeHTTP(writer, &http.Request{
Method: "GET",
Header: http.Header{
"Accept-Encoding": []string{"gzip"},
},
})
if writer.Body == nil {
t.Fatal("expected a body")
}
if l := writer.Body.Len(); l != 29 {
t.Fatalf("invalid body length, got %d", l)
}
vary := writer.Header()["Vary"]
if !reflect.DeepEqual(vary, []string{"Accept-Encoding"}) {
t.Fatalf("invalid Vary headers, got %#v", vary)
}
}
func TestWithGzipAndMultipleVaryHeader(t *testing.T) {
handler := httpgzip.NewHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Vary", "Foo")
w.Write([]byte("hello"))
}))
writer := httptest.NewRecorder()
handler.ServeHTTP(writer, &http.Request{
Method: "GET",
Header: http.Header{
"Accept-Encoding": []string{"gzip"},
},
})
if writer.Body == nil {
t.Fatal("expected a body")
}
if l := writer.Body.Len(); l != 29 {
t.Fatalf("invalid body length, got %d", l)
}
vary := writer.Header()["Vary"]
if !reflect.DeepEqual(vary, []string{"Accept-Encoding", "Foo"}) {
t.Fatalf("invalid Vary headers, got %#v", vary)
}
}
func TestWithGzipReal(t *testing.T) {
const raw = "hello"
handler := httpgzip.NewHandler(stubHandler(raw))
server := httptest.NewServer(handler)
defer server.Close()
resp, err := http.Get(server.URL)
if err != nil {
t.Fatalf("failed http request: %s", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if string(body) != raw {
t.Fatalf(`did not find expected "%s" but got "%s" instead`, raw, resp)
}
vary := resp.Header["Vary"]
if !reflect.DeepEqual(vary, []string{"Accept-Encoding"}) {
t.Fatalf("invalid Vary headers, got %#v", vary)
}
}
func TestWithGzipRealAndMultipleVaryHeaders(t *testing.T) {
const raw = "hello"
handler := httpgzip.NewHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Vary", "Foo")
w.Write([]byte(raw))
}))
server := httptest.NewServer(handler)
defer server.Close()
resp, err := http.Get(server.URL)
if err != nil {
t.Fatalf("failed http request: %s", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if string(body) != raw {
t.Fatalf(`did not find expected "%s" but got "%s" instead`, raw, resp)
}
vary := resp.Header["Vary"]
if !reflect.DeepEqual(vary, []string{"Accept-Encoding", "Foo"}) {
t.Fatalf("invalid Vary headers, got %#v", vary)
}
}
func TestWithGzipDoubleWrite(t *testing.T) {
handler := httpgzip.NewHandler(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write(bytes.Repeat([]byte("foo"), 1000))
w.Write(bytes.Repeat([]byte("bar"), 1000))
}))
writer := httptest.NewRecorder()
handler.ServeHTTP(writer, &http.Request{
Method: "GET",
Header: http.Header{
"Accept-Encoding": []string{"gzip"},
},
})
if writer.Body == nil {
t.Fatal("expected a body")
}
if l := writer.Body.Len(); l != 54 {
t.Fatalf("invalid body length, got %d", l)
}
}