-
Notifications
You must be signed in to change notification settings - Fork 691
/
container_test.go
173 lines (160 loc) · 4.95 KB
/
container_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package restful
import (
"context"
"encoding/hex"
"net/http"
"net/http/httptest"
"testing"
)
// go test -v -test.run TestContainer_computeAllowedMethods ...restful
func TestContainer_computeAllowedMethods(t *testing.T) {
wc := NewContainer()
ws1 := new(WebService).Path("/users")
ws1.Route(ws1.GET("{i}").To(dummy))
ws1.Route(ws1.POST("{i}").To(dummy))
wc.Add(ws1)
httpRequest, _ := http.NewRequest("GET", "http://api.his.com/users/1", nil)
rreq := Request{Request: httpRequest}
m := wc.computeAllowedMethods(&rreq)
if len(m) != 2 {
t.Errorf("got %d expected 2 methods, %v", len(m), m)
}
}
func TestContainer_HandleWithFilter(t *testing.T) {
prefilterCalled := false
postfilterCalled := false
httpHandlerCalled := false
contextAvailable := false
wc := NewContainer()
wc.Filter(func(request *Request, response *Response, chain *FilterChain) {
prefilterCalled = true
request.Request = request.Request.WithContext(context.WithValue(request.Request.Context(), "prefilterContextSet", "true"))
chain.ProcessFilter(request, response)
})
wc.HandleWithFilter("/", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
httpHandlerCalled = true
_, ok1 := req.Context().Value("prefilterContextSet").(string)
_, ok2 := req.Context().Value("postfilterContextSet").(string)
if ok1 && ok2 {
contextAvailable = true
}
w.Write([]byte("ok"))
}))
wc.Filter(func(request *Request, response *Response, chain *FilterChain) {
postfilterCalled = true
request.Request = request.Request.WithContext(context.WithValue(request.Request.Context(), "postfilterContextSet", "true"))
chain.ProcessFilter(request, response)
})
recorder := httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/", nil)
wc.ServeHTTP(recorder, request)
if recorder.Code != http.StatusOK {
t.Errorf("unexpected code %d", recorder.Code)
}
if recorder.Body.String() != "ok" {
t.Errorf("unexpected body %s", recorder.Body.String())
}
if !prefilterCalled {
t.Errorf("filter added before calling HandleWithFilter wasn't called")
}
if !postfilterCalled {
t.Errorf("filter added after calling HandleWithFilter wasn't called")
}
if !httpHandlerCalled {
t.Errorf("handler added by calling HandleWithFilter wasn't called")
}
if !contextAvailable {
t.Errorf("Context not available in http handler")
}
}
func TestContainerAddAndRemove(t *testing.T) {
ws1 := new(WebService).Path("/")
ws2 := new(WebService).Path("/users")
wc := NewContainer()
wc.Add(ws1)
wc.Add(ws2)
wc.Remove(ws2)
if len(wc.webServices) != 1 {
t.Errorf("expected one webservices")
}
if !wc.isRegisteredOnRoot {
t.Errorf("expected on root registered")
}
wc.Remove(ws1)
if len(wc.webServices) > 0 {
t.Errorf("expected zero webservices")
}
if wc.isRegisteredOnRoot {
t.Errorf("expected not on root registered")
}
}
func TestContainerCompressResponse(t *testing.T) {
wc := NewContainer()
ws := new(WebService).Path("/")
ws.Route(ws.GET("/").To(dummy))
wc.Add(ws)
// no accept header, encoding disabled
{
recorder := httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/", nil)
wc.ServeHTTP(recorder, request)
if recorder.Code != http.StatusOK {
t.Errorf("unexpected code %d", recorder.Code)
}
if recorder.Body.String() != "dummy" {
t.Errorf("unexpected body %s", recorder.Body.String())
}
}
// with gzip accept header, encoding disabled
{
recorder := httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/", nil)
request.Header.Set("accept-encoding", "gzip")
wc.ServeHTTP(recorder, request)
if recorder.Code != http.StatusOK {
t.Errorf("unexpected code %d", recorder.Code)
}
if recorder.Body.String() != "dummy" {
t.Errorf("unexpected body %s", recorder.Body.String())
}
}
// no accept header, encoding enabled
{
wc.EnableContentEncoding(true)
recorder := httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/", nil)
wc.ServeHTTP(recorder, request)
if recorder.Code != http.StatusOK {
t.Errorf("unexpected code %d", recorder.Code)
}
if recorder.Body.String() != "dummy" {
t.Errorf("unexpected body %s", recorder.Body.String())
}
}
// with accept gzip header, encoding enabled
{
wc.EnableContentEncoding(true)
recorder := httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/", nil)
request.Header.Set("accept-encoding", "gzip")
wc.ServeHTTP(recorder, request)
if recorder.Code != http.StatusOK {
t.Errorf("unexpected code %d", recorder.Code)
}
if hex.EncodeToString(recorder.Body.Bytes()) == hex.EncodeToString(gzippedDummy()) {
t.Errorf("unexpected body %v", recorder.Body.Bytes())
}
}
// response says it is already compressed
{
wc.EnableContentEncoding(true)
recorder := httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/", nil)
request.Header.Set("accept-encoding", "gzip")
recorder.HeaderMap.Set("content-encoding", "gzip")
wc.ServeHTTP(recorder, request)
if recorder.Body.String() != "dummy" {
t.Errorf("unexpected body %s", recorder.Body.String())
}
}
}