From 22f47735b41f5f763bc3b0b58c961a2c9dcad54e Mon Sep 17 00:00:00 2001 From: 178inaba <178inaba@users.noreply.github.com> Date: Sat, 6 May 2017 23:34:42 +0900 Subject: [PATCH] Add processing when pagination is nil --- mastodon.go | 17 ++++++++--------- mastodon_test.go | 24 ++++++++++++++++++------ 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/mastodon.go b/mastodon.go index 17acacc..1ac6029 100644 --- a/mastodon.go +++ b/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) } diff --git a/mastodon_test.go b/mastodon_test.go index ff1dd28..974a380 100644 --- a/mastodon_test.go +++ b/mastodon_test.go @@ -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", `; rel="next", ; 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) {