From f036c17863e36ba5740c60433a2289ed14b7f929 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 18 Dec 2025 17:10:36 +0100 Subject: [PATCH 1/8] Refactored options check --- downloader.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/downloader.go b/downloader.go index 5b689ee..b8a6530 100644 --- a/downloader.go +++ b/downloader.go @@ -12,6 +12,7 @@ import ( "io" "net/http" "os" + "slices" "sync" "time" ) @@ -137,14 +138,9 @@ func DownloadWithConfig(file string, reqURL string, config Config, options ...Do // in the specified file. A download resume is tried if a file shorter than the requested // url is already present. The download can be cancelled using the provided context. func DownloadWithConfigAndContext(ctx context.Context, file string, reqURL string, config Config, options ...DownloadOptions) (*Downloader, error) { - noResume := false - for _, opt := range options { - if opt == NoResume { - noResume = true - } - } - req, err := http.NewRequestWithContext(ctx, "GET", reqURL, nil) + noResume := slices.Contains(options, NoResume) + req, err := http.NewRequestWithContext(ctx, "GET", reqURL, nil) if err != nil { return nil, fmt.Errorf("setting up HTTP request: %s", err) } From 4b28ed15585ea60392384472a31f75c851123d2f Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 18 Dec 2025 17:48:16 +0100 Subject: [PATCH 2/8] Perform an HEAD request to get info before downloading --- downloader.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/downloader.go b/downloader.go index b8a6530..fef8bf3 100644 --- a/downloader.go +++ b/downloader.go @@ -140,13 +140,27 @@ func DownloadWithConfig(file string, reqURL string, config Config, options ...Do func DownloadWithConfigAndContext(ctx context.Context, file string, reqURL string, config Config, options ...DownloadOptions) (*Downloader, error) { noResume := slices.Contains(options, NoResume) + // Perform a HEAD call to gather information about the server capabilities + serverAcceptsRanges := false + var size int64 + if headReq, err := http.NewRequestWithContext(ctx, "HEAD", reqURL, nil); err != nil { + return nil, fmt.Errorf("setting up HEAD request: %s", err) + } else if headResp, err := config.HttpClient.Do(headReq); err != nil { + return nil, fmt.Errorf("performing HEAD request: %s", err) + } else { + serverAcceptsRanges = (headResp.Header.Get("Accept-Ranges") == "bytes") + size = headResp.ContentLength + _ = headResp.Body.Close() + } + + // Setup the actual GET request req, err := http.NewRequestWithContext(ctx, "GET", reqURL, nil) if err != nil { return nil, fmt.Errorf("setting up HTTP request: %s", err) } var completed int64 - if !noResume { + if !noResume && serverAcceptsRanges { if info, err := os.Stat(file); err == nil { completed = info.Size() req.Header.Set("Range", fmt.Sprintf("bytes=%d-", completed)) @@ -178,7 +192,7 @@ func DownloadWithConfigAndContext(ctx context.Context, file string, reqURL strin Resp: resp, out: f, completed: completed, - size: resp.ContentLength + completed, + size: size, } return d, nil } From a074d4c47a05dc54ba6462f3be2717c574fd1427 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 18 Dec 2025 17:50:21 +0100 Subject: [PATCH 3/8] Refactored test helper function --- downloader_test.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/downloader_test.go b/downloader_test.go index ee0f703..548156b 100644 --- a/downloader_test.go +++ b/downloader_test.go @@ -13,6 +13,7 @@ import ( "io" "net/http" "os" + "path/filepath" "testing" "time" @@ -25,12 +26,14 @@ func makeTmpFile(t *testing.T) string { require.NoError(t, tmp.Close()) tmpFile := tmp.Name() require.NoError(t, os.Remove(tmpFile)) + t.Cleanup(func() { + os.Remove(tmpFile) + }) return tmpFile } func TestDownload(t *testing.T) { tmpFile := makeTmpFile(t) - defer os.Remove(tmpFile) d, err := Download(tmpFile, "https://go.bug.st/test.txt") require.NoError(t, err) @@ -49,7 +52,6 @@ func TestDownload(t *testing.T) { func TestResume(t *testing.T) { tmpFile := makeTmpFile(t) - defer os.Remove(tmpFile) part, err := os.ReadFile("testdata/test.txt.part") require.NoError(t, err) @@ -73,7 +75,6 @@ func TestResume(t *testing.T) { func TestNoResume(t *testing.T) { tmpFile := makeTmpFile(t) - defer os.Remove(tmpFile) part, err := os.ReadFile("testdata/test.txt.part") require.NoError(t, err) @@ -97,7 +98,6 @@ func TestNoResume(t *testing.T) { func TestInvalidRequest(t *testing.T) { tmpFile := makeTmpFile(t) - defer os.Remove(tmpFile) d, err := Download(tmpFile, "asd://go.bug.st/test.txt") require.Error(t, err) @@ -112,7 +112,6 @@ func TestInvalidRequest(t *testing.T) { func TestRunAndPool(t *testing.T) { tmpFile := makeTmpFile(t) - defer os.Remove(tmpFile) d, err := Download(tmpFile, "https://downloads.arduino.cc/cores/avr-1.6.20.tar.bz2") require.NoError(t, err) @@ -131,7 +130,6 @@ func TestRunAndPool(t *testing.T) { func TestErrorOnFileOpening(t *testing.T) { tmpFile := makeTmpFile(t) - defer os.Remove(tmpFile) require.NoError(t, os.WriteFile(tmpFile, []byte{}, 0000)) d, err := Download(tmpFile, "http://go.bug.st/test.txt") @@ -196,7 +194,6 @@ func TestContextCancelation(t *testing.T) { time.Sleep(time.Second) tmpFile := makeTmpFile(t) - defer os.Remove(tmpFile) ctx, cancel := context.WithCancel(context.Background()) d, err := DownloadWithConfigAndContext(ctx, tmpFile, "http://127.0.0.1:8080/slow", Config{}) From e9b8574debd846701437bb26734c3151dab03265 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 18 Dec 2025 17:51:06 +0100 Subject: [PATCH 4/8] Fixed TestErrorOnFileOpening unit-test --- downloader_test.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/downloader_test.go b/downloader_test.go index 548156b..46bc492 100644 --- a/downloader_test.go +++ b/downloader_test.go @@ -129,10 +129,8 @@ func TestRunAndPool(t *testing.T) { } func TestErrorOnFileOpening(t *testing.T) { - tmpFile := makeTmpFile(t) - - require.NoError(t, os.WriteFile(tmpFile, []byte{}, 0000)) - d, err := Download(tmpFile, "http://go.bug.st/test.txt") + unaccessibleFile := filepath.Join(os.TempDir(), "nonexistentdir", "test.txt") + d, err := Download(unaccessibleFile, "http://go.bug.st/test.txt") require.Error(t, err) require.Nil(t, d) } From df8f82a9e7e21513028dd4c1a644da87dd5e929c Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 18 Dec 2025 17:51:31 +0100 Subject: [PATCH 5/8] Fixed TestContextCancelation unit-test --- downloader_test.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/downloader_test.go b/downloader_test.go index 46bc492..9d73af2 100644 --- a/downloader_test.go +++ b/downloader_test.go @@ -174,7 +174,7 @@ func TestApplyUserAgentHeaderUsingConfig(t *testing.T) { func TestContextCancelation(t *testing.T) { slowHandler := func(w http.ResponseWriter, r *http.Request) { - for i := 0; i < 50; i++ { + for i := range 50 { fmt.Fprintf(w, "Hello %d\n", i) w.(http.Flusher).Flush() time.Sleep(100 * time.Millisecond) @@ -185,8 +185,8 @@ func TestContextCancelation(t *testing.T) { mux.HandleFunc("/slow", slowHandler) server := &http.Server{Addr: ":8080", Handler: mux} go func() { - server.ListenAndServe() - fmt.Println("Server stopped") + err := server.ListenAndServe() + fmt.Println("Server stopped with err:", err) }() // Wait for server start time.Sleep(time.Second) @@ -198,6 +198,7 @@ func TestContextCancelation(t *testing.T) { require.NoError(t, err) // Cancel in two seconds + startTime := time.Now() go func() { time.Sleep(2 * time.Second) cancel() @@ -206,11 +207,11 @@ func TestContextCancelation(t *testing.T) { // Run slow download max := int64(0) err = d.RunAndPoll(func(curr int64) { - fmt.Println(curr) + fmt.Println("Downloaded", curr, "bytes. Elapsed:", time.Since(startTime)) max = curr }, 100*time.Millisecond) require.EqualError(t, err, "context canceled") require.True(t, max < 400) - require.NoError(t, server.Shutdown(ctx)) + require.NoError(t, server.Shutdown(context.Background())) } From 7de1d699d494b87ead1a36b792d6099c3fac2fc1 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 18 Dec 2025 17:58:14 +0100 Subject: [PATCH 6/8] Use bash in CI --- .github/workflows/go-test.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/go-test.yml b/.github/workflows/go-test.yml index 32cdb6c..13b6754 100644 --- a/.github/workflows/go-test.yml +++ b/.github/workflows/go-test.yml @@ -28,4 +28,5 @@ jobs: go-version-file: go.mod - name: Run tests + shell: bash run: go test -race -coverprofile=coverage.txt -covermode=atomic From 2d64b3d4dd006706276c29eb89c447635250ad35 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 18 Dec 2025 20:34:41 +0100 Subject: [PATCH 7/8] Force git to not alter line endings on testdata --- .gitattributes | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..57b3ae6 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +testdata/* text eol=lf From d462f8a7a212891414aa7c77cee57b8f6b9262a4 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 18 Dec 2025 20:45:30 +0100 Subject: [PATCH 8/8] Added test to check resume-an-already-downloaded-file --- downloader_test.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/downloader_test.go b/downloader_test.go index 9d73af2..29fcb9f 100644 --- a/downloader_test.go +++ b/downloader_test.go @@ -73,6 +73,26 @@ func TestResume(t *testing.T) { require.Equal(t, file1, file2) } +func TestResumeOnAlreadyCompletedFile(t *testing.T) { + tmpFile := makeTmpFile(t) + + full, err := os.ReadFile("testdata/test.txt") + require.NoError(t, err) + require.NoError(t, os.WriteFile(tmpFile, full, 0644)) + + d, err := Download(tmpFile, "https://go.bug.st/test.txt") + require.NoError(t, err) + require.Equal(t, int64(8052), d.Completed()) + require.Equal(t, int64(8052), d.Size()) + require.NoError(t, d.Run()) + require.Equal(t, int64(8052), d.Completed()) + + // Check file content is unchanged + file2, err := os.ReadFile(tmpFile) + require.NoError(t, err) + require.Equal(t, full, file2) +} + func TestNoResume(t *testing.T) { tmpFile := makeTmpFile(t)