Skip to content

Commit

Permalink
Uses GET to get download file size
Browse files Browse the repository at this point in the history
(instead of HEAD, since servers might not respond to HEAD)
  • Loading branch information
cuducos committed Oct 24, 2023
1 parent e62f47a commit d7d361a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (d *Downloader) getDownloadSize(ctx context.Context, u string) (int64, erro
defer close(ch)
err := retry.Do(
func() error {
req, err := http.NewRequestWithContext(ctx, http.MethodHead, u, nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
if err != nil {
return fmt.Errorf("creating the request for %s: %w", u, err)
}
Expand Down
9 changes: 6 additions & 3 deletions downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ func TestDownload_Error(t *testing.T) {
{"timeout", func(w http.ResponseWriter) { time.Sleep(10 * timeout) }},
} {
t.Run(tc.desc, func(t *testing.T) {
var firstReq int32
s := httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodHead {
if atomic.CompareAndSwapInt32(&firstReq, 0, 1) {
w.Header().Add("Content-Length", "2")
return
}
Expand Down Expand Up @@ -62,9 +63,10 @@ func TestDownload_Error(t *testing.T) {
}

func TestDownload_OkWithDefaultDownloader(t *testing.T) {
var firstReq int32
s := httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodHead {
if atomic.CompareAndSwapInt32(&firstReq, 0, 1) {
w.Header().Add("Content-Length", "2")
return
}
Expand Down Expand Up @@ -271,9 +273,10 @@ func TestDownload_ReportPreviouslyDownloadedBytes(t *testing.T) {
func TestDownloadWithContext_ErrorUserTimeout(t *testing.T) {
userTimeout := 250 * time.Millisecond // please note that the user timeout is less than the timeout per chunk.
timeout := 10 * userTimeout
var firstReq int32
s := httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodHead {
if atomic.CompareAndSwapInt32(&firstReq, 0, 1) {
w.Header().Add("Content-Length", "2")
return
}
Expand Down

0 comments on commit d7d361a

Please sign in to comment.