-
Notifications
You must be signed in to change notification settings - Fork 7
/
routergroup_test.go
170 lines (142 loc) · 4.31 KB
/
routergroup_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
package neko
import (
"bytes"
. "github.com/smartystreets/goconvey/convey"
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"strings"
"testing"
)
func Test_Router(t *testing.T) {
testRoute("GET", t)
testRoute("POST", t)
testRoute("DELETE", t)
testRoute("PATCH", t)
testRoute("PUT", t)
testRoute("OPTIONS", t)
testRoute("HEAD", t)
testRouteAny(t)
testGroup(t)
testStatic(t)
}
func performRequest(r http.Handler, method, path string, postData string) *httptest.ResponseRecorder {
req, _ := http.NewRequest(method, path, nil)
if strings.ToLower(method) == "post" {
data, _ := url.ParseQuery(postData)
req, _ = http.NewRequest(method, path, bytes.NewBufferString(data.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
} else if strings.ToLower(method) == "post|json" {
req, _ = http.NewRequest("POST", path, bytes.NewBufferString(postData))
req.Header.Add("Content-Type", "application/json;")
}
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
return w
}
func testRoute(method string, t *testing.T) {
Convey(method+" Method", t, func() {
passed := false
m := New()
switch method {
case "GET":
m.GET("", func(ctx *Context) { passed = true })
case "POST":
m.POST("", func(ctx *Context) { passed = true })
case "DELETE":
m.DELETE("", func(ctx *Context) { passed = true })
case "PATCH":
m.PATCH("", func(ctx *Context) { passed = true })
case "PUT":
m.PUT("", func(ctx *Context) { passed = true })
case "OPTIONS":
m.OPTIONS("", func(ctx *Context) { passed = true })
case "HEAD":
m.HEAD("", func(ctx *Context) { passed = true })
}
// RUN
w := performRequest(m, method, "/", "")
So(passed, ShouldBeTrue)
So(w.Code, ShouldEqual, http.StatusOK)
})
}
func testRouteAny(t *testing.T) {
Convey("Any Method", t, func() {
passed := false
m := New()
m.Any("", func(ctx *Context) { passed = true })
w := performRequest(m, "GET", "/", "")
So(passed, ShouldBeTrue)
So(w.Code, ShouldEqual, http.StatusOK)
passed = false
w = performRequest(m, "POST", "/", "")
So(passed, ShouldBeTrue)
So(w.Code, ShouldEqual, http.StatusOK)
passed = false
w = performRequest(m, "PUT", "/", "")
So(passed, ShouldBeTrue)
So(w.Code, ShouldEqual, http.StatusOK)
passed = false
w = performRequest(m, "PATCH", "/", "")
So(passed, ShouldBeTrue)
So(w.Code, ShouldEqual, http.StatusOK)
passed = false
w = performRequest(m, "HEAD", "/", "")
So(passed, ShouldBeTrue)
So(w.Code, ShouldEqual, http.StatusOK)
passed = false
w = performRequest(m, "OPTIONS", "/", "")
So(passed, ShouldBeTrue)
So(w.Code, ShouldEqual, http.StatusOK)
passed = false
w = performRequest(m, "DELETE", "/", "")
So(passed, ShouldBeTrue)
So(w.Code, ShouldEqual, http.StatusOK)
passed = false
w = performRequest(m, "CONNECT", "/", "")
So(passed, ShouldBeTrue)
So(w.Code, ShouldEqual, http.StatusOK)
passed = false
w = performRequest(m, "TRACE", "/", "")
So(passed, ShouldBeTrue)
So(w.Code, ShouldEqual, http.StatusOK)
passed = false
w = performRequest(m, "NO", "/", "")
So(passed, ShouldBeFalse)
So(w.Code, ShouldEqual, http.StatusMethodNotAllowed)
})
}
func testGroup(t *testing.T) {
Convey("Group routing", t, func() {
passedGroup, passedGroup2, passedNest := false, false, false
m := New()
v1 := m.Group("/v1", func(router *RouterGroup) {
router.GET("/test", func(ctx *Context) { passedGroup = true })
router.Group("/sub", func(sub *RouterGroup) {
sub.GET("/test", func(ctx *Context) { passedNest = true })
})
})
v1.GET("/", func(ctx *Context) { passedGroup2 = true })
performRequest(m, "GET", "/v1/test", "")
So(passedGroup, ShouldBeTrue)
performRequest(m, "GET", "/v1/", "")
So(passedGroup2, ShouldBeTrue)
performRequest(m, "GET", "/v1/sub/test", "")
So(passedNest, ShouldBeTrue)
w := performRequest(m, "GET", "/v2/test", "")
So(w.Code, ShouldEqual, http.StatusNotFound)
})
}
func testStatic(t *testing.T) {
Convey("Static serves", t, func() {
m := New()
So(func() { m.Static("", "test/") }, ShouldPanic)
m.Static("/static", "test/")
w := performRequest(m, "GET", "/static/test.css", "")
So(w.Code, ShouldEqual, http.StatusOK)
w = performRequest(m, "GET", "/static/test1.css", "")
So(w.Code, ShouldEqual, http.StatusNotFound)
})
}