mirror of
https://github.com/portainer/portainer.git
synced 2025-08-06 06:15:22 +02:00
fix(linters): add the bodyclose linter BE-12112 (#959)
This commit is contained in:
parent
da30780ac2
commit
3eab294908
4 changed files with 50 additions and 1 deletions
|
@ -13,6 +13,7 @@ linters:
|
||||||
- intrange
|
- intrange
|
||||||
- perfsprint
|
- perfsprint
|
||||||
- ineffassign
|
- ineffassign
|
||||||
|
- bodyclose
|
||||||
|
|
||||||
linters-settings:
|
linters-settings:
|
||||||
depguard:
|
depguard:
|
||||||
|
|
|
@ -33,8 +33,13 @@ func TestLimitAccess(t *testing.T) {
|
||||||
|
|
||||||
ts := httptest.NewServer(handler)
|
ts := httptest.NewServer(handler)
|
||||||
defer ts.Close()
|
defer ts.Close()
|
||||||
http.Get(ts.URL)
|
|
||||||
resp, err := http.Get(ts.URL)
|
resp, err := http.Get(ts.URL)
|
||||||
|
if err == nil {
|
||||||
|
resp.Body.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err = http.Get(ts.URL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,6 +42,8 @@ func ValidateHelmRepositoryURL(repoUrl string, client *http.Client) error {
|
||||||
return fmt.Errorf("%s is not a valid chart repository or cannot be reached: %w", repoUrl, err)
|
return fmt.Errorf("%s is not a valid chart repository or cannot be reached: %w", repoUrl, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
response.Body.Close()
|
||||||
|
|
||||||
// Success is indicated with 2xx status codes. 3xx status codes indicate a redirect.
|
// Success is indicated with 2xx status codes. 3xx status codes indicate a redirect.
|
||||||
statusOK := response.StatusCode >= 200 && response.StatusCode < 300
|
statusOK := response.StatusCode >= 200 && response.StatusCode < 300
|
||||||
if !statusOK {
|
if !statusOK {
|
||||||
|
|
|
@ -1,10 +1,13 @@
|
||||||
package libhelm
|
package libhelm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/portainer/portainer/pkg/libhelm/test"
|
"github.com/portainer/portainer/pkg/libhelm/test"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_ValidateHelmRepositoryURL(t *testing.T) {
|
func Test_ValidateHelmRepositoryURL(t *testing.T) {
|
||||||
|
@ -49,3 +52,41 @@ func Test_ValidateHelmRepositoryURL(t *testing.T) {
|
||||||
}(test)
|
}(test)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestValidateHelmRepositoryURL(t *testing.T) {
|
||||||
|
var fail bool
|
||||||
|
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if fail {
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}))
|
||||||
|
defer srv.Close()
|
||||||
|
|
||||||
|
// Success
|
||||||
|
err := ValidateHelmRepositoryURL(srv.URL, nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// Failure
|
||||||
|
fail = true
|
||||||
|
|
||||||
|
var failureURLs = []string{
|
||||||
|
"",
|
||||||
|
"!",
|
||||||
|
"oci://example.com",
|
||||||
|
"ftp://example.com",
|
||||||
|
srv.URL,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, url := range failureURLs {
|
||||||
|
err = ValidateHelmRepositoryURL(url, nil)
|
||||||
|
require.Error(t, err)
|
||||||
|
|
||||||
|
err = ValidateHelmRepositoryURL(srv.URL, nil)
|
||||||
|
require.Error(t, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue