-
Notifications
You must be signed in to change notification settings - Fork 0
/
gzong_test.go
270 lines (234 loc) · 6.31 KB
/
gzong_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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package gzong
import (
"github.com/cshwen/gzong/middleware"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
"time"
)
const contentType = "Content-Type"
const contentTypeTextHTML = "text/html;charset=UTF-8"
const contentTypeApplicationJSON = "application/json"
const success = "success"
const postBody = `{"test": "ss"}`
func TestNew(t *testing.T) {
gz := New()
if gz == nil {
t.Error("gzong New()不应返回为空")
}
}
func testPost(w http.ResponseWriter, r *http.Request) {
bodyBytes, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Println(err)
}
w.Header().Set(contentType, contentTypeApplicationJSON)
w.WriteHeader(http.StatusOK)
w.Write(bodyBytes)
}
func testGet(w http.ResponseWriter, r *http.Request) {
w.Header().Set(contentType, contentTypeTextHTML)
w.WriteHeader(http.StatusOK)
w.Write([]byte(success))
}
func TestRouter_Add(t *testing.T) {
gz := New()
gz.GET("/get", testGet)
gz.POST("/post", testGet)
gz.PUT("/put", testGet)
gz.Add("/add", "GET", testGet)
gz.Add("/add", "POST", testGet)
if len(gz.handlersMap) != 4 {
t.Error("路由个数不符合预期构建数目")
}
routeNum := 0
for _, handlers := range gz.handlersMap {
routeNum += len(handlers)
}
if routeNum != 5 {
t.Error("路由总个数不符合预期构建数目")
}
}
func TestRouter_AddMiddleware(t *testing.T) {
gz := New()
gz.GET("/test", testGet)
tsOk := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gz.ServeHTTP(w, r)
}))
defer tsOk.Close()
resp, err := http.Get(tsOk.URL + "/test")
if err != nil {
t.Log(err)
}
if resp.StatusCode != http.StatusOK {
t.Error("GET请求存在的地址,响应返回的状态码不符合预期")
}
name, pwd := "ss", "pwd"
u := middleware.BaseUser{Name: name, Pwd: pwd}
gz.AddMiddleware(u.BasicAuth)
tsUn := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gz.ServeHTTP(w, r)
}))
defer tsUn.Close()
resp, err = http.Get(tsUn.URL + "/test")
if err != nil {
t.Log(err)
}
if resp.StatusCode != http.StatusUnauthorized {
t.Error("请求添加了中间件basicAuth的服务,未携带认证信息应不通过")
}
req, err := http.NewRequest("GET", tsUn.URL+"/test", nil)
if err != nil {
t.Log(err)
}
req.Header.Add("Authorization", "Basic "+middleware.Base64Encode(name, pwd))
resp, err = http.DefaultClient.Do(req)
if err != nil {
t.Log(err)
}
if resp.StatusCode != http.StatusOK {
t.Error("请求添加了中间件basicAuth的服务,携带认证信息应顺利通过")
}
}
func TestRouter_POST(t *testing.T) {
gz := New()
gz.POST("/test", testPost)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gz.ServeHTTP(w, r)
}))
defer ts.Close()
resp, err := http.Post(ts.URL+"/test", contentTypeApplicationJSON, strings.NewReader(postBody))
if err != nil {
t.Log(err)
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Log(err)
}
if resp.StatusCode != http.StatusOK {
t.Error("POST请求存在的地址,响应返回的状态码不符合预期")
}
if resp.Header.Get(contentType) != contentTypeApplicationJSON {
t.Error("POST请求存在的地址,响应返回的header不符合预期")
}
if string(bodyBytes) != postBody {
t.Error("POST请求存在的地址,响应返回的body不符合预期")
}
}
func TestRouter_PUT(t *testing.T) {
gz := New()
gz.PUT("/test", testPost)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gz.ServeHTTP(w, r)
}))
defer ts.Close()
req, err := http.NewRequest("PUT", ts.URL+"/test", strings.NewReader(postBody))
if err != nil {
t.Log(err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Log(err)
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Log(err)
}
if resp.StatusCode != http.StatusOK {
t.Error("PUT请求存在的地址,响应返回的状态码不符合预期")
}
if resp.Header.Get(contentType) != contentTypeApplicationJSON {
t.Error("PUT请求存在的地址,响应返回的header不符合预期")
}
if string(bodyBytes) != postBody {
t.Error("PUT请求存在的地址,响应返回的body不符合预期")
}
}
func TestRouter_ServeHTTP(t *testing.T) {
gz := New()
gz.GET("/test", testGet)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gz.ServeHTTP(w, r)
}))
defer ts.Close()
resp, err := http.Get(ts.URL + "/test")
if err != nil {
t.Log(err)
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Log(err)
}
if resp.StatusCode != http.StatusOK || string(bodyBytes) != success {
t.Error("GET请求存在的地址访问异常")
}
resp, err = http.Get(ts.URL + "/error")
if err != nil {
t.Log(err)
}
if resp.StatusCode != http.StatusNotFound {
t.Error("GET请求不存在的地址应404")
}
}
func TestRouter_Run(t *testing.T) {
port := 9872
strPort := ":" + strconv.Itoa(port)
gz := New()
gz.GET("/test", testGet)
go func() {
gz.Run(strPort)
}()
time.Sleep(233 * time.Millisecond)
resp, err := http.Get("http://localhost" + strPort + "/test")
if err != nil {
t.Log(err)
}
if resp.StatusCode != http.StatusOK {
t.Error("GET请求存在的地址访问异常")
}
resp, err = http.Get("http://localhost" + strPort + "/error")
if err != nil {
t.Log(err)
}
if resp.StatusCode != http.StatusNotFound {
t.Error("GET请求不存在的地址应404")
}
}
func TestRouter_GET(t *testing.T) {
port := 9871
strPort := ":" + strconv.Itoa(port)
gz := New()
gz.GET("/test", testGet)
go func() {
gz.Run(strPort)
}()
time.Sleep(233 * time.Millisecond)
resp, err := http.Get("http://127.0.0.1" + strPort + "/test")
if err != nil {
t.Log(err)
}
if resp.StatusCode != http.StatusOK {
t.Error("GET请求存在的地址,响应返回的状态码不符合预期")
}
if resp.Header.Get(contentType) != contentTypeTextHTML {
t.Error("GET请求存在的地址,响应返回的header不符合预期")
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Log(err)
}
if string(bodyBytes) != success {
t.Error("GET请求存在的地址,响应返回的body不符合预期")
}
resp, err = http.Get("http://127.0.0.1" + strPort + "/error")
if err != nil {
t.Log(err)
}
if resp.StatusCode != http.StatusNotFound {
t.Error("GET请求不存在的地址应404")
}
}