Skip to content

Commit b0a5e60

Browse files
authored
fix: DownloadReleaseAsset handles renamed repository (#3392)
Fixes: #3081.
1 parent 3c06fc1 commit b0a5e60

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

github/repos_releases.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,10 @@ func (s *RepositoriesService) GetReleaseAsset(ctx context.Context, owner, repo s
333333
// of the io.ReadCloser. Exactly one of rc and redirectURL will be zero.
334334
//
335335
// followRedirectsClient can be passed to download the asset from a redirected
336-
// location. Passing http.DefaultClient is recommended unless special circumstances
337-
// exist, but it's possible to pass any http.Client. If nil is passed the
338-
// redirectURL will be returned instead.
336+
// location. Specifying any http.Client is possible, but passing http.DefaultClient
337+
// is recommended, except when the specified repository is private, in which case
338+
// it's necessary to pass an http.Client that performs authenticated requests.
339+
// If nil is passed the redirectURL will be returned instead.
339340
//
340341
// GitHub API docs: https://docs.github.com/rest/releases/assets#get-a-release-asset
341342
//
@@ -387,7 +388,7 @@ func (s *RepositoriesService) downloadReleaseAssetFromURL(ctx context.Context, f
387388
return nil, err
388389
}
389390
req = withContext(ctx, req)
390-
req.Header.Set("Accept", "*/*")
391+
req.Header.Set("Accept", defaultMediaType)
391392
resp, err := followRedirectsClient.Do(req)
392393
if err != nil {
393394
return nil, err

github/repos_releases_test.go

+44-2
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ func TestRepositoriesService_DownloadReleaseAsset_FollowRedirect(t *testing.T) {
489489
})
490490
mux.HandleFunc("/yo", func(w http.ResponseWriter, r *http.Request) {
491491
testMethod(t, r, "GET")
492-
testHeader(t, r, "Accept", "*/*")
492+
testHeader(t, r, "Accept", defaultMediaType)
493493
w.Header().Set("Content-Type", "application/octet-stream")
494494
w.Header().Set("Content-Disposition", "attachment; filename=hello-world.txt")
495495
fmt.Fprint(w, "Hello World")
@@ -511,6 +511,48 @@ func TestRepositoriesService_DownloadReleaseAsset_FollowRedirect(t *testing.T) {
511511
}
512512
}
513513

514+
func TestRepositoriesService_DownloadReleaseAsset_FollowMultipleRedirects(t *testing.T) {
515+
t.Parallel()
516+
client, mux, _ := setup(t)
517+
518+
mux.HandleFunc("/repos/o/r/releases/assets/1", func(w http.ResponseWriter, r *http.Request) {
519+
w.Header().Set("Content-Type", "application/json; charset=utf-8")
520+
testMethod(t, r, "GET")
521+
testHeader(t, r, "Accept", defaultMediaType)
522+
// /yo, below will be served as baseURLPath/yo
523+
http.Redirect(w, r, baseURLPath+"/yo", http.StatusMovedPermanently)
524+
})
525+
mux.HandleFunc("/yo", func(w http.ResponseWriter, r *http.Request) {
526+
w.Header().Set("Content-Type", "text/html;charset=utf-8")
527+
testMethod(t, r, "GET")
528+
testHeader(t, r, "Accept", defaultMediaType)
529+
// /yo2, below will be served as baseURLPath/yo2
530+
http.Redirect(w, r, baseURLPath+"/yo2", http.StatusFound)
531+
})
532+
mux.HandleFunc("/yo2", func(w http.ResponseWriter, r *http.Request) {
533+
w.Header().Set("Content-Type", "application/octet-stream")
534+
w.Header().Set("Content-Disposition", "attachment; filename=hello-world.txt")
535+
testMethod(t, r, "GET")
536+
testHeader(t, r, "Accept", defaultMediaType)
537+
fmt.Fprint(w, "Hello World")
538+
})
539+
540+
ctx := context.Background()
541+
reader, _, err := client.Repositories.DownloadReleaseAsset(ctx, "o", "r", 1, http.DefaultClient)
542+
if err != nil {
543+
t.Errorf("Repositories.DownloadReleaseAsset returned error: %v", err)
544+
}
545+
content, err := io.ReadAll(reader)
546+
if err != nil {
547+
t.Errorf("Reading Repositories.DownloadReleaseAsset returned error: %v", err)
548+
}
549+
reader.Close()
550+
want := []byte("Hello World")
551+
if !bytes.Equal(want, content) {
552+
t.Errorf("Repositories.DownloadReleaseAsset returned %+v, want %+v", content, want)
553+
}
554+
}
555+
514556
func TestRepositoriesService_DownloadReleaseAsset_FollowRedirectToError(t *testing.T) {
515557
t.Parallel()
516558
client, mux, _ := setup(t)
@@ -523,7 +565,7 @@ func TestRepositoriesService_DownloadReleaseAsset_FollowRedirectToError(t *testi
523565
})
524566
mux.HandleFunc("/yo", func(w http.ResponseWriter, r *http.Request) {
525567
testMethod(t, r, "GET")
526-
testHeader(t, r, "Accept", "*/*")
568+
testHeader(t, r, "Accept", defaultMediaType)
527569
w.WriteHeader(http.StatusNotFound)
528570
})
529571

0 commit comments

Comments
 (0)