-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathship_test.go
219 lines (197 loc) · 5.39 KB
/
ship_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
// Copyright 2020 xgfone
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ship
import (
"bytes"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/xgfone/ship/v5/router/echo"
)
func TestNotFound(t *testing.T) {
notFound := func(c *Context) error {
return c.Text(http.StatusNotFound, http.StatusText(http.StatusNotFound))
}
router := New()
router.NotFound = notFound
router.Route("/home/").GET(OkHandler())
router.Route("/home/").POST(OkHandler())
router.Route("/users/:id").GET(OkHandler())
router.Route("/users/:id/:id2/:id3").GET(OkHandler())
req, _ := http.NewRequest(http.MethodOptions, "/home/", nil)
rec := httptest.NewRecorder()
router.ServeHTTP(rec, req)
if rec.Code != http.StatusNotFound {
t.Errorf("StatusCode: expect %d, got %d", http.StatusNotFound, rec.Code)
}
req, _ = http.NewRequest(http.MethodGet, "/users/14/more", nil)
rec = httptest.NewRecorder()
router.ServeHTTP(rec, req)
if rec.Code != http.StatusNotFound {
t.Errorf("StatusCode: expect %d, got %d", http.StatusNotFound, rec.Code)
}
}
func TestMethodNotAllowed(t *testing.T) {
router := New()
router.Router = echo.NewRouter(&echo.Config{
MethodNotAllowedHandler: func(allowedMethods []string) interface{} {
return MethodNotAllowedHandler(allowedMethods)
}},
)
router.Route("/path").GET(OkHandler()).POST(OkHandler())
req, _ := http.NewRequest(http.MethodPut, "/path", nil)
rec := httptest.NewRecorder()
router.ServeHTTP(rec, req)
if rec.Code != 405 {
t.Errorf("expect status code '%d', but got '%d'", 405, rec.Code)
} else if methods := rec.Header().Get(HeaderAllow); methods != "GET, POST" {
t.Errorf("expect Allow header '%s', but got '%v'", "GET, POST", methods)
}
}
func TestRouteFilter(t *testing.T) {
router := New()
router.RouteFilter = func(ri Route) bool {
if ri.Name == "" {
return true
} else if !strings.HasPrefix(ri.Path, "/group/") {
return true
}
return false
}
handler := func(ctx *Context) error { return nil }
router.Group("/group").Route("/name").Name("test").GET(handler)
router.Route("/noname").GET(handler)
switch routes := router.Routes(); len(routes) {
case 0:
t.Error("no routes")
case 1:
if name := routes[0].Name; name != "test" {
t.Errorf("expect route name '%s', but got '%s'", "test", name)
}
default:
t.Errorf("too many routes: %v", routes)
}
}
func TestRouteModifier(t *testing.T) {
router := New()
router.RouteModifier = func(ri Route) Route {
if !strings.HasPrefix(ri.Path, "/prefix/") {
ri.Path = "/prefix" + ri.Path
}
return ri
}
handler := func(ctx *Context) error { return nil }
router.Route("/path").GET(handler)
switch routes := router.Routes(); len(routes) {
case 0:
t.Error("no routes")
case 1:
if path := routes[0].Path; path != "/prefix/path" {
t.Errorf("expect path '%s', but got '%s'", "/prefix/path", path)
}
default:
t.Errorf("too many routes: %v", routes)
}
}
const middlewareoutput = `
pre m1 start
pre m2 start
use m1 start
use m2 start
group m1 start
group m2 start
route m1 start
route m2 start
route m2 end
route m1 end
group m2 end
group m1 end
use m2 end
use m1 end
pre m2 end
pre m1 end
`
func TestMiddleware(t *testing.T) {
bs := bytes.NewBufferString("\n")
router := New()
router.Pre(func(next Handler) Handler {
return func(ctx *Context) error {
bs.WriteString("pre m1 start\n")
err := next(ctx)
bs.WriteString("pre m1 end\n")
return err
}
}, func(next Handler) Handler {
return func(ctx *Context) error {
bs.WriteString("pre m2 start\n")
err := next(ctx)
bs.WriteString("pre m2 end\n")
return err
}
})
router.Use(func(next Handler) Handler {
return func(ctx *Context) error {
bs.WriteString("use m1 start\n")
err := next(ctx)
bs.WriteString("use m1 end\n")
return err
}
}, func(next Handler) Handler {
return func(ctx *Context) error {
bs.WriteString("use m2 start\n")
err := next(ctx)
bs.WriteString("use m2 end\n")
return err
}
})
group := router.Group("/v1")
group.Use(func(next Handler) Handler {
return func(ctx *Context) error {
bs.WriteString("group m1 start\n")
err := next(ctx)
bs.WriteString("group m1 end\n")
return err
}
}, func(next Handler) Handler {
return func(ctx *Context) error {
bs.WriteString("group m2 start\n")
err := next(ctx)
bs.WriteString("group m2 end\n")
return err
}
})
group.Route("/route").Use(func(next Handler) Handler {
return func(ctx *Context) error {
bs.WriteString("route m1 start\n")
err := next(ctx)
bs.WriteString("route m1 end\n")
return err
}
}, func(next Handler) Handler {
return func(ctx *Context) error {
bs.WriteString("route m2 start\n")
err := next(ctx)
bs.WriteString("route m2 end\n")
return err
}
}).GET(OkHandler())
req := httptest.NewRequest(http.MethodGet, "/v1/route", nil)
rec := httptest.NewRecorder()
router.ServeHTTP(rec, req)
if bs.String() != middlewareoutput {
t.Error(bs.String())
t.Fail()
}
}