-
Notifications
You must be signed in to change notification settings - Fork 1
/
route_test.go
162 lines (154 loc) · 4.64 KB
/
route_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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package trout
import (
"encoding/base64"
"math/rand"
"net/http"
"net/http/httptest"
"testing"
"time"
)
type testHandler string
func (t testHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte(t))
if err != nil {
panic(err)
}
}
func TestRouting(t *testing.T) {
type testCase struct {
url, method, handler, pattern string
}
cases := []testCase{
{"/v1", "GET", "get-static", "/v1"},
{"/v1/", "GET", "get-static", "/v1"},
{"/hello", "GET", "get-dynamic", "/{id}"},
{"/hello/", "GET", "get-dynamic", "/{id}"},
{"/v1", "POST", "post-dynamic", "/{id}"},
{"/v1/", "POST", "post-dynamic", "/{id}"},
{"/", "GET", "get-root", ""},
{"/hello/world", "DELETE", "catch-all", "/hello/world"},
{"/ancestor/one", "GET", "ancestor-one", "/ancestor/one"},
{"/ancestor/two", "GET", "ancestor-two", "/ancestor/two"},
{"/prefix/foo/bar", "GET", "get-prefix", "/prefix/{id::prefix}"},
{"/prefix/foo", "GET", "get-prefix", "/prefix/{id::prefix}"},
{"/prefix/static", "GET", "static-prefix", "/prefix/static::prefix"},
{"/prefix/static/bar/baz", "GET", "static-prefix", "/prefix/static::prefix"},
{"/prefix", "GET", "get-dynamic", "/{id}"},
}
var router Router
router.Handle404 = testHandler("404")
router.Handle405 = testHandler("405")
router.Prefix("/prefix/static").Methods("GET").Handler(testHandler("static-prefix"))
router.Prefix("/prefix/{id}").Methods("GET").Handler(testHandler("get-prefix"))
router.Endpoint("/{id}").Methods("GET").Handler(testHandler("get-dynamic"))
router.Endpoint("/v1").Methods("GET").Handler(testHandler("get-static"))
router.Endpoint("/{id}").Methods("POST").Handler(testHandler("post-dynamic"))
router.Endpoint("/").Methods("GET").Handler(testHandler("get-root"))
router.Endpoint("/ancestor/one").Methods("GET").Handler(testHandler("ancestor-one"))
router.Endpoint("/ancestor/two").Methods("GET").Handler(testHandler("ancestor-two"))
router.Endpoint("/hello/world").Handler(testHandler("catch-all"))
for _, c := range cases {
r, err := http.NewRequest(c.method, c.url, nil)
if err != nil {
t.Fatalf("Error creating request for %s %s: %+v", c.method, c.url, err)
}
h := router.getHandler(r)
res := string(h.(testHandler))
if res != c.handler {
t.Errorf("Expected to route \"%s %s\" to %s, routed to %s", c.method, c.url, c.handler, res)
}
if r.Header.Get("Trout-Pattern") != c.pattern {
t.Errorf("Expected \"%s %s\" to have a pattern of %q, got %q", c.method, c.url, c.pattern, r.Header.Get("Trout-Pattern"))
}
}
}
func TestKeysFromString(t *testing.T) {
cases := map[string][]key{
"/{id}/": []key{
{value: "id", dynamic: true},
},
"/v1": []key{
{value: "v1"},
},
"/": []key{
{value: ""},
},
"/ancestor/one": []key{
{value: "ancestor"},
{value: "one"},
},
"/ancestor/two": []key{
{value: "ancestor"},
{value: "two"},
},
}
for in, expect := range cases {
t.Logf("Testing case %s", in)
result := keysFromString(in)
if len(result) != len(expect) {
t.Errorf("Expected %d results, got %d: %+v", len(expect), len(result), result)
continue
}
for pos, res := range result {
if !res.equals(expect[pos]) {
t.Errorf("Expected result %d to be %+v, was %+v", pos, expect[pos], res)
}
}
}
}
var benchRouter Router
var benchTests []string
var benchMethods = [...]string{"GET", "POST", "PUT", "DELETE"}
func init() {
for i := 0; i < 100; i++ {
rand.Seed(time.Now().UnixNano())
depth := rand.Intn(4) + 1
var route string
var req string
for x := 0; x < depth; x++ {
rand.Seed(time.Now().UnixNano())
param := rand.Intn(1) == 1
pieceLength := rand.Intn(24) + 1
piece := make([]byte, pieceLength)
rand.Read(piece)
pieceStr := base64.URLEncoding.EncodeToString(piece)
req = req + "/" + pieceStr
if param {
pieceStr = "{" + pieceStr + "}"
}
route = route + "/" + pieceStr
}
benchTests = append(benchTests, req)
endpoint := benchRouter.Endpoint(route)
rand.Seed(time.Now().UnixNano())
get := rand.Intn(1) == 1
rand.Seed(time.Now().UnixNano())
post := rand.Intn(1) == 1
catchAll := !get && !post
var methods []string
if get {
methods = append(methods, "GET")
}
if post {
methods = append(methods, "POST")
}
if catchAll {
methods = append(methods, catchAllMethod)
}
endpoint.Methods(methods...).Handler(testHandler("benchmark"))
}
}
func BenchmarkRouting(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
w := httptest.NewRecorder()
route := benchTests[i%len(benchTests)]
method := benchMethods[i%len(benchMethods)]
req, err := http.NewRequest(method, route, nil)
if err != nil {
b.Fatalf(err.Error())
}
b.StartTimer()
benchRouter.ServeHTTP(w, req)
}
}