-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer_test.go
65 lines (53 loc) · 1.64 KB
/
writer_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
package compress
import (
"net/http/httptest"
"testing"
"gotest.tools/assert"
)
func Test_withKnowContentEncoding(t *testing.T) {
w := httptest.NewRecorder()
c := newConfig()
rw, ok := newResponseWriter(nil, w, c, "gzip")
assert.Equal(t, ok, true)
rw.Header().Set("content-encoding", "flate")
rw.Header().Set("content-type", "text/plain")
rw.WriteHeader(401)
rw.end()
assert.Equal(t, w.Code, 401)
assert.Equal(t, w.Header().Get("content-encoding"), "flate")
}
func Test_withKnowContentLength(t *testing.T) {
w := httptest.NewRecorder()
c := newConfig()
rw, ok := newResponseWriter(nil, w, c, "gzip")
assert.Equal(t, ok, true)
rw.Header().Set("content-type", "text/plain")
rw.Header().Set("content-length", "100000")
rw.WriteHeader(400)
rw.end()
assert.Equal(t, w.Code, 400)
assert.Equal(t, w.Header().Get("content-encoding"), "gzip")
assert.Equal(t, w.Header().Get("content-length"), "")
}
func Test_withUnknownContentLength(t *testing.T) {
run := func(bodyWriteTimes int, status int, encoding string, bodyLen int) func(*testing.T) {
return func(t *testing.T) {
w := httptest.NewRecorder()
c := newConfig(WithMinSize(15))
rw, ok := newResponseWriter(nil, w, c, "gzip")
assert.Equal(t, ok, true)
rw.Header().Set("content-type", "text/plain")
rw.WriteHeader(status)
body := make([]byte, 10)
for i := 0; i < bodyWriteTimes; i++ {
rw.Write(body)
}
rw.end()
assert.Equal(t, w.Code, status)
assert.Equal(t, w.Header().Get("content-encoding"), encoding)
assert.Equal(t, w.Body.Len(), bodyLen)
}
}
t.Run("less than minSize", run(1, 403, "", 10))
t.Run("more than minSize", run(2, 403, "gzip", 27))
}