-
Notifications
You must be signed in to change notification settings - Fork 0
/
route_test.go
108 lines (102 loc) · 2.47 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
package medium
import (
"context"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRouteMatching(t *testing.T) {
tests := map[string]struct {
reqMethod string
reqPath string
routeMethod string
routePath string
want bool
params map[string]string
}{
"length check": {
reqMethod: "GET",
reqPath: "/foo",
routeMethod: "GET",
routePath: "/foo/bar",
want: false,
params: nil,
},
"path mismatch": {
reqMethod: "GET",
reqPath: "/foo",
routeMethod: "GET",
routePath: "/bar",
want: false,
params: nil,
},
"method mismatch": {
reqMethod: "CONNECT",
reqPath: "/foo",
routeMethod: "GET",
routePath: "/foo",
want: false,
params: nil,
},
"valid root": {
reqMethod: "GET",
reqPath: "/",
routeMethod: "GET",
routePath: "/",
want: true,
params: map[string]string{},
},
"valid basic route": {
reqMethod: "GET",
reqPath: "/foo",
routeMethod: "GET",
routePath: "/foo",
want: true,
params: map[string]string{},
},
"valid long route": {
reqMethod: "GET",
reqPath: "/foo/baz/bar",
routeMethod: "GET",
routePath: "/foo/baz/bar",
want: true,
params: map[string]string{},
},
"valid route params": {
reqMethod: "GET",
reqPath: "/foo/baz/bar?name=true",
routeMethod: "GET",
routePath: "/foo/baz/bar",
want: true,
params: map[string]string{},
},
"valid dynamic route": {
reqMethod: "GET",
reqPath: "/hello/greg",
routeMethod: "GET",
routePath: "/hello/:name",
want: true,
params: map[string]string{"name": "greg"},
},
"valid multi dynamic route": {
reqMethod: "GET",
reqPath: "/hello/greg/boston",
routeMethod: "GET",
routePath: "/hello/:name/:location",
want: true,
params: map[string]string{"name": "greg", "location": "boston"},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
req := httptest.NewRequest(tc.reqMethod, tc.reqPath, nil)
root := &RootRequest{originalRequest: req}
route := newRoute(tc.routeMethod, tc.routePath, func(context.Context, *Request[NoData]) Response {
return OK()
})
got, params := route.IsMatch(root)
assert.Equal(t, got, tc.want, "expected route to match")
assert.Equal(t, params, tc.params, "expected route to match")
})
}
}