(t *testing.T)
| 24 | ) |
| 25 | |
| 26 | func 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 |
nothing calls this directly
no test coverage detected