-
Notifications
You must be signed in to change notification settings - Fork 0
/
router_test.go
166 lines (156 loc) · 4.95 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
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
163
164
165
166
package heligo_test
import (
"context"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/sted/heligo"
)
type tParam struct {
name string
value string
}
func TestHandle(t *testing.T) {
tests := []struct {
handler string
url string
status bool
params []tParam
}{
{"/a", "/a", true, nil},
{"", "/b", false, nil},
{"/", "/", true, nil},
{"/app/:client_id/tokens", "/app/51/tokens", true, []tParam{{"client_id", "51"}}},
{"/app/:client_id/tokens/:access_token", "/app/51/tokens/ky89", true, []tParam{{"client_id", "51"}, {"access_token", "ky89"}}},
{"/bb/:gg/kk", "/bb/r/kk", true, []tParam{{"gg", "r"}}},
{"", "/bb/rhg/kk", true, []tParam{{"gg", "rhg"}}},
{"/a/b", "/a/b", true, nil},
{"/aa", "/aa", true, nil},
{"/ac", "/ac", true, nil},
{"/boo", "/boo", true, nil},
{"", "/aa", true, nil},
{"", "/a/b", true, nil},
{"", "/book", false, nil},
{"", "/boo/u", false, nil},
{"", "/boo", true, nil},
{"/contribute.html", "/contribute.html", true, nil},
{"/debugging_with_gdb.html", "/debugging_with_gdb.html", true, nil},
{"/docs.html", "/docs.html", true, nil},
{"/effective_go.html", "/effective_go.html", true, nil},
{"/files.log", "/files.log", true, nil},
{"/gccgo_contribute.html", "/gccgo_contribute.html", true, nil},
{"/gccgo_install.html", "/gccgo_install.html", true, nil},
{"/go-logo-black.png", "/go-logo-black.png", true, nil},
{"/go-logo-blue.png", "/go-logo-blue.png", true, nil},
{"/go-logo-white.png", "/go-logo-white.png", true, nil},
{"/go1.1.html", "/go1.1.html", true, nil},
{"/go1.2.html", "/go1.2.html", true, nil},
{"/go1.html", "/go1.html", true, nil},
{"/api/:test", "/api/test", true, []tParam{{"test", "test"}}},
{"", "/api/test/other", false, nil},
{"/api/:test/:n", "/api/test/n1", true, []tParam{{"test", "test"}, {"n", "n1"}}},
{"/api/t/*tt", "/api/t/1", true, []tParam{{"tt", "1"}}},
{"", "/api/t/muchmore", true, []tParam{{"tt", "muchmore"}}},
{"", "/api/t/muchmore/andmore", true, []tParam{{"tt", "muchmore/andmore"}}},
{"/api/*test", "/api/test/u/y", true, []tParam{{"test", "test/u/y"}}},
{"/a/*b", "/a/b/c", true, []tParam{{"b", "b/c"}}},
{"", "/contribute.html", true, nil},
{"", "/debugging_with_gdb.html", true, nil},
{"", "/docs.html", true, nil},
{"", "/effective_go.html", true, nil},
{"", "/files.log", true, nil},
{"", "/gccgo_contribute.html", true, nil},
{"", "/gccgo_install.html", true, nil},
{"", "/go-logo-black.png", true, nil},
{"", "/go-logo-blue.png", true, nil},
{"", "/go-logo-white.png", true, nil},
{"", "/go1.1.html", true, nil},
{"", "/go1.2.html", true, nil},
{"", "/go1.html", true, nil},
{"", "/", true, nil},
}
router := heligo.New()
for _, test := range tests {
if test.handler != "" {
router.Handle("GET", test.handler, func(ctx context.Context, w http.ResponseWriter, r heligo.Request) (int, error) {
if test.url != r.Request.URL.Path {
t.Fail()
}
params := r.Params()
if len(params) == len(test.params) {
for i := 0; i < len(params); i++ {
if params[i].Name != test.params[i].name ||
params[i].Value != test.params[i].value {
t.Fail()
}
}
} else {
t.Fail()
}
return 200, nil
})
}
w := httptest.NewRecorder()
r, _ := http.NewRequest("GET", test.url, nil)
router.ServeHTTP(w, r)
if test.status == (w.Code == http.StatusNotFound) {
t.Fail()
}
}
}
// helpre straight from go-chi/chi
func testRequest(t *testing.T, ts *httptest.Server, method, path string, body io.Reader) (*http.Response, string) {
req, err := http.NewRequest(method, ts.URL+path, body)
if err != nil {
t.Fatal(err)
return nil, ""
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
return nil, ""
}
respBody, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
return nil, ""
}
defer resp.Body.Close()
return resp, string(respBody)
}
func TestHead(t *testing.T) {
router := heligo.New()
router.Handle("GET", "/head", func(ctx context.Context, w http.ResponseWriter, r heligo.Request) (int, error) {
w.Header().Set("X-Test", "yes")
w.Write([]byte("test"))
return 200, nil
})
ts := httptest.NewServer(router)
defer ts.Close()
resp, body := testRequest(t, ts, "HEAD", "/head", nil)
if resp.StatusCode == http.StatusNotFound || string(body) != "" || resp.Header.Get("X-Test") != "yes" {
t.Fail()
}
resp, body = testRequest(t, ts, "GET", "/head", nil)
if resp.StatusCode == http.StatusNotFound || string(body) != "test" || resp.Header.Get("X-Test") != "yes" {
t.Fail()
}
}
func BenchmarkRouter(b *testing.B) {
ww := httptest.NewRecorder()
req_base, err := http.NewRequest("GET", "/base/test", nil)
if err != nil {
b.Fatal(err)
}
router := heligo.New()
router.Handle("GET", "/base/:test", func(ctx context.Context, _ http.ResponseWriter, r heligo.Request) (int, error) {
r.Param("test")
return 200, nil
})
b.Run("Heligo base", func(b *testing.B) {
for i := 0; i < b.N; i++ {
router.ServeHTTP(ww, req_base)
}
})
}