MCPcopy
hub / github.com/gofiber/fiber / TestErrorHandler_PicksRightOne

Function TestErrorHandler_PicksRightOne

app_test.go:3405–3474  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

3403}
3404
3405func TestErrorHandler_PicksRightOne(t *testing.T) {
3406 t.Parallel()
3407 // common handler to be used by all routes,
3408 // it will always fail by returning an error since
3409 // we need to test that the right ErrorHandler is invoked
3410 handler := func(_ Ctx) error {
3411 return errors.New("random error")
3412 }
3413
3414 // subapp /api/v1/users [no custom error handler]
3415 appAPIV1Users := New()
3416 appAPIV1Users.Get("/", handler)
3417
3418 // subapp /api/v1/use [with custom error handler]
3419 appAPIV1UseEH := func(c Ctx, _ error) error {
3420 return c.SendString("/api/v1/use error handler")
3421 }
3422 appAPIV1Use := New(Config{ErrorHandler: appAPIV1UseEH})
3423 appAPIV1Use.Get("/", handler)
3424
3425 // subapp: /api/v1 [with custom error handler]
3426 appV1EH := func(c Ctx, _ error) error {
3427 return c.SendString("/api/v1 error handler")
3428 }
3429 appV1 := New(Config{ErrorHandler: appV1EH})
3430 appV1.Get("/", handler)
3431 appV1.Use("/users", appAPIV1Users)
3432 appV1.Use("/use", appAPIV1Use)
3433
3434 // root app [no custom error handler]
3435 app := New()
3436 app.Get("/", handler)
3437 app.Use("/api/v1", appV1)
3438
3439 testCases := []struct {
3440 path string // the endpoint url to test
3441 expected string // the expected error response
3442 }{
3443 // /api/v1/users mount doesn't have custom ErrorHandler
3444 // so it should use the upper-nearest one (/api/v1)
3445 {"/api/v1/users", "/api/v1 error handler"},
3446
3447 // /api/v1/use mount has a custom ErrorHandler
3448 {"/api/v1/use", "/api/v1/use error handler"},
3449
3450 // /api/v1 mount has a custom ErrorHandler
3451 {"/api/v1", "/api/v1 error handler"},
3452
3453 // / mount doesn't have custom ErrorHandler, since is
3454 // the root path i will use Fiber's default Error Handler
3455 {"/", "random error"},
3456 }
3457
3458 for _, testCase := range testCases {
3459 t.Run(testCase.path, func(t *testing.T) {
3460 t.Parallel()
3461 resp, err := app.Test(httptest.NewRequest(MethodGet, testCase.path, http.NoBody))
3462 if err != nil {

Callers

nothing calls this directly

Calls 7

TestMethod · 0.80
NewFunction · 0.70
NewMethod · 0.65
GetMethod · 0.65
SendStringMethod · 0.65
UseMethod · 0.65
FatalMethod · 0.65

Tested by

no test coverage detected