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

Function TestAdapter_MixedHandlerIntegration

adapter_test.go:487–562  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

485}
486
487func TestAdapter_MixedHandlerIntegration(t *testing.T) {
488 app := New()
489
490 app.Use(func(c Ctx) error {
491 c.Set("X-Middleware", "fiber")
492 return c.Next()
493 })
494
495 app.Use(func(_ Req, res Res, next func() error) error {
496 res.Set("X-Express", "middleware")
497 return next()
498 })
499
500 app.Get("/fiber", func(c Ctx) error {
501 c.Set("X-Route", "fiber")
502 return c.SendString("fiber handler")
503 })
504
505 app.Post("/express", func(_ Req, res Res) error {
506 res.Set("X-Route", "express")
507 return res.SendString("express handler")
508 })
509
510 var httpHandlerWriteErr error
511 app.Put("/http", func(w http.ResponseWriter, _ *http.Request) {
512 w.Header().Set("X-Route", "http")
513 w.WriteHeader(http.StatusAccepted)
514 _, httpHandlerWriteErr = w.Write([]byte("http handler"))
515 })
516
517 app.Delete("/fasthttp", func(ctx *fasthttp.RequestCtx) error {
518 ctx.Response.Header.Set("X-Route", "fasthttp")
519 ctx.SetStatusCode(http.StatusCreated)
520 ctx.SetBodyString("fasthttp handler")
521 return nil
522 })
523
524 run := func(name string, buildRequest func() *http.Request, expectStatus int, expectBody, expectRoute string) {
525 t.Run(name, func(t *testing.T) {
526 req := buildRequest()
527
528 resp, err := app.Test(req)
529 require.NoError(t, err)
530 t.Cleanup(func() {
531 require.NoError(t, resp.Body.Close())
532 })
533
534 body, err := io.ReadAll(resp.Body)
535 require.NoError(t, err)
536
537 require.Equal(t, expectStatus, resp.StatusCode)
538 require.Equal(t, expectBody, string(body))
539 require.Equal(t, "fiber", resp.Header.Get("X-Middleware"))
540 require.Equal(t, "middleware", resp.Header.Get("X-Express"))
541 require.Equal(t, expectRoute, resp.Header.Get("X-Route"))
542 })
543 }
544

Callers

nothing calls this directly

Calls 14

TestMethod · 0.80
NewFunction · 0.70
UseMethod · 0.65
SetMethod · 0.65
NextMethod · 0.65
GetMethod · 0.65
SendStringMethod · 0.65
PostMethod · 0.65
PutMethod · 0.65
WriteMethod · 0.65
DeleteMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected