Add processing when pagination is nil
This commit is contained in:
parent
134128cb56
commit
22f47735b4
17
mastodon.go
17
mastodon.go
|
@ -103,19 +103,18 @@ func (c *Client) doAPI(ctx context.Context, method string, uri string, params in
|
|||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
lh := resp.Header.Get("Link")
|
||||
if lh != "" {
|
||||
retPG, err := newPagination(lh)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*pg = *retPG
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return parseAPIError("bad request", resp)
|
||||
} else if res == nil {
|
||||
return nil
|
||||
} else if pg != nil {
|
||||
if lh := resp.Header.Get("Link"); lh != "" {
|
||||
pg2, err := newPagination(lh)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*pg = *pg2
|
||||
}
|
||||
}
|
||||
return json.NewDecoder(resp.Body).Decode(&res)
|
||||
}
|
||||
|
|
|
@ -13,24 +13,24 @@ import (
|
|||
|
||||
func TestDoAPI(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
q := r.URL.Query()
|
||||
if q.Get("max_id") == "123" && q.Get("since_id") == "789" && q.Get("limit") == "10" {
|
||||
if r.URL.Query().Get("max_id") == "999" {
|
||||
w.Header().Set("Link", `<:>; rel="next"`)
|
||||
} else {
|
||||
w.Header().Set("Link", `<http://example.com?max_id=234>; rel="next", <http://example.com?since_id=890>; rel="prev"`)
|
||||
fmt.Fprintln(w, `[{"username": "foo"}, {"username": "bar"}]`)
|
||||
}
|
||||
w.Header().Set("Link", `<:>; rel="next"`)
|
||||
fmt.Fprintln(w, `[{"username": "foo"}, {"username": "bar"}]`)
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
c := NewClient(&Config{Server: ts.URL})
|
||||
err := c.doAPI(context.Background(), http.MethodGet, "/", nil, nil, &Pagination{
|
||||
var accounts []Account
|
||||
err := c.doAPI(context.Background(), http.MethodGet, "/", nil, &accounts, &Pagination{
|
||||
MaxID: Int64(999),
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatalf("should be fail: %v", err)
|
||||
}
|
||||
|
||||
var accounts []Account
|
||||
pg := &Pagination{
|
||||
MaxID: Int64(123),
|
||||
SinceID: Int64(789),
|
||||
|
@ -74,6 +74,18 @@ func TestDoAPI(t *testing.T) {
|
|||
if accounts[1].Username != "bar" {
|
||||
t.Fatalf("want %q but %q", "bar", accounts[1].Username)
|
||||
}
|
||||
|
||||
// *Pagination is nil
|
||||
err = c.doAPI(context.Background(), http.MethodGet, "/", nil, &accounts, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("should not be fail: %v", err)
|
||||
}
|
||||
if accounts[0].Username != "foo" {
|
||||
t.Fatalf("want %q but %q", "foo", accounts[0].Username)
|
||||
}
|
||||
if accounts[1].Username != "bar" {
|
||||
t.Fatalf("want %q but %q", "bar", accounts[1].Username)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthenticate(t *testing.T) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user