paginate_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // paginate_test.go — prueba de paginate ante páginas extremas (issue #201).
  2. package httpapi
  3. import (
  4. "math"
  5. "testing"
  6. )
  7. // TestPaginateHugePageNoPanic: una page enorme (p.ej. 9223372036854774784)
  8. // desbordaba (page-1)*pageSize y producía un índice negativo → panic
  9. // "slice bounds out of range" (issue #201). No debe paniquear.
  10. func TestPaginateHugePageNoPanic(t *testing.T) {
  11. items := make([]int, 10)
  12. for i := range items {
  13. items[i] = i
  14. }
  15. // El valor exacto que desbordaba int64 con pageSize 50 (coerceInt max).
  16. out := paginate(items, math.MaxInt64-134217729, 50)
  17. if out == nil {
  18. t.Fatal("paginate devolvió nil")
  19. }
  20. if items, ok := out["items"].([]int); ok {
  21. if len(items) != 0 {
  22. t.Errorf("esperaba 0 items (página fuera de rango), got %d", len(items))
  23. }
  24. } else {
  25. t.Errorf("items con tipo inesperado: %T", out["items"])
  26. }
  27. }
  28. // TestPaginatePrimeraPagina: la página 1 sigue devolviendo los items en orden.
  29. func TestPaginatePrimeraPagina(t *testing.T) {
  30. items := []string{"a", "b", "c", "d"}
  31. out := paginate(items, 1, 2)
  32. got := out["items"].([]string)
  33. if len(got) != 2 || got[0] != "a" || got[1] != "b" {
  34. t.Errorf("esperaba [a b], got %v", got)
  35. }
  36. if out["total"] != 4 || out["page"] != int64(1) {
  37. t.Errorf("meta incorrecta: %v", out)
  38. }
  39. }