-
Notifications
You must be signed in to change notification settings - Fork 0
/
router_test.go
76 lines (68 loc) · 1.6 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
package drouter
import (
"reflect"
"testing"
)
func TestRouterLookup(t *testing.T) {
routed := false
wantHandle := func() {
routed = true
}
wantParams := Params{Param{"name", "gopher"}}
router := New()
// try empty router first
params := make(Params, 0, 1)
handle, tsr := router.Lookup("/nope", ¶ms)
if handle != nil {
t.Fatalf("Got handle for unregistered pattern: %v", handle)
}
if tsr {
t.Error("Got wrong TSR recommendation!")
}
// insert route and try again
router.AddRoute("/user/:name", wantHandle)
params = make(Params, 0, 1)
handle, _ = router.Lookup("/user/gopher", ¶ms)
if handle == nil {
t.Fatal("Got no handle!")
} else {
handle.(func())()
if !routed {
t.Fatal("Routing failed!")
}
}
if !reflect.DeepEqual(params, wantParams) {
t.Fatalf("Wrong parameter values: want %v, got %v", wantParams, params)
}
routed = false
// route without param
router.AddRoute("/user", wantHandle)
params = nil
handle, _ = router.Lookup("/user", ¶ms)
if handle == nil {
t.Fatal("Got no handle!")
} else {
handle.(func())()
if !routed {
t.Fatal("Routing failed!")
}
}
if params != nil {
t.Fatalf("Wrong parameter values: want %v, got %v", nil, params)
}
params = make(Params, 0, 1)
handle, tsr = router.Lookup("/user/gopher/", ¶ms)
if handle != nil {
t.Fatalf("Got handle for unregistered pattern: %v", handle)
}
if !tsr {
t.Error("Got no TSR recommendation!")
}
handle, tsr = router.Lookup("/nope", ¶ms)
if handle != nil {
t.Fatalf("Got handle for unregistered pattern: %v", handle)
}
if tsr {
t.Error("Got wrong TSR recommendation!")
}
}