1
20 package pagination
21
22 import (
23 "fmt"
24 "net/http"
25 "net/url"
26 "testing"
27
28 "github.com/stretchr/testify/assert"
29 )
30
31 func TestParse(t *testing.T) {
32 for _, tc := range []struct {
33 d string
34 url string
35 dl int
36 do int
37 ml int
38 el int
39 eo int
40 }{
41 {"normal", "http://localhost/foo?limit=10&offset=10", 0, 0, 120, 10, 10},
42 {"defaults", "http://localhost/foo", 5, 5, 10, 5, 5},
43 {"defaults_and_limits", "http://localhost/foo", 5, 5, 2, 2, 5},
44 {"limits", "http://localhost/foo?limit=10&offset=10", 0, 0, 5, 5, 10},
45 {"negatives", "http://localhost/foo?limit=-1&offset=-1", 0, 0, 5, 0, 0},
46 {"default_negatives", "http://localhost/foo", -1, -1, 5, 0, 0},
47 {"invalid_defaults", "http://localhost/foo?limit=a&offset=b", 10, 10, 15, 10, 10},
48 } {
49 t.Run(fmt.Sprintf("case=%s", tc.d), func(t *testing.T) {
50 u, _ := url.Parse(tc.url)
51 limit, offset := Parse(&http.Request{URL: u}, tc.dl, tc.do, tc.ml)
52 assert.EqualValues(t, limit, tc.el)
53 assert.EqualValues(t, offset, tc.eo)
54 })
55 }
56 }
57
View as plain text