This is an automated email from the ASF dual-hosted git repository. astefanutti pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit 0139d21f9d3c4abf3569bc4ec3400a5430b0a00e Author: Luca Burgazzoli <[email protected]> AuthorDate: Wed Nov 24 14:39:07 2021 +0100 fix(lint): net/http.Get must not be called (noctx) --- .golangci.yml | 1 - pkg/cmd/util_content.go | 19 +++++++++++-------- pkg/cmd/util_content_test.go | 20 ++++++++++++++++++++ 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 490d6da..c9af8be 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -54,7 +54,6 @@ linters: - nlreturn - nilnil - exhaustive - - noctx - nakedret - contextcheck - maligned diff --git a/pkg/cmd/util_content.go b/pkg/cmd/util_content.go index 289621e..0ec57a4 100644 --- a/pkg/cmd/util_content.go +++ b/pkg/cmd/util_content.go @@ -18,8 +18,9 @@ limitations under the License. package cmd import ( + "context" "fmt" - "io/ioutil" + "io" "net/http" "net/url" "os" @@ -101,7 +102,14 @@ func loadTextContent(source string, base64Compression bool) (string, string, boo } func loadContentHTTP(u fmt.Stringer) ([]byte, error) { - resp, err := http.Get(u.String()) + req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, u.String(), nil) + if err != nil { + return nil, err + } + + c := &http.Client{} + + resp, err := c.Do(req) if err != nil { return []byte{}, err } @@ -113,12 +121,7 @@ func loadContentHTTP(u fmt.Stringer) ([]byte, error) { return []byte{}, fmt.Errorf("the provided URL %s is not reachable, error code is %d", u.String(), resp.StatusCode) } - content, err := ioutil.ReadAll(resp.Body) - if err != nil { - return []byte{}, err - } - - return content, nil + return io.ReadAll(resp.Body) } func loadContentGitHub(u *url.URL) ([]byte, error) { diff --git a/pkg/cmd/util_content_test.go b/pkg/cmd/util_content_test.go index ae34b1d..5684ac8 100644 --- a/pkg/cmd/util_content_test.go +++ b/pkg/cmd/util_content_test.go @@ -18,7 +18,11 @@ limitations under the License. package cmd import ( + "fmt" "io/ioutil" + "net/http" + "net/http/httptest" + "net/url" "os" "testing" @@ -97,3 +101,19 @@ func TestIsBinary(t *testing.T) { assert.True(t, isBinary("application/zip")) assert.False(t, isBinary("text/plain")) } + +func TestContentHttp(t *testing.T) { + expected := "the content" + svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _, _ = fmt.Fprintf(w, expected) + })) + defer svr.Close() + + u, err := url.Parse(svr.URL) + assert.Nil(t, err) + + data, err := loadContentHTTP(u) + assert.Nil(t, err) + assert.NotEmpty(t, data) + assert.Equal(t, expected, string(data)) +}
