-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgzip_test.go
75 lines (58 loc) · 1.53 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
69
70
71
72
73
74
75
package pgo2
import (
"bytes"
"net/http/httptest"
"testing"
"github.com/pinguo/pgo2/iface"
)
func TestNewGzip(t *testing.T) {
var obj interface{}
obj = NewGzip()
if _, ok := obj.(iface.IPlugin); ok == false {
t.FailNow()
}
}
func TestGzip_HandleRequest(t *testing.T) {
body := bytes.NewReader([]byte(""))
t.Run("Accept-Encoding!=gzip", func(t *testing.T) {
r := httptest.NewRequest("GET", "/view", body)
w := httptest.NewRecorder()
context := &Context{}
context.HttpRW(false,true, r, w)
gzip := NewGzip()
gzip.HandleRequest(context)
if w.Result().Header.Get("Content-Encoding") != "" {
t.FailNow()
}
})
t.Run("img", func(t *testing.T) {
r := httptest.NewRequest("GET", "/view.png", body)
r.Header.Set("Accept-Encoding", "gzip")
w := httptest.NewRecorder()
context := &Context{}
context.HttpRW(false,true, r, w)
gzip := NewGzip()
gzip.HandleRequest(context)
if w.Result().Header.Get("Content-Encoding") != "" {
t.FailNow()
}
})
t.Run("normal", func(t *testing.T) {
r := httptest.NewRequest("GET", "/view.html", body)
r.Header.Set("Accept-Encoding", "gzip")
w := httptest.NewRecorder()
context := &Context{}
context.HttpRW(false,true, r, w)
gzip := NewGzip()
gzip.HandleRequest(context)
str := "ss"
//context.response.WriteString(str)
context.Output().Write([]byte(str))
if w.Result().Header.Get("Content-Encoding") != "gzip" {
t.Fatal(` w.Result().Header.Get("Content-Encoding")!="gzip"`)
}
if len(w.Body.String()) < 1 {
t.Fatal(`len(w.Body.String()) <1`)
}
})
}