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

Function Test_PaginateWithQueries

middleware/paginate/paginate_test.go:332–372  ·  view source on GitHub ↗

--- Middleware handler tests ---

(t *testing.T)

Source from the content-addressed store, hash-verified

330// --- Middleware handler tests ---
331
332func Test_PaginateWithQueries(t *testing.T) {
333 t.Parallel()
334 app := fiber.New()
335
336 app.Use(New(Config{
337 DefaultSort: "id",
338 }))
339
340 app.Get("/", func(c fiber.Ctx) error {
341 pageInfo, ok := FromContext(c)
342 if !ok {
343 return fiber.ErrBadRequest
344 }
345
346 return c.JSON(paginateResponse{
347 Page: pageInfo.Page,
348 Limit: pageInfo.Limit,
349 Offset: pageInfo.Offset,
350 Start: pageInfo.Start(),
351 Sort: pageInfo.Sort,
352 NextPageURL: pageInfo.NextPageURL(c.BaseURL()),
353 PreviousPageURL: pageInfo.PreviousPageURL(c.BaseURL()),
354 })
355 })
356
357 resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/?page=2&limit=20", http.NoBody))
358 require.NoError(t, err)
359 defer resp.Body.Close() //nolint:errcheck // close error not relevant in tests
360 require.Equal(t, fiber.StatusOK, resp.StatusCode)
361
362 var body paginateResponse
363 require.NoError(t, json.NewDecoder(resp.Body).Decode(&body))
364
365 require.Equal(t, 2, body.Page)
366 require.Equal(t, 20, body.Limit)
367 require.Equal(t, 0, body.Offset)
368 require.Equal(t, 20, body.Start)
369 require.Equal(t, "http://example.com?limit=20&page=3", body.NextPageURL)
370 require.Equal(t, "http://example.com?limit=20&page=1", body.PreviousPageURL)
371 require.Equal(t, []SortField{{Field: "id", Order: ASC}}, body.Sort)
372}
373
374func Test_PaginateWithOffset(t *testing.T) {
375 t.Parallel()

Callers

nothing calls this directly

Calls 12

NextPageURLMethod · 0.80
PreviousPageURLMethod · 0.80
TestMethod · 0.80
NewFunction · 0.70
FromContextFunction · 0.70
NewMethod · 0.65
UseMethod · 0.65
GetMethod · 0.65
JSONMethod · 0.65
StartMethod · 0.65
BaseURLMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected