fix httpNoBody from go-critic

Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
This commit is contained in:
Matthieu MOREL
2025-05-26 22:30:15 +02:00
parent 8f7faa01d1
commit 469afa5f8f
20 changed files with 51 additions and 52 deletions

View File

@@ -127,7 +127,6 @@ linters:
- exposedSyncMutex
- filepathJoin
- hexLiteral
- httpNoBody
- hugeParam
- ifElseChain
- importShadow

View File

@@ -30,7 +30,7 @@ func TestBoolValue(t *testing.T) {
for c, e := range cases {
v := url.Values{}
v.Set("test", c)
r, _ := http.NewRequest(http.MethodPost, "", nil)
r, _ := http.NewRequest(http.MethodPost, "", http.NoBody)
r.Form = v
a := BoolValue(r, "test")
@@ -41,14 +41,14 @@ func TestBoolValue(t *testing.T) {
}
func TestBoolValueOrDefault(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, "", nil)
r, _ := http.NewRequest(http.MethodGet, "", http.NoBody)
if !BoolValueOrDefault(r, "queryparam", true) {
t.Fatal("Expected to get true default value, got false")
}
v := url.Values{}
v.Set("param", "")
r, _ = http.NewRequest(http.MethodGet, "", nil)
r, _ = http.NewRequest(http.MethodGet, "", http.NoBody)
r.Form = v
if BoolValueOrDefault(r, "param", true) {
t.Fatal("Expected not to get true")
@@ -66,7 +66,7 @@ func TestInt64ValueOrZero(t *testing.T) {
for c, e := range cases {
v := url.Values{}
v.Set("test", c)
r, _ := http.NewRequest(http.MethodPost, "", nil)
r, _ := http.NewRequest(http.MethodPost, "", http.NoBody)
r.Form = v
a := Int64ValueOrZero(r, "test")
@@ -86,7 +86,7 @@ func TestInt64ValueOrDefault(t *testing.T) {
for c, e := range cases {
v := url.Values{}
v.Set("test", c)
r, _ := http.NewRequest(http.MethodPost, "", nil)
r, _ := http.NewRequest(http.MethodPost, "", http.NoBody)
r.Form = v
a, err := Int64ValueOrDefault(r, "test", -1)
@@ -102,7 +102,7 @@ func TestInt64ValueOrDefault(t *testing.T) {
func TestInt64ValueOrDefaultWithError(t *testing.T) {
v := url.Values{}
v.Set("test", "invalid")
r, _ := http.NewRequest(http.MethodPost, "", nil)
r, _ := http.NewRequest(http.MethodPost, "", http.NoBody)
r.Form = v
_, err := Int64ValueOrDefault(r, "test", -1)
@@ -150,7 +150,7 @@ func TestUint32Value(t *testing.T) {
}
for _, tc := range tests {
t.Run(tc.value, func(t *testing.T) {
r, _ := http.NewRequest(http.MethodPost, "", nil)
r, _ := http.NewRequest(http.MethodPost, "", http.NoBody)
r.Form = url.Values{}
if tc.value != valueNotSet {
r.Form.Set("field", tc.value)

View File

@@ -33,7 +33,7 @@ func TestJsonContentType(t *testing.T) {
func TestReadJSON(t *testing.T) {
t.Run("nil body", func(t *testing.T) {
req, err := http.NewRequest(http.MethodPost, "https://example.com/some/path", nil)
req, err := http.NewRequest(http.MethodPost, "https://example.com/some/path", http.NoBody)
if err != nil {
t.Error(err)
}

View File

@@ -79,7 +79,7 @@ func TestVersionMiddlewareVersion(t *testing.T) {
assert.NilError(t, err)
h := m.WrapHandler(handler)
req, _ := http.NewRequest(http.MethodGet, "/containers/json", nil)
req, _ := http.NewRequest(http.MethodGet, "/containers/json", http.NoBody)
resp := httptest.NewRecorder()
ctx := context.Background()
@@ -129,7 +129,7 @@ func TestVersionMiddlewareWithErrorsReturnsHeaders(t *testing.T) {
assert.NilError(t, err)
h := m.WrapHandler(handler)
req, _ := http.NewRequest(http.MethodGet, "/containers/json", nil)
req, _ := http.NewRequest(http.MethodGet, "/containers/json", http.NoBody)
resp := httptest.NewRecorder()
ctx := context.Background()

View File

@@ -22,7 +22,7 @@ import (
func callGetVolume(v *volumeRouter, name string) (*httptest.ResponseRecorder, error) {
ctx := context.WithValue(context.Background(), httputils.APIVersionKey{}, clusterVolumesVersion)
vars := map[string]string{"name": name}
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/volumes/%s", name), nil)
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/volumes/%s", name), http.NoBody)
resp := httptest.NewRecorder()
err := v.getVolumeByName(ctx, resp, req, vars)
@@ -32,7 +32,7 @@ func callGetVolume(v *volumeRouter, name string) (*httptest.ResponseRecorder, er
func callListVolumes(v *volumeRouter) (*httptest.ResponseRecorder, error) {
ctx := context.WithValue(context.Background(), httputils.APIVersionKey{}, clusterVolumesVersion)
vars := map[string]string{}
req := httptest.NewRequest(http.MethodGet, "/volumes", nil)
req := httptest.NewRequest(http.MethodGet, "/volumes", http.NoBody)
resp := httptest.NewRecorder()
err := v.getVolumesList(ctx, resp, req, vars)
@@ -428,7 +428,7 @@ func TestVolumeRemove(t *testing.T) {
}
ctx := context.WithValue(context.Background(), httputils.APIVersionKey{}, clusterVolumesVersion)
req := httptest.NewRequest(http.MethodDelete, "/volumes/vol1", nil)
req := httptest.NewRequest(http.MethodDelete, "/volumes/vol1", http.NoBody)
resp := httptest.NewRecorder()
err := v.deleteVolumes(ctx, resp, req, map[string]string{"name": "vol1"})
@@ -455,7 +455,7 @@ func TestVolumeRemoveSwarm(t *testing.T) {
}
ctx := context.WithValue(context.Background(), httputils.APIVersionKey{}, clusterVolumesVersion)
req := httptest.NewRequest(http.MethodDelete, "/volumes/vol1", nil)
req := httptest.NewRequest(http.MethodDelete, "/volumes/vol1", http.NoBody)
resp := httptest.NewRecorder()
err := v.deleteVolumes(ctx, resp, req, map[string]string{"name": "vol1"})
@@ -472,7 +472,7 @@ func TestVolumeRemoveNotFoundNoSwarm(t *testing.T) {
}
ctx := context.WithValue(context.Background(), httputils.APIVersionKey{}, clusterVolumesVersion)
req := httptest.NewRequest(http.MethodDelete, "/volumes/vol1", nil)
req := httptest.NewRequest(http.MethodDelete, "/volumes/vol1", http.NoBody)
resp := httptest.NewRecorder()
err := v.deleteVolumes(ctx, resp, req, map[string]string{"name": "vol1"})
@@ -489,7 +489,7 @@ func TestVolumeRemoveNotFoundNoManager(t *testing.T) {
}
ctx := context.WithValue(context.Background(), httputils.APIVersionKey{}, clusterVolumesVersion)
req := httptest.NewRequest(http.MethodDelete, "/volumes/vol1", nil)
req := httptest.NewRequest(http.MethodDelete, "/volumes/vol1", http.NoBody)
resp := httptest.NewRecorder()
err := v.deleteVolumes(ctx, resp, req, map[string]string{"name": "vol1"})
@@ -513,7 +513,7 @@ func TestVolumeRemoveFoundNoSwarm(t *testing.T) {
}
ctx := context.WithValue(context.Background(), httputils.APIVersionKey{}, clusterVolumesVersion)
req := httptest.NewRequest(http.MethodDelete, "/volumes/vol1", nil)
req := httptest.NewRequest(http.MethodDelete, "/volumes/vol1", http.NoBody)
resp := httptest.NewRecorder()
err := v.deleteVolumes(ctx, resp, req, map[string]string{"name": "vol1"})
@@ -536,7 +536,7 @@ func TestVolumeRemoveNoSwarmInUse(t *testing.T) {
}
ctx := context.WithValue(context.Background(), httputils.APIVersionKey{}, clusterVolumesVersion)
req := httptest.NewRequest(http.MethodDelete, "/volumes/inuse", nil)
req := httptest.NewRequest(http.MethodDelete, "/volumes/inuse", http.NoBody)
resp := httptest.NewRecorder()
err := v.deleteVolumes(ctx, resp, req, map[string]string{"name": "inuse"})
@@ -564,7 +564,7 @@ func TestVolumeRemoveSwarmForce(t *testing.T) {
}
ctx := context.WithValue(context.Background(), httputils.APIVersionKey{}, clusterVolumesVersion)
req := httptest.NewRequest(http.MethodDelete, "/volumes/vol1", nil)
req := httptest.NewRequest(http.MethodDelete, "/volumes/vol1", http.NoBody)
resp := httptest.NewRecorder()
err := v.deleteVolumes(ctx, resp, req, map[string]string{"name": "vol1"})
@@ -573,7 +573,7 @@ func TestVolumeRemoveSwarmForce(t *testing.T) {
assert.Assert(t, cerrdefs.IsConflict(err))
ctx = context.WithValue(context.Background(), httputils.APIVersionKey{}, clusterVolumesVersion)
req = httptest.NewRequest(http.MethodDelete, "/volumes/vol1?force=1", nil)
req = httptest.NewRequest(http.MethodDelete, "/volumes/vol1?force=1", http.NoBody)
resp = httptest.NewRecorder()
err = v.deleteVolumes(ctx, resp, req, map[string]string{"name": "vol1"})

View File

@@ -21,7 +21,7 @@ func TestMiddlewares(t *testing.T) {
}
srv.UseMiddleware(*m)
req, _ := http.NewRequest(http.MethodGet, "/containers/json", nil)
req, _ := http.NewRequest(http.MethodGet, "/containers/json", http.NoBody)
resp := httptest.NewRecorder()
ctx := context.Background()

View File

@@ -525,7 +525,7 @@ func TestClientRedirect(t *testing.T) {
for _, tc := range tests {
t.Run(tc.httpMethod, func(t *testing.T) {
req, err := http.NewRequest(tc.httpMethod, "/redirectme", nil)
req, err := http.NewRequest(tc.httpMethod, "/redirectme", http.NoBody)
assert.NilError(t, err)
resp, err := client.Do(req)
assert.Check(t, is.Equal(resp.StatusCode, tc.statusCode))

View File

@@ -40,7 +40,7 @@ func (cli *Client) postHijacked(ctx context.Context, path string, query url.Valu
// DialHijack returns a hijacked connection with negotiated protocol proto.
func (cli *Client) DialHijack(ctx context.Context, url, proto string, meta map[string][]string) (net.Conn, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, nil)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, http.NoBody)
if err != nil {
return nil, err
}

View File

@@ -603,7 +603,7 @@ func parseURL(info logger.Info) (*url.URL, error) {
}
func verifySplunkConnection(l *splunkLogger) error {
req, err := http.NewRequest(http.MethodOptions, l.url, nil)
req, err := http.NewRequest(http.MethodOptions, l.url, http.NoBody)
if err != nil {
return err
}

View File

@@ -103,7 +103,7 @@ func TestNewWithProxy(t *testing.T) {
proxyFunc := splunkLogger.transport.Proxy
assert.Assert(t, proxyFunc != nil)
req, err := http.NewRequest(http.MethodGet, splunkURL, nil)
req, err := http.NewRequest(http.MethodGet, splunkURL, http.NoBody)
assert.NilError(t, err)
proxyURL, err := proxyFunc(req)

View File

@@ -190,7 +190,7 @@ func TestAuthZPluginAPIDenyResponse(t *testing.T) {
socketClient, err := socketHTTPClient(daemonURL)
assert.NilError(t, err)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "/version", nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "/version", http.NoBody)
assert.NilError(t, err)
req.URL.Scheme = "http"
req.URL.Host = client.DummyHost
@@ -486,7 +486,7 @@ func TestAuthZPluginHeader(t *testing.T) {
socketClient, err := socketHTTPClient(daemonURL)
assert.NilError(t, err)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "/version", nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "/version", http.NoBody)
assert.NilError(t, err)
req.URL.Scheme = "http"
req.URL.Host = client.DummyHost

View File

@@ -46,7 +46,7 @@ func TestPeerCertificateMarshalJSON(t *testing.T) {
certs := []*x509.Certificate{cert}
addr := "www.authz.com/auth"
req, err := http.NewRequest(http.MethodGet, addr, nil)
req, err := http.NewRequest(http.MethodGet, addr, http.NoBody)
assert.NilError(t, err)
req.RequestURI = addr

View File

@@ -33,7 +33,7 @@ func TestMiddlewareWrapHandler(t *testing.T) {
assert.Assert(t, mdHandler != nil)
addr := "www.example.com/auth"
req, _ := http.NewRequest(http.MethodGet, addr, nil)
req, _ := http.NewRequest(http.MethodGet, addr, http.NoBody)
req.RequestURI = addr
req.Header.Add("header", "value")

View File

@@ -70,7 +70,7 @@ func loginV2(ctx context.Context, authConfig *registry.AuthConfig, endpoint APIE
endpointStr := strings.TrimRight(endpoint.URL.String(), "/") + "/v2/"
log.G(ctx).WithField("endpoint", endpointStr).Debug("attempting v2 login to registry endpoint")
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpointStr, nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpointStr, http.NoBody)
if err != nil {
return "", err
}
@@ -181,7 +181,7 @@ func PingV2Registry(endpoint *url.URL, transport http.RoundTripper) (challenge.M
Timeout: 15 * time.Second,
}
endpointStr := strings.TrimRight(endpoint.String(), "/") + "/v2/"
req, err := http.NewRequest(http.MethodGet, endpointStr, nil)
req, err := http.NewRequest(http.MethodGet, endpointStr, http.NoBody)
if err != nil {
return nil, err
}

View File

@@ -22,7 +22,7 @@ func TestResumableRequestHeaderSimpleErrors(t *testing.T) {
client := &http.Client{}
var req *http.Request
req, err := http.NewRequest(http.MethodGet, ts.URL, nil)
req, err := http.NewRequest(http.MethodGet, ts.URL, http.NoBody)
assert.NilError(t, err)
resreq := &requestReader{}
@@ -43,7 +43,7 @@ func TestResumableRequestHeaderNotTooMuchFailures(t *testing.T) {
client := &http.Client{}
var badReq *http.Request
badReq, err := http.NewRequest(http.MethodGet, "I'm not an url", nil)
badReq, err := http.NewRequest(http.MethodGet, "I'm not an url", http.NoBody)
assert.NilError(t, err)
resreq := &requestReader{
@@ -63,7 +63,7 @@ func TestResumableRequestHeaderTooMuchFailures(t *testing.T) {
client := &http.Client{}
var badReq *http.Request
badReq, err := http.NewRequest(http.MethodGet, "I'm not an url", nil)
badReq, err := http.NewRequest(http.MethodGet, "I'm not an url", http.NoBody)
assert.NilError(t, err)
resreq := &requestReader{
@@ -92,7 +92,7 @@ func (errorReaderCloser) Read(p []byte) (int, error) {
// If an unknown error is encountered, return 0, nil and log it
func TestResumableRequestReaderWithReadError(t *testing.T) {
var req *http.Request
req, err := http.NewRequest(http.MethodGet, "", nil)
req, err := http.NewRequest(http.MethodGet, "", http.NoBody)
assert.NilError(t, err)
client := &http.Client{}
@@ -123,7 +123,7 @@ func TestResumableRequestReaderWithReadError(t *testing.T) {
func TestResumableRequestReaderWithEOFWith416Response(t *testing.T) {
var req *http.Request
req, err := http.NewRequest(http.MethodGet, "", nil)
req, err := http.NewRequest(http.MethodGet, "", http.NoBody)
assert.NilError(t, err)
client := &http.Client{}
@@ -159,7 +159,7 @@ func TestResumableRequestReaderWithServerDoesntSupportByteRanges(t *testing.T) {
defer ts.Close()
var req *http.Request
req, err := http.NewRequest(http.MethodGet, ts.URL, nil)
req, err := http.NewRequest(http.MethodGet, ts.URL, http.NoBody)
assert.NilError(t, err)
client := &http.Client{}
@@ -185,7 +185,7 @@ func TestResumableRequestReaderWithZeroTotalSize(t *testing.T) {
defer ts.Close()
var req *http.Request
req, err := http.NewRequest(http.MethodGet, ts.URL, nil)
req, err := http.NewRequest(http.MethodGet, ts.URL, http.NoBody)
assert.NilError(t, err)
client := &http.Client{}
@@ -210,7 +210,7 @@ func TestResumableRequestReader(t *testing.T) {
defer ts.Close()
var req *http.Request
req, err := http.NewRequest(http.MethodGet, ts.URL, nil)
req, err := http.NewRequest(http.MethodGet, ts.URL, http.NoBody)
assert.NilError(t, err)
client := &http.Client{}
@@ -236,7 +236,7 @@ func TestResumableRequestReaderWithInitialResponse(t *testing.T) {
defer ts.Close()
var req *http.Request
req, err := http.NewRequest(http.MethodGet, ts.URL, nil)
req, err := http.NewRequest(http.MethodGet, ts.URL, http.NoBody)
assert.NilError(t, err)
client := &http.Client{}

View File

@@ -122,7 +122,7 @@ func (e *v1Endpoint) ping(ctx context.Context) (v1PingResult, error) {
pingURL := e.String() + "_ping"
log.G(ctx).WithField("url", pingURL).Debug("attempting v1 ping for registry endpoint")
req, err := http.NewRequestWithContext(ctx, http.MethodGet, pingURL, nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, pingURL, http.NoBody)
if err != nil {
return v1PingResult{}, invalidParam(err)
}

View File

@@ -175,12 +175,12 @@ func TestV1EndpointValidate(t *testing.T) {
func TestTrustedLocation(t *testing.T) {
for _, u := range []string{"http://example.com", "https://example.com:7777", "http://docker.io", "http://test.docker.com", "https://fakedocker.com"} {
req, _ := http.NewRequest(http.MethodGet, u, nil)
req, _ := http.NewRequest(http.MethodGet, u, http.NoBody)
assert.Check(t, !trustedLocation(req))
}
for _, u := range []string{"https://docker.io", "https://test.docker.com:80"} {
req, _ := http.NewRequest(http.MethodGet, u, nil)
req, _ := http.NewRequest(http.MethodGet, u, http.NoBody)
assert.Check(t, trustedLocation(req))
}
}
@@ -191,10 +191,10 @@ func TestAddRequiredHeadersToRedirectedRequests(t *testing.T) {
{"https://foo.docker.io:7777", "http://bar.docker.com"},
{"https://foo.docker.io", "https://example.com"},
} {
reqFrom, _ := http.NewRequest(http.MethodGet, urls[0], nil)
reqFrom, _ := http.NewRequest(http.MethodGet, urls[0], http.NoBody)
reqFrom.Header.Add("Content-Type", "application/json")
reqFrom.Header.Add("Authorization", "super_secret")
reqTo, _ := http.NewRequest(http.MethodGet, urls[1], nil)
reqTo, _ := http.NewRequest(http.MethodGet, urls[1], http.NoBody)
_ = addRequiredHeadersToRedirectedRequests(reqTo, []*http.Request{reqFrom})
@@ -215,10 +215,10 @@ func TestAddRequiredHeadersToRedirectedRequests(t *testing.T) {
{"https://docker.io", "https://docker.com"},
{"https://foo.docker.io:7777", "https://bar.docker.com"},
} {
reqFrom, _ := http.NewRequest(http.MethodGet, urls[0], nil)
reqFrom, _ := http.NewRequest(http.MethodGet, urls[0], http.NoBody)
reqFrom.Header.Add("Content-Type", "application/json")
reqFrom.Header.Add("Authorization", "super_secret")
reqTo, _ := http.NewRequest(http.MethodGet, urls[1], nil)
reqTo, _ := http.NewRequest(http.MethodGet, urls[1], http.NoBody)
_ = addRequiredHeadersToRedirectedRequests(reqTo, []*http.Request{reqFrom})

View File

@@ -222,7 +222,7 @@ func (r *session) searchRepositories(ctx context.Context, term string, limit int
u := r.indexEndpoint.String() + "search?q=" + url.QueryEscape(term) + "&n=" + url.QueryEscape(fmt.Sprintf("%d", limit))
log.G(ctx).WithField("url", u).Debug("searchRepositories")
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, http.NoBody)
if err != nil {
return nil, invalidParamWrapf(err, "error building request")
}

View File

@@ -572,7 +572,7 @@ func (d *Daemon) StartWithLogFile(out *os.File, providedArgs ...string) error {
Transport: clientConfig.transport,
}
req, err := http.NewRequest(http.MethodGet, "/_ping", nil)
req, err := http.NewRequest(http.MethodGet, "/_ping", http.NoBody)
if err != nil {
return errors.Wrapf(err, "[%s] could not create new request", d.id)
}
@@ -947,7 +947,7 @@ func (d *Daemon) queryRootDir() (string, error) {
Transport: clientConfig.transport,
}
req, err := http.NewRequest(http.MethodGet, "/info", nil)
req, err := http.NewRequest(http.MethodGet, "/info", http.NoBody)
if err != nil {
return "", err
}

View File

@@ -117,7 +117,7 @@ func newRequest(endpoint string, opts *Options) (*http.Request, error) {
if err != nil {
return nil, errors.Wrapf(err, "failed parsing url %q", opts.host)
}
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
req, err := http.NewRequest(http.MethodGet, endpoint, http.NoBody)
if err != nil {
return nil, errors.Wrap(err, "failed to create request")
}