forked from digitalocean/godo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
links_test.go
199 lines (173 loc) · 3.82 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
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
expected int
}
linkTests := []linkTest{
{
name: "prev",
url: "https://api.digitalocean.com/v2/droplets/?page=1",
expected: 1,
},
{
name: "last",
url: "https://api.digitalocean.com/v2/droplets/?page=5",
expected: 5,
},
{
name: "nexta",
url: "https://api.digitalocean.com/v2/droplets/?page=2",
expected: 2,
},
}
for _, lT := range linkTests {
p, err := pageForURL(lT.url)
if err != nil {
t.Fatal(err)
}
if p != lT.expected {
t.Errorf("expected page for '%s' to be '%d', was '%d'",
lT.url, lT.expected, p)
}
}
}
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)
}
}
}