(t *testing.T)
| 583 | } |
| 584 | |
| 585 | func Test_Session_Next(t *testing.T) { |
| 586 | t.Parallel() |
| 587 | |
| 588 | var ( |
| 589 | doNext bool |
| 590 | muNext sync.RWMutex |
| 591 | ) |
| 592 | |
| 593 | app := fiber.New() |
| 594 | |
| 595 | app.Use(New(Config{ |
| 596 | Next: func(_ fiber.Ctx) bool { |
| 597 | muNext.RLock() |
| 598 | defer muNext.RUnlock() |
| 599 | return doNext |
| 600 | }, |
| 601 | })) |
| 602 | |
| 603 | app.Get("/", func(c fiber.Ctx) error { |
| 604 | sess := FromContext(c) |
| 605 | if sess == nil { |
| 606 | return c.SendStatus(fiber.StatusInternalServerError) |
| 607 | } |
| 608 | id := sess.ID() |
| 609 | return c.SendString("value=" + id) |
| 610 | }) |
| 611 | |
| 612 | h := app.Handler() |
| 613 | |
| 614 | // Test with Next returning false |
| 615 | ctx := &fasthttp.RequestCtx{} |
| 616 | ctx.Request.Header.SetMethod(fiber.MethodGet) |
| 617 | h(ctx) |
| 618 | require.Equal(t, fiber.StatusOK, ctx.Response.StatusCode()) |
| 619 | // Get session cookie |
| 620 | token := string(ctx.Response.Header.Peek(fiber.HeaderSetCookie)) |
| 621 | require.NotEmpty(t, token, "Expected Set-Cookie header to be present") |
| 622 | tokenParts := strings.SplitN(strings.SplitN(token, ";", 2)[0], "=", 2) |
| 623 | require.Len(t, tokenParts, 2, "Expected Set-Cookie header to contain a token") |
| 624 | token = tokenParts[1] |
| 625 | require.Equal(t, "value="+token, string(ctx.Response.Body())) |
| 626 | |
| 627 | // Test with Next returning true |
| 628 | muNext.Lock() |
| 629 | doNext = true |
| 630 | muNext.Unlock() |
| 631 | |
| 632 | ctx = &fasthttp.RequestCtx{} |
| 633 | ctx.Request.Header.SetMethod(fiber.MethodGet) |
| 634 | h(ctx) |
| 635 | require.Equal(t, fiber.StatusInternalServerError, ctx.Response.StatusCode()) |
| 636 | } |
| 637 | |
| 638 | func Test_Session_Middleware_Store(t *testing.T) { |
| 639 | t.Parallel() |
nothing calls this directly
no test coverage detected