-
Notifications
You must be signed in to change notification settings - Fork 0
/
mux_test.go
70 lines (65 loc) · 1.44 KB
/
mux_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
package ich
import (
"net/http"
"net/url"
"testing"
)
func TestBuildPath(t *testing.T) {
r := New()
r.Get("/", http.NotFound).Name("home")
r.Get("/foo/{bar:[a-z-]+}/*", http.NotFound).Name("foo")
r.Route("/nested/{foo}", func(r *Mux) {
r.Get("/bar/{baz}", http.NotFound).Name("bar")
r.Group(func(r *Mux) {
r.HandleFunc("/any/method", http.NotFound).Name("any")
})
})
tests := []struct {
name string
args []any
path string
}{
{
"home",
[]any{},
"/",
},
{
"home",
[]any{"foo", "bar"},
"/?foo=bar",
},
{
"foo",
[]any{"bar", "value", "*", "wild/card"},
"/foo/value/wild/card",
},
{
"bar",
[]any{"baz", "value2", []string{"foo", "value1"}},
"/nested/value1/bar/value2",
},
{
"bar",
[]any{url.Values{"q": []string{"qvalue1", "qvalue2"}}, "baz", "value2", []string{"foo", "value1"}},
"/nested/value1/bar/value2?q=qvalue1&q=qvalue2",
},
}
for _, test := range tests {
u, err := r.BuildPath(test.name, test.args...)
if err != nil {
t.Error(err)
} else if u.String() != test.path {
t.Errorf("Expected path %q but got %q", test.path, u.String())
}
}
if r.RouteName("GET", "/foo/{bar:[a-z-]+}/*") != "foo" {
t.Errorf("Route name %q not found", "foo")
}
if r.RouteName("GET", "/nested/{foo}/bar/{baz}") != "bar" {
t.Errorf("Route name %q not found", "bar")
}
if r.RouteName("", "/nested/{foo}/any/method") != "any" {
t.Errorf("Route name %q not found", "any")
}
}