-
Notifications
You must be signed in to change notification settings - Fork 301
/
links_test.go
259 lines (230 loc) · 5.44 KB
/
links_test.go
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package godo
import (
"encoding/json"
"testing"
)
var (
firstPageLinksJSONBlob = []byte(`{
"links": {
"pages": {
"last": "https://api.digitalocean.com/v2/droplets/?page=3",
"next": "https://api.digitalocean.com/v2/droplets/?page=2"
}
}
}`)
otherPageLinksJSONBlob = []byte(`{
"links": {
"pages": {
"first": "https://api.digitalocean.com/v2/droplets/?page=1",
"prev": "https://api.digitalocean.com/v2/droplets/?page=1",
"last": "https://api.digitalocean.com/v2/droplets/?page=3",
"next": "https://api.digitalocean.com/v2/droplets/?page=3"
}
}
}`)
lastPageLinksJSONBlob = []byte(`{
"links": {
"pages": {
"first": "https://api.digitalocean.com/v2/droplets/?page=1",
"prev": "https://api.digitalocean.com/v2/droplets/?page=2"
}
}
}`)
projectsLastPageLinksJSONBlob = []byte(`{
"links": {
"pages": {
"first": "https://api.digitalocean.com/v2/projects?page=1",
"prev": "https://api.digitalocean.com/v2/projects?page=2",
"last": "https://api.digitalocean.com/v2/projects?page=3"
}
}
}`)
missingLinksJSONBlob = []byte(`{ }`)
)
type godoList struct {
Links Links `json:"links"`
}
func loadLinksJSON(t *testing.T, j []byte) Links {
var list godoList
err := json.Unmarshal(j, &list)
if err != nil {
t.Fatal(err)
}
return list.Links
}
func TestLinks_ParseFirst(t *testing.T) {
links := loadLinksJSON(t, firstPageLinksJSONBlob)
_, err := links.CurrentPage()
if err != nil {
t.Fatal(err)
}
r := &Response{Links: &links}
checkCurrentPage(t, r, 1)
if links.IsLastPage() {
t.Fatalf("shouldn't be last page")
}
}
func TestLinks_ParseMiddle(t *testing.T) {
links := loadLinksJSON(t, otherPageLinksJSONBlob)
_, err := links.CurrentPage()
if err != nil {
t.Fatal(err)
}
r := &Response{Links: &links}
checkCurrentPage(t, r, 2)
if links.IsLastPage() {
t.Fatalf("shouldn't be last page")
}
}
func TestLinks_ParseLast(t *testing.T) {
links := loadLinksJSON(t, lastPageLinksJSONBlob)
_, err := links.CurrentPage()
if err != nil {
t.Fatal(err)
}
r := &Response{Links: &links}
checkCurrentPage(t, r, 3)
if !links.IsLastPage() {
t.Fatalf("expected last page")
}
}
func TestLinks_ParseProjectsLast(t *testing.T) {
links := loadLinksJSON(t, projectsLastPageLinksJSONBlob)
_, err := links.CurrentPage()
if err != nil {
t.Fatal(err)
}
r := &Response{Links: &links}
checkCurrentPage(t, r, 3)
if !links.IsLastPage() {
t.Fatalf("expected last page")
}
}
func TestLinks_ParseMissing(t *testing.T) {
links := loadLinksJSON(t, missingLinksJSONBlob)
_, err := links.CurrentPage()
if err != nil {
t.Fatal(err)
}
r := &Response{Links: &links}
checkCurrentPage(t, r, 1)
}
func TestLinks_ParseURL(t *testing.T) {
type linkTest struct {
name, url string
expectedPage int
expectedPageToken string
}
linkTests := []linkTest{
{
name: "prev",
url: "https://api.digitalocean.com/v2/droplets/?page=1",
expectedPage: 1,
},
{
name: "last",
url: "https://api.digitalocean.com/v2/droplets/?page=5",
expectedPage: 5,
},
{
name: "next",
url: "https://api.digitalocean.com/v2/droplets/?page=2",
expectedPage: 2,
},
{
name: "page token",
url: "https://api.digitalocean.com/v2/droplets/?page=2&page_token=aaa",
expectedPage: 2,
expectedPageToken: "aaa",
},
}
for _, lT := range linkTests {
p, err := pageForURL(lT.url)
if err != nil {
t.Fatal(err)
}
if p != lT.expectedPage {
t.Errorf("expected page for '%s' to be '%d', was '%d'",
lT.url, lT.expectedPage, p)
}
pageToken, err := pageTokenFromURL(lT.url)
if err != nil {
t.Fatal(err)
}
if pageToken != lT.expectedPageToken {
t.Errorf("expected pageToken for '%s' to be '%s', was '%s'",
lT.url, lT.expectedPageToken, pageToken)
}
}
}
func TestLinks_ParseEmptyString(t *testing.T) {
type linkTest struct {
name, url string
expected int
}
linkTests := []linkTest{
{
name: "none",
url: "http://example.com",
expected: 0,
},
{
name: "bad",
url: "no url",
expected: 0,
},
{
name: "empty",
url: "",
expected: 0,
},
}
for _, lT := range linkTests {
_, err := pageForURL(lT.url)
if err == nil {
t.Fatalf("expected error for test '%s', but received none", lT.name)
}
}
}
func TestLinks_NextPageToken(t *testing.T) {
t.Run("happy token", func(t *testing.T) {
checkNextPageToken(t, &Response{Links: &Links{
Pages: &Pages{
Next: "https://api.digitalocean.com/v2/droplets/?page_token=aaa",
},
}}, "aaa")
})
t.Run("empty token", func(t *testing.T) {
checkNextPageToken(t, &Response{Links: &Links{
Pages: &Pages{
Next: "https://api.digitalocean.com/v2/droplets/",
},
}}, "")
})
t.Run("no next page", func(t *testing.T) {
checkNextPageToken(t, &Response{Links: &Links{
Pages: &Pages{},
}}, "")
})
}
func TestLinks_ParseNextPageToken(t *testing.T) {
t.Run("happy token", func(t *testing.T) {
checkPreviousPageToken(t, &Response{Links: &Links{
Pages: &Pages{
Prev: "https://api.digitalocean.com/v2/droplets/?page_token=aaa",
},
}}, "aaa")
})
t.Run("empty token", func(t *testing.T) {
checkPreviousPageToken(t, &Response{Links: &Links{
Pages: &Pages{
Prev: "https://api.digitalocean.com/v2/droplets/",
},
}}, "")
})
t.Run("no next page", func(t *testing.T) {
checkPreviousPageToken(t, &Response{Links: &Links{
Pages: &Pages{},
}}, "")
})
}