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

Function TestRouterPanicHandler

router_test.go:450–476  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

448}
449
450func TestRouterPanicHandler(t *testing.T) {
451 router := New()
452 panicHandled := false
453
454 router.PanicHandler = func(rw http.ResponseWriter, r *http.Request, p interface{}) {
455 panicHandled = true
456 }
457
458 router.Handle(http.MethodPut, "/user/:name", func(_ http.ResponseWriter, _ *http.Request, _ Params) {
459 panic("oops!")
460 })
461
462 w := new(mockResponseWriter)
463 req, _ := http.NewRequest(http.MethodPut, "/user/gopher", nil)
464
465 defer func() {
466 if rcv := recover(); rcv != nil {
467 t.Fatal("handling panic failed")
468 }
469 }()
470
471 router.ServeHTTP(w, req)
472
473 if !panicHandled {
474 t.Fatal("simulating failed")
475 }
476}
477
478func TestRouterLookup(t *testing.T) {
479 routed := false

Callers

nothing calls this directly

Calls 3

NewFunction · 0.85
HandleMethod · 0.80
ServeHTTPMethod · 0.45

Tested by

no test coverage detected