MCPcopy
hub / github.com/julienschmidt/httprouter / TestRouterChaining

Function TestRouterChaining

router_test.go:177–217  ·  router_test.go::TestRouterChaining
(t *testing.T)

Source from the content-addressed store, hash-verified

175}
176
177func TestRouterChaining(t *testing.T) {
178 router1 := New()
179 router2 := New()
180 router1.NotFound = router2
181
182 fooHit := false
183 router1.POST("/foo", func(w http.ResponseWriter, req *http.Request, _ Params) {
184 fooHit = true
185 w.WriteHeader(http.StatusOK)
186 })
187
188 barHit := false
189 router2.POST("/bar", func(w http.ResponseWriter, req *http.Request, _ Params) {
190 barHit = true
191 w.WriteHeader(http.StatusOK)
192 })
193
194 r, _ := http.NewRequest(http.MethodPost, "/foo", nil)
195 w := httptest.NewRecorder()
196 router1.ServeHTTP(w, r)
197 if !(w.Code == http.StatusOK && fooHit) {
198 t.Errorf("Regular routing failed with router chaining.")
199 t.FailNow()
200 }
201
202 r, _ = http.NewRequest(http.MethodPost, "/bar", nil)
203 w = httptest.NewRecorder()
204 router1.ServeHTTP(w, r)
205 if !(w.Code == http.StatusOK && barHit) {
206 t.Errorf("Chained routing failed with router chaining.")
207 t.FailNow()
208 }
209
210 r, _ = http.NewRequest(http.MethodPost, "/qax", nil)
211 w = httptest.NewRecorder()
212 router1.ServeHTTP(w, r)
213 if !(w.Code == http.StatusNotFound) {
214 t.Errorf("NotFound behavior failed with router chaining.")
215 t.FailNow()
216 }
217}
218
219func BenchmarkAllowed(b *testing.B) {
220 handlerFunc := func(_ http.ResponseWriter, _ *http.Request, _ Params) {}

Callers

nothing calls this directly

Calls 4

NewFunction · 0.85
POSTMethod · 0.80
WriteHeaderMethod · 0.80
ServeHTTPMethod · 0.45

Tested by

no test coverage detected