chore(grequests): update grequests to v2
All checks were successful
ci/woodpecker/push/lint Pipeline was successful
ci/woodpecker/push/test Pipeline was successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/deploy Pipeline was successful

This commit is contained in:
Tom Neuber 2025-06-28 19:36:10 +02:00
parent a88da88a47
commit 00bf1820f1
Signed by: tom
GPG key ID: F17EFE4272D89FF6
5 changed files with 18 additions and 13 deletions

View file

@ -1,13 +1,14 @@
package downloader
import (
"context"
"errors"
"io"
"net/http"
"os"
"strings"
req "github.com/levigross/grequests"
req "github.com/levigross/grequests/v2"
"github.com/schollz/progressbar/v3"
)
@ -29,8 +30,8 @@ func NewContext(filename, link string) *Context {
}
}
func (c *Context) Download() error {
resp, err := req.Head(c.Link, nil)
func (c *Context) Download(ctx context.Context) error {
resp, err := req.Head(ctx, c.Link)
if err != nil {
if strings.Contains(err.Error(), "no such host") {
return ErrInvalidURL
@ -54,7 +55,7 @@ func (c *Context) Download() error {
return errors.New(resp.RawResponse.Status)
}
resp, err = req.Get(c.Link, nil)
resp, err = req.Get(ctx, c.Link)
if err != nil {
return err
}

View file

@ -1,6 +1,7 @@
package downloader_test
import (
"context"
"net/http"
"net/http/httptest"
"os"
@ -37,7 +38,7 @@ func TestDownload(t *testing.T) {
ctx := downloader.NewContext(filename, server.URL)
defer os.Remove(filename)
err := ctx.Download()
err := ctx.Download(context.Background())
if err != nil {
t.Errorf("expected no error, got %v", err)
}
@ -49,7 +50,7 @@ func TestDownload(t *testing.T) {
t.Run("InvalidURL", func(t *testing.T) {
ctx := downloader.NewContext("test_invalid_url.txt", "http://invalid.url")
err := ctx.Download()
err := ctx.Download(context.Background())
if err == nil || err.Error() != "invalid url" {
t.Errorf("expected invalid url error, got %v", err)
@ -63,7 +64,8 @@ func TestDownload(t *testing.T) {
"https://self-signed.badssl.com/", // Example URL that can be used
)
if err := ctx.Download(); err == nil || err.Error() != "certificate from unknown authority" {
err := ctx.Download(context.Background())
if err == nil || err.Error() != "certificate from unknown authority" {
t.Errorf("expected certificate from unknown authority error, got %v", err)
}
})
@ -77,7 +79,7 @@ func TestDownload(t *testing.T) {
ctx := downloader.NewContext("test_not_found.txt", server.URL)
if err := ctx.Download(); err == nil || err.Error() != "invalid url" {
if err := ctx.Download(context.Background()); err == nil || err.Error() != "invalid url" {
t.Errorf("expected invalid url error, got %v", err)
}
})
@ -91,7 +93,8 @@ func TestDownload(t *testing.T) {
ctx := downloader.NewContext("test_restricted_access.txt", server.URL)
if err := ctx.Download(); err == nil || err.Error() != "restricted access (credentials required)" {
err := ctx.Download(context.Background())
if err == nil || err.Error() != "restricted access (credentials required)" {
t.Errorf("expected restricted access error, got %v", err)
}
})