Skip to content

Commit

Permalink
support empty bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
aviau committed Sep 14, 2024
1 parent 32ba34c commit 6d9688f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions api_client_iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ func (client *ApiClient) IterPostJson(
return createPagingIterator(
func(cursor string) (*http.Response, error) {
if cursor != "" {
if body == nil {
body = make(map[string]interface{})
}
body["from"] = cursor
}

Expand Down
36 changes: 36 additions & 0 deletions api_client_iter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,39 @@ func TestIterPostJson(t *testing.T) {
)

}

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")
}

0 comments on commit 6d9688f

Please sign in to comment.