-
Notifications
You must be signed in to change notification settings - Fork 7
/
route_test.go
148 lines (137 loc) · 3.82 KB
/
route_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
package vox
import (
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
)
func TestRoute(t *testing.T) {
app := New()
app.SetConfig("logging:disable", "true")
app.Route("GET", "/test_route", func(ctx *Context, req *Request, res *Response) {
res.Body = "Hello Vox!"
res.Header.Set("foo", "bar")
})
r := httptest.NewRequest("GET", "http://test.com/test_route", nil)
w := httptest.NewRecorder()
app.ServeHTTP(w, r)
if w.Result().StatusCode != 200 {
t.Errorf("expect StatusCode 200, got %d\r\n", w.Result().StatusCode)
}
r = httptest.NewRequest("GET", "http://test.com/invalid_path", nil)
w = httptest.NewRecorder()
app.ServeHTTP(w, r)
if w.Result().StatusCode != 404 {
t.Errorf("expect StatusCode 404, got %d\r\n", w.Result().StatusCode)
}
}
func TestMatchAnyMethod(t *testing.T) {
app := New()
app.SetConfig("logging:disable", "true")
app.Route("*", "/test_route", func(ctx *Context, req *Request, res *Response) {
res.Body = "matched!"
res.Status = http.StatusFound
})
r := httptest.NewRequest("ANYMETHOD", "http://test.com/test_route", nil)
w := httptest.NewRecorder()
app.ServeHTTP(w, r)
if w.Result().StatusCode != http.StatusFound {
t.Errorf("expect StatusCode 302, got %d\r\n", w.Result().StatusCode)
}
}
func TestRouteWithParams(t *testing.T) {
app := New()
app.SetConfig("logging:disable", "true")
app.Route("GET", `/(?P<first>\w+)/\w+/(?P<second>\w+)`, func(ctx *Context, req *Request, res *Response) {
res.Body = "Hello Vox!"
if req.Params["first"] != "foo" {
t.Fail()
}
if req.Params["second"] != "bar" {
t.Fail()
}
})
r := httptest.NewRequest("GET", "http://test.com/foo/xxxxx/bar", nil)
w := httptest.NewRecorder()
app.ServeHTTP(w, r)
if w.Result().StatusCode != 200 {
t.Fail()
}
}
func TestRouteShortcut(t *testing.T) {
methods := []string{
"Get",
"Head",
"Post",
"Put",
"Patch",
"Delete",
"Options",
"Trace",
}
app := New()
app.SetConfig("logging:disable", "true")
for _, method := range methods {
args := []reflect.Value{}
args = append(args, reflect.ValueOf("/"))
args = append(args, reflect.ValueOf(func(ctx *Context, req *Request, res *Response) {
res.Body = method
}))
reflect.ValueOf(app).MethodByName(method).Call(args)
}
for _, method := range methods {
r := httptest.NewRequest(strings.ToUpper(method), "http://test.com/", nil)
w := httptest.NewRecorder()
app.ServeHTTP(w, r)
if w.Result().StatusCode != 200 && w.Body.String() != method {
t.Fail()
}
}
}
func TestRouteFallthrough(t *testing.T) {
app := New()
app.SetConfig("logging:disable", "true")
app.Get("/fallthrough", func(ctx *Context, req *Request, res *Response) {
})
app.Use(func(ctx *Context, req *Request, res *Response) {
res.Body = "fallthrough"
})
r := httptest.NewRequest("Get", "http://test.com/", nil)
w := httptest.NewRecorder()
app.ServeHTTP(w, r)
if w.Result().StatusCode != 200 && w.Body.String() != "fallthrough" {
t.Fail()
}
}
func TestRouteToRegexp(t *testing.T) {
fn := func(input, expect string) {
result := routeToRegexp(input)
if result.String() != expect {
t.Errorf("expect %s, got %s", expect, result)
}
}
fn("/xxx/foo", "^/xxx/foo$")
fn(`/xxx/(?P<name>\w+)`, `^/xxx/(?P<name>\w+)$`)
fn(`/xxx/{name}`, `^/xxx/(?P<name>[-_.\p{Lu}\p{Ll}0-9]+)$`)
fn(`/xxx/{name2}`, `^/xxx/(?P<name2>[-_.\p{Lu}\p{Ll}0-9]+)$`)
}
func TestRouteWithUnicodeParams(t *testing.T) {
app := New()
app.SetConfig("logging:disable", "true")
app.Route("GET", `/{first}/\w+/{second}`, func(ctx *Context, req *Request, res *Response) {
res.Body = "Hello Vox!"
if req.Params["first"] != "éèçà" {
t.Fail()
}
if req.Params["second"] != "aa_1-.aspx" {
t.Fail()
}
})
r := httptest.NewRequest("GET", "http://test.com/éèçà/xxxxx/aa_1-.aspx", nil)
w := httptest.NewRecorder()
app.ServeHTTP(w, r)
if w.Result().StatusCode != 200 {
t.Fail()
}
}