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

Function Test_Session_NewWithStore

middleware/session/middleware_test.go:402–445  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

400}
401
402func Test_Session_NewWithStore(t *testing.T) {
403 t.Parallel()
404 app := fiber.New()
405
406 app.Use(New())
407
408 app.Get("/", func(c fiber.Ctx) error {
409 sess := FromContext(c)
410 id := sess.ID()
411 return c.SendString("value=" + id)
412 })
413 app.Post("/", func(c fiber.Ctx) error {
414 sess := FromContext(c)
415 id := sess.ID()
416 c.Cookie(&fiber.Cookie{
417 Name: "session_id",
418 Value: id,
419 })
420 return nil
421 })
422
423 h := app.Handler()
424
425 // Test GET request without cookie
426 ctx := &fasthttp.RequestCtx{}
427 ctx.Request.Header.SetMethod(fiber.MethodGet)
428 h(ctx)
429 require.Equal(t, fiber.StatusOK, ctx.Response.StatusCode())
430 // Get session cookie
431 token := string(ctx.Response.Header.Peek(fiber.HeaderSetCookie))
432 require.NotEmpty(t, token, "Expected Set-Cookie header to be present")
433 tokenParts := strings.SplitN(strings.SplitN(token, ";", 2)[0], "=", 2)
434 require.Len(t, tokenParts, 2, "Expected Set-Cookie header to contain a token")
435 token = tokenParts[1]
436 require.Equal(t, "value="+token, string(ctx.Response.Body()))
437
438 // Test GET request with cookie
439 ctx = &fasthttp.RequestCtx{}
440 ctx.Request.Header.SetMethod(fiber.MethodGet)
441 ctx.Request.Header.SetCookie("session_id", token)
442 h(ctx)
443 require.Equal(t, fiber.StatusOK, ctx.Response.StatusCode())
444 require.Equal(t, "value="+token, string(ctx.Response.Body()))
445}
446
447func Test_Session_FromSession(t *testing.T) {
448 t.Parallel()

Callers

nothing calls this directly

Calls 15

HandlerMethod · 0.80
SetMethodMethod · 0.80
NewFunction · 0.70
FromContextFunction · 0.70
NewMethod · 0.65
UseMethod · 0.65
GetMethod · 0.65
SendStringMethod · 0.65
PostMethod · 0.65
CookieMethod · 0.65
LenMethod · 0.65
BodyMethod · 0.65

Tested by

no test coverage detected