-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute_test.go
More file actions
182 lines (164 loc) · 4.2 KB
/
route_test.go
File metadata and controls
182 lines (164 loc) · 4.2 KB
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
package fox
import (
"net/http"
"net/http/httptest"
"slices"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRoute_HandleMiddlewareMalloc(t *testing.T) {
f, _ := NewRouter()
for _, rte := range githubAPI {
require.NoError(t, onlyError(f.Add([]string{rte.method}, rte.path, emptyHandler)))
}
for _, rte := range githubAPI {
req := httptest.NewRequest(rte.method, rte.path, nil)
w := httptest.NewRecorder()
r, c, _ := f.Lookup(&recorder{ResponseWriter: w}, req)
allocs := testing.AllocsPerRun(100, func() {
r.HandleMiddleware(c)
})
c.Close()
assert.Equal(t, float64(0), allocs)
}
}
func TestRoute_HostnamePath(t *testing.T) {
cases := []struct {
name string
pattern string
wantPath string
wantHost string
}{
{
name: "only path",
pattern: "/foo/bar",
wantPath: "/foo/bar",
},
{
name: "only slash",
pattern: "/",
wantPath: "/",
},
{
name: "host and path",
pattern: "a.b.c/foo/bar",
wantPath: "/foo/bar",
wantHost: "a.b.c",
},
{
name: "host and slash",
pattern: "a.b.c/",
wantPath: "/",
wantHost: "a.b.c",
},
{
name: "single letter host and slash",
pattern: "a/",
wantPath: "/",
wantHost: "a",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
f, _ := NewRouter()
r, err := f.Add(MethodGet, tc.pattern, emptyHandler)
require.NoError(t, err)
assert.Equal(t, tc.wantHost, r.Hostname())
assert.Equal(t, tc.wantPath, r.Path())
})
}
}
func TestRoute_Methods(t *testing.T) {
f := MustRouter()
f.MustAdd([]string{http.MethodHead, http.MethodOptions, http.MethodGet}, "/foo/bar", emptyHandler)
route := f.Route([]string{http.MethodOptions, http.MethodHead, http.MethodGet}, "/foo/bar")
assert.Equal(t, []string{http.MethodGet, http.MethodHead, http.MethodOptions}, slices.Collect(route.Methods()))
}
func TestRoute_String(t *testing.T) {
t.Run("many methods + name + many matchers", func(t *testing.T) {
f := MustRouter()
r := f.MustAdd(
[]string{http.MethodGet, http.MethodHead}, "/foo/bar",
emptyHandler,
WithName("foo"),
WithQueryMatcher("a", "b"),
WithHeaderMatcher("a", "b"),
)
assert.Equal(t, "method:GET,HEAD pattern:/foo/bar name:foo matchers:{q:a=b,h:A=b}", r.String())
})
t.Run("single method + name + single matchers", func(t *testing.T) {
f := MustRouter()
r := f.MustAdd(
[]string{http.MethodGet}, "/foo/bar",
emptyHandler,
WithName("foo"),
WithQueryMatcher("a", "b"),
)
assert.Equal(t, "method:GET pattern:/foo/bar name:foo matchers:{q:a=b}", r.String())
})
t.Run("no method + pattern", func(t *testing.T) {
f := MustRouter()
r := f.MustAdd(
MethodAny, "/foo/bar",
emptyHandler,
)
assert.Equal(t, "method:* pattern:/foo/bar", r.String())
})
}
func TestRoute_Middleware(t *testing.T) {
var c0, c1, c2 bool
m0 := MiddlewareFunc(func(next HandlerFunc) HandlerFunc {
return func(c *Context) {
c0 = true
next(c)
}
})
m1 := MiddlewareFunc(func(next HandlerFunc) HandlerFunc {
return func(c *Context) {
c1 = true
next(c)
}
})
m2 := MiddlewareFunc(func(next HandlerFunc) HandlerFunc {
return func(c *Context) {
c2 = true
next(c)
}
})
f, err := NewRouter(WithMiddleware(m0))
require.NoError(t, err)
f.MustAdd(MethodGet, "/1", emptyHandler, WithMiddleware(m1))
f.MustAdd(MethodGet, "/2", emptyHandler, WithMiddleware(m2))
req := httptest.NewRequest(http.MethodGet, "/1", nil)
w := httptest.NewRecorder()
f.ServeHTTP(w, req)
assert.True(t, c0)
assert.True(t, c1)
assert.False(t, c2)
c0, c1, c2 = false, false, false
req.URL.Path = "/2"
f.ServeHTTP(w, req)
assert.True(t, c0)
assert.False(t, c1)
assert.True(t, c2)
c0, c1, c2 = false, false, false
rte1 := f.Route(MethodGet, "/1")
require.NotNil(t, rte1)
rte1.Handle(newTestContext(f))
assert.False(t, c0)
assert.False(t, c1)
assert.False(t, c2)
c0, c1, c2 = false, false, false
rte1.HandleMiddleware(newTestContext(f))
assert.False(t, c0)
assert.True(t, c1)
assert.False(t, c2)
c0, c1, c2 = false, false, false
rte2 := f.Route(MethodGet, "/2")
require.NotNil(t, rte2)
rte2.HandleMiddleware(newTestContext(f))
assert.False(t, c0)
assert.False(t, c1)
assert.True(t, c2)
}