From 5c1757545ea9973c8b31497de72b0fc4e106ad57 Mon Sep 17 00:00:00 2001 From: srivignessh Date: Thu, 11 Jul 2019 00:18:29 -0700 Subject: [PATCH] add options to list gist forks endpoint Added Test cases to increase coverage --- github/gists.go | 7 ++++- github/gists_test.go | 65 ++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 66 insertions(+), 6 deletions(-) diff --git a/github/gists.go b/github/gists.go index 15e0bc2cd9d..36d9361967c 100644 --- a/github/gists.go +++ b/github/gists.go @@ -341,8 +341,13 @@ func (s *GistsService) Fork(ctx context.Context, id string) (*Gist, *Response, e // ListForks lists forks of a gist. // // GitHub API docs: https://developer.github.com/v3/gists/#list-gist-forks -func (s *GistsService) ListForks(ctx context.Context, id string) ([]*GistFork, *Response, error) { +func (s *GistsService) ListForks(ctx context.Context, id string, opt *ListOptions) ([]*GistFork, *Response, error) { u := fmt.Sprintf("gists/%v/forks", id) + u, err := addOptions(u, opt) + if err != nil { + return nil, nil, err + } + req, err := s.client.NewRequest("GET", u, nil) if err != nil { return nil, nil, err diff --git a/github/gists_test.go b/github/gists_test.go index 2d79b8c5e8b..d24e0e40b83 100644 --- a/github/gists_test.go +++ b/github/gists_test.go @@ -568,6 +568,14 @@ func TestGistsService_ListCommits_withOptions(t *testing.T) { } } +func TestGistsService_ListCommits_invalidID(t *testing.T) { + client, _, _, teardown := setup() + defer teardown() + + _, _, err := client.Gists.ListCommits(context.Background(), "%", nil) + testURLParseError(t, err) +} + func TestGistsService_Delete(t *testing.T) { client, mux, _, teardown := setup() defer teardown() @@ -717,7 +725,7 @@ func TestGistsService_ListForks(t *testing.T) { `) }) - gistForks, _, err := client.Gists.ListForks(context.Background(), "1") + gistForks, _, err := client.Gists.ListForks(context.Background(), "1", nil) if err != nil { t.Errorf("Gists.ListForks returned error: %v", err) } @@ -732,12 +740,59 @@ func TestGistsService_ListForks(t *testing.T) { if !reflect.DeepEqual(gistForks, want) { t.Errorf("Gists.ListForks returned %+v, want %+v", gistForks, want) } + } -func TestGistsService_Fork_invalidID(t *testing.T) { - client, _, _, teardown := setup() +func TestGistsService_ListForks_withOptions(t *testing.T) { + client, mux, _, teardown := setup() defer teardown() - _, _, err := client.Gists.Fork(context.Background(), "%") - testURLParseError(t, err) + mux.HandleFunc("/gists/1/forks", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testFormValues(t, r, values{ + "page": "2", + }) + fmt.Fprint(w, `[]`) + }) + + gistForks, _, err := client.Gists.ListForks(context.Background(), "1", &ListOptions{Page: 2}) + if err != nil { + t.Errorf("Gists.ListForks returned error: %v", err) + } + + want := []*GistFork{} + if !reflect.DeepEqual(gistForks, want) { + t.Errorf("Gists.ListForks returned %+v, want %+v", gistForks, want) + } + + // Test addOptions failure + _, _, err = client.Gists.ListForks(context.Background(), "%", &ListOptions{}) + if err == nil { + t.Error("Gists.ListForks returned err = nil") + } + + // Test client.NewRequest failure + got, resp, err := client.Gists.ListForks(context.Background(), "%", nil) + if got != nil { + t.Errorf("Gists.ListForks = %#v, want nil", got) + } + if resp != nil { + t.Errorf("Gists.ListForks resp = %#v, want nil", resp) + } + if err == nil { + t.Error("Gists.ListForks err = nil, want error") + } + + // Test client.Do failure + client.rateLimits[0].Reset.Time = time.Now().Add(10 * time.Minute) + got, resp, err = client.Gists.ListForks(context.Background(), "1", &ListOptions{Page: 2}) + if got != nil { + t.Errorf("Gists.ListForks returned = %#v, want nil", got) + } + if want := http.StatusForbidden; resp == nil || resp.Response.StatusCode != want { + t.Errorf("Gists.ListForks returned resp = %#v, want StatusCode=%v", resp.Response, want) + } + if err == nil { + t.Error("rGists.ListForks returned err = nil, want error") + } }