-
Notifications
You must be signed in to change notification settings - Fork 0
/
methodr_test.go
207 lines (187 loc) · 5.56 KB
/
methodr_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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package methodr
import (
"net/http"
"net/http/httptest"
"testing"
)
// Status codes depending on method, for tests only
const (
statusGET = iota + 200
statusHEAD
statusPOST
statusPUT
statusDELETE
statusTRACE
statusOPTIONS
statusCONNECT
statusPATCH
)
// Success handler, writes status code depending on request method
var succHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
status := 0
switch r.Method {
case methodGET:
status = statusGET
case methodHEAD:
status = statusHEAD
case methodPOST:
status = statusPOST
case methodPUT:
status = statusPUT
case methodDELETE:
status = statusDELETE
case methodTRACE:
status = statusTRACE
case methodOPTIONS:
status = statusOPTIONS
case methodCONNECT:
status = statusCONNECT
case methodPATCH:
status = statusPATCH
}
w.WriteHeader(status)
})
// Handler returning 404
var deadHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
})
type routeTest struct {
reqMethod string
handler http.Handler
code int
}
var routeTests = []routeTest{
// Chain constructors
{"GET", GET(succHandler), statusGET},
{"HEAD", HEAD(succHandler), statusHEAD},
{"POST", POST(succHandler), statusPOST},
{"PUT", PUT(succHandler), statusPUT},
{"DELETE", DELETE(succHandler), statusDELETE},
{"TRACE", TRACE(succHandler), statusTRACE},
{"OPTIONS", OPTIONS(succHandler), statusOPTIONS},
{"CONNECT", CONNECT(succHandler), statusCONNECT},
{"PATCH", PATCH(succHandler), statusPATCH},
// Chained - Call chained method
{"GET", POST(deadHandler).GET(succHandler), statusGET},
{"HEAD", GET(deadHandler).HEAD(succHandler), statusHEAD},
{"POST", GET(deadHandler).POST(succHandler), statusPOST},
{"PUT", GET(deadHandler).PUT(succHandler), statusPUT},
{"DELETE", GET(deadHandler).DELETE(succHandler), statusDELETE},
{"TRACE", GET(deadHandler).TRACE(succHandler), statusTRACE},
{"OPTIONS", GET(deadHandler).OPTIONS(succHandler), statusOPTIONS},
{"CONNECT", GET(deadHandler).CONNECT(succHandler), statusCONNECT},
{"PATCH", GET(deadHandler).PATCH(succHandler), statusPATCH},
//Miss -> Defaulthandler
{"GET", POST(succHandler), defaultHandlerStatusCode},
// Special: HEAD uses GET if no HEAD handler is set and GET is available
{"HEAD", GET(succHandler), statusHEAD},
{"HEAD", POST(succHandler), defaultHandlerStatusCode},
{"POST", GET(succHandler), defaultHandlerStatusCode},
{"PUT", GET(succHandler), defaultHandlerStatusCode},
{"DELETE", GET(succHandler), defaultHandlerStatusCode},
{"TRACE", GET(succHandler), defaultHandlerStatusCode},
{"OPTIONS", GET(succHandler), defaultHandlerStatusCode},
{"CONNECT", GET(succHandler), defaultHandlerStatusCode},
{"PATCH", GET(succHandler), defaultHandlerStatusCode},
//Custom default handler
{"GET", DEFAULT(succHandler), statusGET},
{"POST", GET(deadHandler).DEFAULT(succHandler), statusPOST},
{"POST", DEFAULT(succHandler).GET(deadHandler), statusPOST},
//Unknown method
{"UNKNOWN", DEFAULT(succHandler), 0},
//Default handlers
{"GET", DefaultHandlerMethodNotAllowed, http.StatusMethodNotAllowed},
}
// Test routeTest table
func TestRouting(t *testing.T) {
for _, rt := range routeTests {
resp := httptest.NewRecorder()
req, err := http.NewRequest(rt.reqMethod, "", nil)
if err != nil {
t.Fatal(err)
}
rt.handler.ServeHTTP(resp, req)
if resp.Code != rt.code {
t.Errorf("Wrong statuscode, expected %d got %d", rt.code, resp.Code)
}
}
}
// Benchmark stdhandler without routing
func BenchmarkNoRoutingReference(b *testing.B) {
resp := httptest.NewRecorder()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
req, err := http.NewRequest("GET", "", nil)
if err != nil {
b.Fatal(err)
}
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
handler.ServeHTTP(resp, req)
}
}
// Benchmark routing with Hit in routing table
func BenchmarkRoutingHit(b *testing.B) {
resp := httptest.NewRecorder()
endhandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
req, err := http.NewRequest("GET", "", nil)
if err != nil {
b.Fatal(err)
}
handler := GET(endhandler)
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
handler.ServeHTTP(resp, req)
}
}
// Benchmark routing with Miss in routing table -> Default handler
func BenchmarkRoutingMissToDefault(b *testing.B) {
resp := httptest.NewRecorder()
endhandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
req, err := http.NewRequest("POST", "", nil)
if err != nil {
b.Fatal(err)
}
handler := GET(endhandler)
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
handler.ServeHTTP(resp, req)
}
}
// Benchmark routing with Miss in routing table -> Custom default handler
func BenchmarkRoutingMissToCustom(b *testing.B) {
resp := httptest.NewRecorder()
endhandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
req, err := http.NewRequest("POST", "", nil)
if err != nil {
b.Fatal(err)
}
handler := GET(endhandler).DEFAULT(endhandler)
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
handler.ServeHTTP(resp, req)
}
}
// func BenchmarkMissCustom(b *testing.B) {
// resp := httptest.NewRecorder()
// endhandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// w.WriteHeader(http.StatusOK)
// })
// req, err := http.NewRequest("POST", "", nil)
// handler := GET(endhandler)
// b.ResetTimer()
// for n := 0; n < b.N; n++ {
// handler.ServeHttp(resp, req)
// }
// }