Skip to content

Commit 75ddd50

Browse files
andersjanmyrgmlewis
authored andcommitted
Allow DownloadReleaseAsset to follow redirects (#1381)
Fixes #1378.
1 parent 200fc50 commit 75ddd50

3 files changed

Lines changed: 79 additions & 4 deletions

File tree

‎github/repos_releases.go‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,13 @@ func (s *RepositoriesService) GetReleaseAsset(ctx context.Context, owner, repo s
266266
// If a redirect is returned, the redirect URL will be returned as a string instead
267267
// of the io.ReadCloser. Exactly one of rc and redirectURL will be zero.
268268
//
269+
// followRedirectsClient can be passed to download the asset from a redirected
270+
// location. Passing http.DefaultClient is recommended unless special circumstances
271+
// exist, but it's possible to pass any http.Client. If nil is passed the
272+
// redirectURL will be returned instead.
273+
//
269274
// GitHub API docs: https://developer.github.com/v3/repos/releases/#get-a-single-release-asset
270-
func (s *RepositoriesService) DownloadReleaseAsset(ctx context.Context, owner, repo string, id int64) (rc io.ReadCloser, redirectURL string, err error) {
275+
func (s *RepositoriesService) DownloadReleaseAsset(ctx context.Context, owner, repo string, id int64, followRedirectsClient *http.Client) (rc io.ReadCloser, redirectURL string, err error) {
271276
u := fmt.Sprintf("repos/%s/%s/releases/assets/%d", owner, repo, id)
272277

273278
req, err := s.client.NewRequest("GET", u, nil)
@@ -293,6 +298,10 @@ func (s *RepositoriesService) DownloadReleaseAsset(ctx context.Context, owner, r
293298
if !strings.Contains(err.Error(), "disable redirect") {
294299
return nil, "", err
295300
}
301+
if followRedirectsClient != nil {
302+
rc, err := s.downloadReleaseAssetFromURL(ctx, followRedirectsClient, loc)
303+
return rc, "", err
304+
}
296305
return nil, loc, nil // Intentionally return no error with valid redirect URL.
297306
}
298307

@@ -304,6 +313,24 @@ func (s *RepositoriesService) DownloadReleaseAsset(ctx context.Context, owner, r
304313
return resp.Body, "", nil
305314
}
306315

316+
func (s *RepositoriesService) downloadReleaseAssetFromURL(ctx context.Context, followRedirectsClient *http.Client, url string) (rc io.ReadCloser, err error) {
317+
req, err := http.NewRequest("GET", url, nil)
318+
if err != nil {
319+
return nil, err
320+
}
321+
req = withContext(ctx, req)
322+
req.Header.Set("Accept", "*/*")
323+
resp, err := followRedirectsClient.Do(req)
324+
if err != nil {
325+
return nil, err
326+
}
327+
if err := CheckResponse(resp); err != nil {
328+
resp.Body.Close()
329+
return nil, err
330+
}
331+
return resp.Body, nil
332+
}
333+
307334
// EditReleaseAsset edits a repository release asset.
308335
//
309336
// GitHub API docs: https://developer.github.com/v3/repos/releases/#edit-a-release-asset

‎github/repos_releases_test.go‎

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ func TestRepositoriesService_DownloadReleaseAsset_Stream(t *testing.T) {
252252
fmt.Fprint(w, "Hello World")
253253
})
254254

255-
reader, _, err := client.Repositories.DownloadReleaseAsset(context.Background(), "o", "r", 1)
255+
reader, _, err := client.Repositories.DownloadReleaseAsset(context.Background(), "o", "r", 1, nil)
256256
if err != nil {
257257
t.Errorf("Repositories.DownloadReleaseAsset returned error: %v", err)
258258
}
@@ -276,7 +276,7 @@ func TestRepositoriesService_DownloadReleaseAsset_Redirect(t *testing.T) {
276276
http.Redirect(w, r, "/yo", http.StatusFound)
277277
})
278278

279-
_, got, err := client.Repositories.DownloadReleaseAsset(context.Background(), "o", "r", 1)
279+
_, got, err := client.Repositories.DownloadReleaseAsset(context.Background(), "o", "r", 1, nil)
280280
if err != nil {
281281
t.Errorf("Repositories.DownloadReleaseAsset returned error: %v", err)
282282
}
@@ -286,6 +286,36 @@ func TestRepositoriesService_DownloadReleaseAsset_Redirect(t *testing.T) {
286286
}
287287
}
288288

289+
func TestRepositoriesService_DownloadReleaseAsset_FollowRedirect(t *testing.T) {
290+
client, mux, _, teardown := setup()
291+
defer teardown()
292+
293+
mux.HandleFunc("/repos/o/r/releases/assets/1", func(w http.ResponseWriter, r *http.Request) {
294+
testMethod(t, r, "GET")
295+
testHeader(t, r, "Accept", defaultMediaType)
296+
// /yo, below will be served as baseURLPath/yo
297+
http.Redirect(w, r, baseURLPath+"/yo", http.StatusFound)
298+
})
299+
mux.HandleFunc("/yo", func(w http.ResponseWriter, r *http.Request) {
300+
testMethod(t, r, "GET")
301+
testHeader(t, r, "Accept", "*/*")
302+
w.Header().Set("Content-Type", "application/octet-stream")
303+
w.Header().Set("Content-Disposition", "attachment; filename=hello-world.txt")
304+
fmt.Fprint(w, "Hello World")
305+
})
306+
307+
reader, _, err := client.Repositories.DownloadReleaseAsset(context.Background(), "o", "r", 1, http.DefaultClient)
308+
content, err := ioutil.ReadAll(reader)
309+
if err != nil {
310+
t.Errorf("Repositories.DownloadReleaseAsset returned error: %v", err)
311+
}
312+
reader.Close()
313+
want := []byte("Hello World")
314+
if !bytes.Equal(want, content) {
315+
t.Errorf("Repositories.DownloadReleaseAsset returned %+v, want %+v", content, want)
316+
}
317+
}
318+
289319
func TestRepositoriesService_DownloadReleaseAsset_APIError(t *testing.T) {
290320
client, mux, _, teardown := setup()
291321
defer teardown()
@@ -297,7 +327,7 @@ func TestRepositoriesService_DownloadReleaseAsset_APIError(t *testing.T) {
297327
fmt.Fprint(w, `{"message":"Not Found","documentation_url":"https://developer.github.com/v3"}`)
298328
})
299329

300-
resp, loc, err := client.Repositories.DownloadReleaseAsset(context.Background(), "o", "r", 1)
330+
resp, loc, err := client.Repositories.DownloadReleaseAsset(context.Background(), "o", "r", 1, nil)
301331
if err == nil {
302332
t.Error("Repositories.DownloadReleaseAsset did not return an error")
303333
}

‎test/integration/repos_test.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ package integration
99

1010
import (
1111
"context"
12+
"io"
13+
"io/ioutil"
1214
"net/http"
1315
"reflect"
1416
"testing"
@@ -179,3 +181,19 @@ func TestRepositories_List(t *testing.T) {
179181
}
180182
}
181183
}
184+
185+
func TestRepositories_DownloadReleaseAsset(t *testing.T) {
186+
if !checkAuth("TestRepositories_DownloadReleaseAsset") {
187+
return
188+
}
189+
190+
rc, _, err := client.Repositories.DownloadReleaseAsset(context.Background(), "andersjanmyr", "goose", 484892, http.DefaultClient)
191+
if err != nil {
192+
t.Fatalf("Repositories.DownloadReleaseAsset(andersjanmyr, goose, 484892, true) returned error: %v", err)
193+
}
194+
defer func() { _ = rc.Close() }()
195+
_, err = io.Copy(ioutil.Discard, rc)
196+
if err != nil {
197+
t.Fatalf("Repositories.DownloadReleaseAsset(andersjanmyr, goose, 484892, true) returned error: %v", err)
198+
}
199+
}

0 commit comments

Comments
 (0)