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
|
package growel
import (
"testing"
)
func dummy(c *Context) {}
func TestRouterStaticMatch(t *testing.T) {
r := NewRouter()
r.Add("GET", "/hello", dummy)
h, params := r.Find("GET", "/hello")
if h == nil {
t.Fatal("expected handler, got nil")
}
if len(params) != 0 {
t.Fatalf("expected no params, got %v", params)
}
}
func TestRouterStaticNoMatch(t *testing.T) {
r := NewRouter()
r.Add("GET", "/hello", dummy)
h, _ := r.Find("GET", "/nothello")
if h != nil {
t.Fatal("expected nil handler, got non-nil")
}
}
func TestRouterParamMatch(t *testing.T) {
r := NewRouter()
r.Add("GET", "/user/:id", dummy)
h, params := r.Find("GET", "/user/42")
if h == nil {
t.Fatal("expected handler, got nil")
}
if params["id"] != "42" {
t.Fatalf("expected id=42, got %v", params)
}
}
func TestRouterParamMismatch(t *testing.T) {
r := NewRouter()
r.Add("GET", "/user/:id", dummy)
h, _ := r.Find("GET", "/user")
if h != nil {
t.Fatal("expected nil handler, got non-nil")
}
}
|