-
Notifications
You must be signed in to change notification settings - Fork 1
/
router_test.go
81 lines (73 loc) · 1.57 KB
/
router_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
package attache
import (
"bytes"
"net/http"
"reflect"
"testing"
)
func TestRouter(t *testing.T) {
cases := []struct {
method, path string
wantErr sentinelError
}{
{"GET", "/", ""},
{"GET", "/a", ""},
{"GET", "/a/b", ""},
{"POST", "/a/b", ""},
{"PUT", "/test", ""},
{"PUT", "/tent", ""},
{"GET", "/a/b", errRouteExists},
{"GET", "/a/b/c/", ""},
{"", "/web", ""},
{"", "/web", errRouteExists},
{"GET", "/we", ""},
{"GET", "/web/bad", errRouteOverMount},
{"GET", "/x/y/z", ""},
{"", "/x/y", errMountOnKnownPath},
}
r := &router{
&node{
prefix: "/",
methods: map[string]stack{},
kids: map[byte]*node{},
},
}
for _, c := range cases {
var err error
if c.method == "" {
c.path = canonicalize(c.path, true)
err = r.mount(c.path, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
} else {
c.path = canonicalize(c.path, false)
err = r.handle(
c.method,
c.path,
stack{
reflect.ValueOf(
func(w http.ResponseWriter, r *http.Request) {},
),
},
)
}
if c.wantErr != "" {
if err == nil {
t.Errorf("%s: wanted error %q, got none", c.path, c.wantErr)
continue
}
if err.Error() != c.wantErr.Error() {
t.Errorf("%s: wanted error %q, got %q", c.path, c.wantErr, err)
}
} else {
if err != nil {
t.Errorf("%s: unexpected error %q", c.path, err)
continue
}
if r.root.lookup(c.path) == nil {
t.Errorf("%s: did not find newly inserted node", c.path)
}
}
}
var buf bytes.Buffer
dump(r.root, "", 0, &buf)
t.Logf("\n%s", buf.String())
}