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

Function Test_Extractors

extractors/extractors_test.go:68–146  ·  view source on GitHub ↗

go test -run Test_Extractors

(t *testing.T)

Source from the content-addressed store, hash-verified

66
67// go test -run Test_Extractors
68func Test_Extractors(t *testing.T) {
69 t.Parallel()
70
71 t.Run("FromParam", func(t *testing.T) {
72 t.Parallel()
73
74 app := fiber.New()
75 app.Get("/test/:token", func(c fiber.Ctx) error {
76 token, err := FromParam("token").Extract(c)
77 require.NoError(t, err)
78 require.Equal(t, "token_from_param", token)
79 return nil
80 })
81 _, err := app.Test(newRequest("/test/token_from_param"))
82 require.NoError(t, err)
83 })
84
85 t.Run("FromForm", func(t *testing.T) {
86 t.Parallel()
87
88 app := fiber.New()
89 ctx := app.AcquireCtx(&fasthttp.RequestCtx{})
90 t.Cleanup(func() { app.ReleaseCtx(ctx) })
91 ctx.Request().Header.SetContentType(fiber.MIMEApplicationForm)
92 ctx.Request().Header.SetMethod(fiber.MethodPost)
93 ctx.Request().SetBodyString("token=token_from_form")
94 token, err := FromForm("token").Extract(ctx)
95 require.NoError(t, err)
96 require.Equal(t, "token_from_form", token)
97 })
98
99 t.Run("FromQuery", func(t *testing.T) {
100 t.Parallel()
101
102 app := fiber.New()
103 ctx := app.AcquireCtx(&fasthttp.RequestCtx{})
104 t.Cleanup(func() { app.ReleaseCtx(ctx) })
105 ctx.Request().SetRequestURI("/?token=token_from_query")
106 token, err := FromQuery("token").Extract(ctx)
107 require.NoError(t, err)
108 require.Equal(t, "token_from_query", token)
109 })
110
111 t.Run("FromHeader", func(t *testing.T) {
112 t.Parallel()
113
114 app := fiber.New()
115 ctx := app.AcquireCtx(&fasthttp.RequestCtx{})
116 t.Cleanup(func() { app.ReleaseCtx(ctx) })
117 ctx.Request().Header.Set("X-Token", "token_from_header")
118 token, err := FromHeader("X-Token").Extract(ctx)
119 require.NoError(t, err)
120 require.Equal(t, "token_from_header", token)
121 })
122
123 t.Run("FromAuthHeader", func(t *testing.T) {
124 t.Parallel()
125

Callers

nothing calls this directly

Calls 15

FromParamFunction · 0.85
newRequestFunction · 0.85
FromFormFunction · 0.85
FromQueryFunction · 0.85
FromHeaderFunction · 0.85
FromAuthHeaderFunction · 0.85
FromCookieFunction · 0.85
TestMethod · 0.80
AcquireCtxMethod · 0.80
ReleaseCtxMethod · 0.80
SetMethodMethod · 0.80
NewMethod · 0.65

Tested by

no test coverage detected