-
Notifications
You must be signed in to change notification settings - Fork 382
/
Copy pathgzip_test.go
68 lines (54 loc) · 1.55 KB
/
gzip_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
package rest
import (
"github.com/ant0ine/go-json-rest/rest/test"
"testing"
)
func TestGzipEnabled(t *testing.T) {
api := NewApi()
// the middleware to test
api.Use(&GzipMiddleware{})
// router app with success and error paths
router, err := MakeRouter(
Get("/ok", func(w ResponseWriter, r *Request) {
w.WriteJson(map[string]string{"Id": "123"})
}),
Get("/error", func(w ResponseWriter, r *Request) {
Error(w, "gzipped error", 500)
}),
)
if err != nil {
t.Fatal(err)
}
api.SetApp(router)
// wrap all
handler := api.MakeHandler()
recorded := test.RunRequest(t, handler, test.MakeSimpleRequest("GET", "http://localhost/ok", nil))
recorded.CodeIs(200)
recorded.ContentTypeIsJson()
recorded.ContentEncodingIsGzip()
recorded.HeaderIs("Vary", "Accept-Encoding")
recorded = test.RunRequest(t, handler, test.MakeSimpleRequest("GET", "http://localhost/error", nil))
recorded.CodeIs(500)
recorded.ContentTypeIsJson()
recorded.ContentEncodingIsGzip()
recorded.HeaderIs("Vary", "Accept-Encoding")
}
func TestGzipDisabled(t *testing.T) {
api := NewApi()
// router app with success and error paths
router, err := MakeRouter(
Get("/ok", func(w ResponseWriter, r *Request) {
w.WriteJson(map[string]string{"Id": "123"})
}),
)
if err != nil {
t.Fatal(err)
}
api.SetApp(router)
handler := api.MakeHandler()
recorded := test.RunRequest(t, handler, test.MakeSimpleRequest("GET", "http://localhost/ok", nil))
recorded.CodeIs(200)
recorded.ContentTypeIsJson()
recorded.HeaderIs("Content-Encoding", "")
recorded.HeaderIs("Vary", "")
}