-
Notifications
You must be signed in to change notification settings - Fork 2
/
handler_test.go
133 lines (120 loc) · 3.28 KB
/
handler_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
package compress
import (
"bytes"
"fmt"
// "golang.org/x/net/http2"
"io"
"net/http"
"net/http/httptest"
"testing"
)
type testableContent struct {
needsGzip bool
body []byte
}
var testableContents = []*testableContent{
{false, []byte("")},
{false, []byte("foobar")},
{false, []byte("%PDF-")},
{false, []byte("<!DOCTYPE HTML>")},
{true, addGzippableMinSize([]byte(""))},
{true, addGzippableMinSize([]byte("foobar"))},
{false, addGzippableMinSize([]byte("%PDF-"))},
{true, addGzippableMinSize([]byte("<!DOCTYPE HTML>"))},
}
func addGzippableMinSize(b []byte) []byte {
return append(b, make([]byte, gzippableMinSize)...)
}
type testCase struct {
t *testing.T
acceptEncoding string
handler http.HandlerFunc
test func(*testableContent, *http.Response) []string
}
func test(tc *testCase) {
ts := httptest.NewServer(HandleFunc(tc.handler))
defer ts.Close()
for _, c := range testableContents {
req, err := http.NewRequest("GET", ts.URL, bytes.NewReader(c.body))
if err != nil {
tc.t.Fatal(err)
}
req.Header.Set("Accept-Encoding", tc.acceptEncoding)
res, err := http.DefaultClient.Do(req)
if err != nil {
tc.t.Fatal(err)
}
if errs := tc.test(c, res); len(errs) > 0 {
for _, err := range errs {
tc.t.Errorf("%v bytes %q: %v", len(c.body), res.Header.Get("Content-Type"), err)
}
}
}
}
func TestGzipAcceptance(t *testing.T) {
test(&testCase{
t: t,
acceptEncoding: "otherThanGzip",
handler: func(w http.ResponseWriter, r *http.Request) {
io.Copy(w, r.Body)
},
test: func(_ *testableContent, res *http.Response) (errs []string) {
if resce := res.Header.Get("Content-Encoding"); resce != "" {
errs = append(errs, "gzipped response without client acceptance")
}
return
},
})
}
func TestResponseAlreadyEncoded(t *testing.T) {
encoding := "otherThanGzip"
test(&testCase{
t: t,
acceptEncoding: "gzip",
handler: func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Encoding", encoding)
io.Copy(w, r.Body)
},
test: func(c *testableContent, res *http.Response) (errs []string) {
if resce := res.Header.Get("Content-Encoding"); resce != encoding {
errs = append(errs, fmt.Sprintf("response already encoded: want %v, got %v", encoding, resce))
}
return
},
})
}
func TestGzip(t *testing.T) {
test(&testCase{
t: t,
acceptEncoding: "gzip",
handler: func(w http.ResponseWriter, r *http.Request) {
io.Copy(w, r.Body)
},
test: func(c *testableContent, res *http.Response) (errs []string) {
if resce := res.Header.Get("Content-Encoding"); c.needsGzip && resce != "gzip" {
errs = append(errs, "gzip needed")
} else if !c.needsGzip && resce != "" {
errs = append(errs, "gzip not needed")
}
return
},
})
}
func TestGzipWriteHeader(t *testing.T) {
status := http.StatusTeapot
test(&testCase{
t: t,
acceptEncoding: "gzip",
handler: func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(status)
io.Copy(w, r.Body)
w.Write(nil) // Use Write when cw.gzipChecked.
},
test: func(c *testableContent, res *http.Response) (errs []string) {
if res.StatusCode != status {
errs = append(errs, fmt.Sprintf("write header: want %v, got %v", status, res.StatusCode))
}
return
},
})
}