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

Function Test_PaginateEdgeCases

middleware/paginate/paginate_test.go:642–686  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

640}
641
642func Test_PaginateEdgeCases(t *testing.T) {
643 t.Parallel()
644
645 testCases := []struct {
646 name string
647 url string
648 expectedPage int
649 expectedLimit int
650 }{
651 {"Negative page", "/?page=-1", 1, 10},
652 {"Page zero", "/?page=0", 1, 10},
653 {"Negative limit", "/?limit=-10", 1, 10},
654 {"Limit zero", "/?limit=0", 1, 10},
655 {"Limit exceeds max", "/?limit=200", 1, DefaultMaxLimit},
656 }
657
658 for _, tc := range testCases {
659 t.Run(tc.name, func(t *testing.T) {
660 t.Parallel()
661 app := fiber.New()
662 app.Use(New(Config{
663 DefaultSort: "id",
664 DefaultLimit: 10,
665 }))
666
667 app.Get("/", func(c fiber.Ctx) error {
668 pageInfo, ok := FromContext(c)
669 if !ok {
670 return fiber.ErrBadRequest
671 }
672 return c.JSON(pageInfo)
673 })
674
675 resp, err := app.Test(httptest.NewRequest(http.MethodGet, tc.url, http.NoBody))
676 require.NoError(t, err)
677 defer resp.Body.Close() //nolint:errcheck // close error not relevant in tests
678 require.Equal(t, 200, resp.StatusCode)
679
680 var result PageInfo
681 require.NoError(t, json.NewDecoder(resp.Body).Decode(&result))
682 require.Equal(t, tc.expectedPage, result.Page)
683 require.Equal(t, tc.expectedLimit, result.Limit)
684 })
685 }
686}
687
688func Test_PaginateWithMultipleSorting(t *testing.T) {
689 t.Parallel()

Callers

nothing calls this directly

Calls 8

TestMethod · 0.80
NewFunction · 0.70
FromContextFunction · 0.70
NewMethod · 0.65
UseMethod · 0.65
GetMethod · 0.65
JSONMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected