-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi_client_iter_test.go
More file actions
211 lines (182 loc) · 5 KB
/
api_client_iter_test.go
File metadata and controls
211 lines (182 loc) · 5 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
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
//go:build go1.23
package flareio
import (
"encoding/json"
"net/http"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
)
func TestIterGet(t *testing.T) {
ct := newClientTest(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/leaksdb/sources", r.URL.Path)
cursor := r.URL.Query().Get("from")
if cursor == "" {
w.Write([]byte(`{"next":"second-page", "items": []}`))
} else if cursor == "second-page" {
w.Write([]byte(`{"next":"third-page", "items": []}`))
} else {
w.Write([]byte(`{"next": null, "items": []}`))
}
}),
)
defer ct.Close()
lastPageIndex := 0
nextTokens := []string{}
for result, err := range ct.apiClient.IterGet(
"/leaksdb/sources",
nil,
) {
lastPageIndex = lastPageIndex + 1
if lastPageIndex > 5 {
// We are going crazy here...
break
}
assert.Nil(t, err, "iter yielded an error")
assert.NotNil(t, result, "didn't get a result")
nextTokens = append(nextTokens, result.Next)
}
assert.Equal(t, 3, lastPageIndex, "Didn't get the expected number of pages")
assert.Equal(
t,
[]string{"second-page", "third-page", ""},
nextTokens,
"Didn't get the expected next tokens",
)
}
func TestIterGetBadResponseJson(t *testing.T) {
ct := newClientTest(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/leaksdb/sources", r.URL.Path)
w.Write([]byte(`{"next": 11, "items": []}`))
}),
)
defer ct.Close()
lastPageIndex := 0
for result, err := range ct.apiClient.IterGet(
"/leaksdb/sources",
nil,
) {
lastPageIndex = lastPageIndex + 1
if lastPageIndex > 2 {
// We are going crazy here...
break
}
assert.ErrorContains(t, err, "failed to unmarshal", "Bad next token should trigger an error")
assert.Nil(t, result, "bad response should not contain a result")
}
assert.Equal(t, 1, lastPageIndex, "Didn't get the expected number of pages")
}
func TestIterGetBadResponseStatus(t *testing.T) {
ct := newClientTest(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Correct response structure but bad response status code.
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`"some error"`))
}),
)
defer ct.Close()
lastPageIndex := 0
for result, err := range ct.apiClient.IterGet(
"/leaksdb/sources",
nil,
) {
lastPageIndex = lastPageIndex + 1
if lastPageIndex > 2 {
// We are going crazy here...
break
}
assert.ErrorContains(
t,
err,
`got http status code 400 status while fetching next page: "some error"`,
"Bad HTTP status should trigger an error",
)
assert.Nil(t, result, "result should be nil on errors")
}
assert.Equal(t, 1, lastPageIndex, "Didn't get the expected number of pages")
}
func TestIterPostJson(t *testing.T) {
ct := newClientTest(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/leaksdb/sources", r.URL.Path)
assert.Equal(t, "value1", r.URL.Query().Get("param1"))
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
type PagedRequest struct {
From string `json:"from"`
}
var pagedRequest PagedRequest
if err := json.NewDecoder(r.Body).Decode(&pagedRequest); !assert.NoError(t, err, "Error decoding posted JSON") {
return
}
cursor := pagedRequest.From
if cursor == "" {
w.Write([]byte(`{"next":"second-page", "items": []}`))
} else if cursor == "second-page" {
w.Write([]byte(`{"next":"third-page", "items": []}`))
} else {
w.Write([]byte(`{"next": null, "items": []}`))
}
}),
)
defer ct.Close()
lastPageIndex := 0
nextTokens := []string{}
for result, err := range ct.apiClient.IterPostJson(
"/leaksdb/sources",
&url.Values{
"param1": []string{"value1"},
},
map[string]interface{}{
"some_param": "hello",
},
) {
lastPageIndex = lastPageIndex + 1
if lastPageIndex > 5 {
// We are going crazy here...
break
}
assert.Nil(t, err, "iter yielded an error")
assert.NotNil(t, result, "didn't get a result")
nextTokens = append(nextTokens, result.Next)
}
assert.Equal(t, 3, lastPageIndex, "Didn't get the expected number of pages")
assert.Equal(
t,
[]string{"second-page", "third-page", ""},
nextTokens,
"Didn't get the expected next tokens",
)
}
func TestIterPostJsonNilMap(t *testing.T) {
requestsReceived := 0
ct := newClientTest(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestsReceived = requestsReceived + 1
if requestsReceived == 1 {
w.Write([]byte(`{"next": "second-page"}`))
} else {
w.Write([]byte(`{"next": null}`))
}
}),
)
defer ct.Close()
requestsSent := 0
nextTokens := []string{}
for result, err := range ct.apiClient.IterPostJson(
"/leaksdb/sources",
nil,
nil,
) {
requestsSent = requestsSent + 1
if requestsSent > 2 {
// We are going crazy here...
break
}
assert.Nil(t, err, "iter yielded an error")
assert.NotNil(t, result, "didn't get a result")
nextTokens = append(nextTokens, result.Next)
}
assert.Equal(t, 2, requestsSent, "Didn't get the expected number of pages")
}