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

Function Test_Session_Middleware

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

Source from the content-addressed store, hash-verified

24)
25
26func Test_Session_Middleware(t *testing.T) {
27 t.Parallel()
28 app := fiber.New()
29
30 app.Use(New())
31
32 app.Get("/get", func(c fiber.Ctx) error {
33 sess := FromContext(c)
34 if sess == nil {
35 return c.SendStatus(fiber.StatusInternalServerError)
36 }
37 value, ok := sess.Get("key").(string)
38 if !ok {
39 return c.Status(fiber.StatusNotFound).SendString("key not found")
40 }
41 return c.SendString("value=" + value)
42 })
43
44 app.Post("/set", func(c fiber.Ctx) error {
45 sess := FromContext(c)
46 if sess == nil {
47 return c.SendStatus(fiber.StatusInternalServerError)
48 }
49 // get a value from the body
50 value := c.FormValue("value")
51 sess.Set("key", value)
52 return c.SendStatus(fiber.StatusOK)
53 })
54
55 app.Post("/delete", func(c fiber.Ctx) error {
56 sess := FromContext(c)
57 if sess == nil {
58 return c.SendStatus(fiber.StatusInternalServerError)
59 }
60 sess.Delete("key")
61 return c.SendStatus(fiber.StatusOK)
62 })
63
64 app.Post("/reset", func(c fiber.Ctx) error {
65 sess := FromContext(c)
66 if sess == nil {
67 return c.SendStatus(fiber.StatusInternalServerError)
68 }
69
70 // Set a value to ensure it is cleared after reset
71 sess.Set("key", "value")
72
73 if err := sess.Reset(); err != nil {
74 return c.SendStatus(fiber.StatusInternalServerError)
75 }
76 // Ensure value is cleared
77 value, ok := sess.Get("key").(string)
78 if ok || value != "" {
79 return c.SendStatus(fiber.StatusInternalServerError)
80 }
81 return c.SendStatus(fiber.StatusOK)
82 })
83

Callers

nothing calls this directly

Calls 15

SetMethodMethod · 0.80
HandlerMethod · 0.80
ContainsMethod · 0.80
NewFunction · 0.70
FromContextFunction · 0.70
NewMethod · 0.65
UseMethod · 0.65
GetMethod · 0.65
SendStatusMethod · 0.65
SendStringMethod · 0.65
StatusMethod · 0.65
PostMethod · 0.65

Tested by

no test coverage detected