-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpagination_test.go
More file actions
131 lines (117 loc) · 3.45 KB
/
pagination_test.go
File metadata and controls
131 lines (117 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package forge
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestPagination_Good_SinglePage(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Total-Count", "2")
json.NewEncoder(w).Encode([]map[string]int{{"id": 1}, {"id": 2}})
}))
defer srv.Close()
c := NewClient(srv.URL, "tok")
result, err := ListAll[map[string]int](context.Background(), c, "/api/v1/repos", nil)
if err != nil {
t.Fatal(err)
}
if len(result) != 2 {
t.Errorf("got %d items", len(result))
}
}
func TestPagination_Good_MultiPage(t *testing.T) {
page := 0
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
page++
w.Header().Set("X-Total-Count", "100")
items := make([]map[string]int, 50)
for i := range items {
items[i] = map[string]int{"id": (page-1)*50 + i + 1}
}
json.NewEncoder(w).Encode(items)
}))
defer srv.Close()
c := NewClient(srv.URL, "tok")
result, err := ListAll[map[string]int](context.Background(), c, "/api/v1/repos", nil)
if err != nil {
t.Fatal(err)
}
if len(result) != 100 {
t.Errorf("got %d items, want 100", len(result))
}
}
func TestPagination_Good_EmptyResult(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Total-Count", "0")
json.NewEncoder(w).Encode([]map[string]int{})
}))
defer srv.Close()
c := NewClient(srv.URL, "tok")
result, err := ListAll[map[string]int](context.Background(), c, "/api/v1/repos", nil)
if err != nil {
t.Fatal(err)
}
if len(result) != 0 {
t.Errorf("got %d items", len(result))
}
}
func TestPagination_Good_Iter(t *testing.T) {
page := 0
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
page++
w.Header().Set("X-Total-Count", "100")
items := make([]map[string]int, 50)
for i := range items {
items[i] = map[string]int{"id": (page-1)*50 + i + 1}
}
json.NewEncoder(w).Encode(items)
}))
defer srv.Close()
c := NewClient(srv.URL, "tok")
count := 0
for item, err := range ListIter[map[string]int](context.Background(), c, "/api/v1/repos", nil) {
if err != nil {
t.Fatal(err)
}
count++
if item["id"] != count {
t.Errorf("got id %d, want %d", item["id"], count)
}
}
if count != 100 {
t.Errorf("got %d items, want 100", count)
}
}
func TestListPage_Good_QueryParams(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
p := r.URL.Query().Get("page")
l := r.URL.Query().Get("limit")
s := r.URL.Query().Get("state")
if p != "2" || l != "25" || s != "open" {
t.Errorf("wrong params: page=%s limit=%s state=%s", p, l, s)
}
w.Header().Set("X-Total-Count", "50")
json.NewEncoder(w).Encode([]map[string]int{})
}))
defer srv.Close()
c := NewClient(srv.URL, "tok")
_, err := ListPage[map[string]int](context.Background(), c, "/api/v1/repos",
map[string]string{"state": "open"}, ListOptions{Page: 2, Limit: 25})
if err != nil {
t.Fatal(err)
}
}
func TestPagination_Bad_ServerError(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(500)
json.NewEncoder(w).Encode(map[string]string{"message": "fail"})
}))
defer srv.Close()
c := NewClient(srv.URL, "tok")
_, err := ListAll[map[string]int](context.Background(), c, "/api/v1/repos", nil)
if err == nil {
t.Fatal("expected error")
}
}