-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompressor_gzip_test.go
121 lines (103 loc) · 2.93 KB
/
compressor_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
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
package xun
import (
"compress/flate"
"compress/gzip"
"io"
"net/http"
"net/http/httptest"
"testing"
"testing/fstest"
"github.com/stretchr/testify/require"
)
func TestGzipCompressor(t *testing.T) {
fsys := fstest.MapFS{
"public/skin.css": {
Data: []byte("body { color: red; }"),
},
"pages/index.html": {
Data: []byte("<html><head><title>index</title></head><body></body></html>"),
},
}
m := http.NewServeMux()
srv := httptest.NewServer(m)
defer srv.Close()
app := New(WithMux(m), WithFsys(fsys), WithCompressor(&GzipCompressor{}, &DeflateCompressor{}))
defer app.Close()
app.Get("/json", func(c *Context) error {
return c.View(map[string]string{"message": "hello"})
})
go app.Start()
var tests = []struct {
name string
acceptEncoding string
contentEncoding string
createReader func(r io.Reader) io.Reader
}{
{
name: "gzip",
acceptEncoding: "gzip",
contentEncoding: "gzip",
createReader: func(r io.Reader) io.Reader {
gr, _ := gzip.NewReader(r)
return gr
},
},
{
name: "any",
acceptEncoding: "*",
contentEncoding: "gzip",
createReader: func(r io.Reader) io.Reader {
gr, _ := gzip.NewReader(r)
return gr
},
},
{
name: "plain",
acceptEncoding: "",
contentEncoding: "",
createReader: func(r io.Reader) io.Reader {
return r
},
},
{
name: "deflate",
acceptEncoding: "deflate",
contentEncoding: "deflate",
createReader: func(r io.Reader) io.Reader {
return flate.NewReader(r)
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, srv.URL+"/skin.css", nil)
require.NoError(t, err)
req.Header.Set("Accept-Encoding", test.acceptEncoding)
resp, err := client.Do(req)
require.NoError(t, err)
require.Equal(t, test.contentEncoding, resp.Header.Get("Content-Encoding"))
buf, err := io.ReadAll(test.createReader(resp.Body))
require.NoError(t, err)
require.Equal(t, fsys["public/skin.css"].Data, buf)
req, err = http.NewRequest(http.MethodGet, srv.URL+"/", nil)
require.NoError(t, err)
req.Header.Set("Accept-Encoding", test.acceptEncoding)
resp, err = client.Do(req)
require.NoError(t, err)
require.Equal(t, test.contentEncoding, resp.Header.Get("Content-Encoding"))
buf, err = io.ReadAll(test.createReader(resp.Body))
require.NoError(t, err)
require.Equal(t, fsys["pages/index.html"].Data, buf)
req, err = http.NewRequest(http.MethodGet, srv.URL+"/json", nil)
require.NoError(t, err)
req.Header.Set("Accept-Encoding", test.acceptEncoding)
resp, err = client.Do(req)
require.NoError(t, err)
require.Equal(t, test.contentEncoding, resp.Header.Get("Content-Encoding"))
data := make(map[string]string)
err = json.NewDecoder(test.createReader(resp.Body)).Decode(&data)
require.NoError(t, err)
require.Equal(t, "hello", data["message"])
})
}
}