-
Notifications
You must be signed in to change notification settings - Fork 0
/
router_test.go
68 lines (57 loc) · 1.59 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
package gweb
import (
"fmt"
"reflect"
"testing"
)
func TestAddRoute(t *testing.T) {
r := newRouter()
r.AddRoute("GET", "/", nil)
r.AddRoute("GET", "/abc/ab", nil)
r.AddRoute("GET", "/c", nil)
r.AddRoute("GET", "/c/ab", nil)
r.AddRoute("GET", "/d/ab", nil)
r.AddRoute("GET", "/d", nil)
ok := reflect.DeepEqual(parsePattern("/p/:name"), []string{"/", "p", ":name"})
ok = ok && reflect.DeepEqual(parsePattern("/p/*"), []string{"/", "p", "*"})
//ok = ok && reflect.DeepEqual(parsePattern("/p/*name/*"), []string{"/", "p", "*name"})
//fmt.Println(parsePattern("/p/:name"))
//fmt.Println(parsePattern("/p/*"))
//fmt.Println(parsePattern("/p/name/*"))
if !ok {
t.Fatal("test parsePattern failed")
}
}
func TestMatchRoute(t *testing.T) {
r := newRouter()
r.AddRoute("GET", "/hello/:name", nil)
r.AddRoute("GET", "/hello/abc", nil)
r.AddRoute("GET", "/:age", nil)
r.AddRoute("GET", "/18", nil)
n, ps := r.Match("GET", "/hello/abc")
if n == nil {
t.Fatal("nil shouldn't be returned")
}
println(n.pattern)
fmt.Printf("%+v\n", ps)
if n.pattern != "/hello/:name" {
t.Fatal("should match /hello/:name")
}
if ps["name"] != "abc" {
t.Fatal("name should be equal to 'abc'")
}
fmt.Printf("matched path: %s, params['name']: %s\n", n.pattern, ps["name"])
r.AddRoute("GET", "/hello/*path", nil)
n, ps = r.Match("GET", "/hello/cb/dfb/df")
if n == nil {
t.Fatal("2: nil shouldn't be returned")
}
println(n.pattern)
fmt.Printf("%+v\n", ps)
if n.pattern != "/hello/*path" {
t.Fatal("should match /hello/*path")
}
if ps["path"] != "cb/dfb/df" {
t.Fatal("name should be equal to 'abc'")
}
}