From e025766623385b03c78283f223e36bce6e619ecf Mon Sep 17 00:00:00 2001 From: mo1ein Date: Sat, 17 Sep 2022 18:56:16 +0430 Subject: [PATCH 1/4] Fix unhandled error --- github/repos_contents.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/github/repos_contents.go b/github/repos_contents.go index 431d4b12dc2..afe978810a3 100644 --- a/github/repos_contents.go +++ b/github/repos_contents.go @@ -317,5 +317,8 @@ func (s *RepositoriesService) GetArchiveLink(ctx context.Context, owner, repo st } parsedURL, err := url.Parse(resp.Header.Get("Location")) - return parsedURL, newResponse(resp), err + if err != nil { + return nil, newResponse(resp), err + } + return parsedURL, newResponse(resp), nil } From aa57a6370d2d93510be53a4ac550915011594e52 Mon Sep 17 00:00:00 2001 From: mo1ein Date: Sat, 17 Sep 2022 18:57:03 +0430 Subject: [PATCH 2/4] Add unit test --- github/repos_contents_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/github/repos_contents_test.go b/github/repos_contents_test.go index e0465e93d03..5115fe6ee94 100644 --- a/github/repos_contents_test.go +++ b/github/repos_contents_test.go @@ -744,6 +744,23 @@ func TestRepositoriesService_GetArchiveLink_StatusMovedPermanently_followRedirec } } +func TestRepositoriesService_GetArchiveLink_invalidLocationHeader(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/tarball", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + ctlChar := 0x7f + badUrl := "https://google.com" + string(byte(ctlChar)) + w.Header().Add("Location", badUrl) + w.WriteHeader(http.StatusFound) + }) + + ctx := context.Background() + _, _, err := client.Repositories.GetArchiveLink(ctx, "o", "r", Tarball, &RepositoryContentGetOptions{}, false) + testURLParseError(t, err) +} + func TestRepositoriesService_GetContents_NoTrailingSlashInDirectoryApiPath(t *testing.T) { client, mux, _, teardown := setup() defer teardown() From 29db345be17e4b36c16587c63cd2033febbec1fd Mon Sep 17 00:00:00 2001 From: mo1ein Date: Sat, 17 Sep 2022 19:06:26 +0430 Subject: [PATCH 3/4] Fix variable name --- github/repos_contents_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/github/repos_contents_test.go b/github/repos_contents_test.go index 5115fe6ee94..1cfa100df31 100644 --- a/github/repos_contents_test.go +++ b/github/repos_contents_test.go @@ -751,8 +751,8 @@ func TestRepositoriesService_GetArchiveLink_invalidLocationHeader(t *testing.T) mux.HandleFunc("/repos/o/r/tarball", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "GET") ctlChar := 0x7f - badUrl := "https://google.com" + string(byte(ctlChar)) - w.Header().Add("Location", badUrl) + badURL := "https://google.com" + string(byte(ctlChar)) + w.Header().Add("Location", badURL) w.WriteHeader(http.StatusFound) }) From 132029f3bbc19e21709867fb29cfad0883ba90ed Mon Sep 17 00:00:00 2001 From: mo1ein Date: Sat, 17 Sep 2022 21:14:06 +0430 Subject: [PATCH 4/4] Add blank line --- github/repos_contents.go | 1 + 1 file changed, 1 insertion(+) diff --git a/github/repos_contents.go b/github/repos_contents.go index afe978810a3..be58fd52f66 100644 --- a/github/repos_contents.go +++ b/github/repos_contents.go @@ -320,5 +320,6 @@ func (s *RepositoriesService) GetArchiveLink(ctx context.Context, owner, repo st if err != nil { return nil, newResponse(resp), err } + return parsedURL, newResponse(resp), nil }