-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter_test.go
93 lines (65 loc) · 1.82 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
82
83
84
85
86
87
88
89
90
91
92
93
package track
import (
"net/http"
"net/http/httptest"
"testing"
)
var router *Router
func TestAdd(t *testing.T) {
if router != nil {
t.Errorf("expected %v got %v ", nil, router)
}
router = New()
v := []string{"hello", "world"}
r := httptest.NewRequest("GET", "/foo", nil)
w := httptest.NewRecorder()
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
//
})
testHandler(w, r)
router.add(v, "GET", testHandler)
// if n.Children["hello"].Children["world"].Value != {
// t.Errorf("expected %v got %v", "handler", n.Children["hello"].Children["world"].Value)
// }
v = []string{"api", "v1", "users", "posts"}
router.add(v, "GET", testHandler)
v = []string{"api2", "v2", ":id", "posts"}
router.add(v, "GET", testHandler)
}
func TestSearch(t *testing.T) {
v := []string{"hello", "world"}
result := router.search(v)
v = []string{"api", "v1", "users", "posts"}
result = router.search(v)
if result == nil {
t.Errorf("expected %v got %v ", nil, result)
}
v = []string{"api2", "v2", "12345", "post"}
result = router.search(v)
if result != nil {
t.Errorf("expected %v got %v ", nil, result)
}
v = []string{"api2", "v2", "12345", "posts"}
result = router.search(v)
if result == nil {
t.Errorf("expected %v got %v ", nil, result)
}
}
func BenchmarkMux(b *testing.B) {
router := New()
handler := func(w http.ResponseWriter, r *http.Request) {}
router.Get("/book/:id", handler)
request, _ := http.NewRequest("GET", "/book/anything", nil)
w := httptest.NewRecorder()
for i := 0; i < b.N; i++ {
router.ServeHTTP(w, request)
}
}
func BenchmarkAdd(b *testing.B) {
// run the Fib function b.N times
v := []string{"v1", ":id"}
handler := func(w http.ResponseWriter, r *http.Request) {}
for i := 0; i < b.N; i++ {
router.add(v, "GET", http.HandlerFunc(handler))
}
}