chore: fix httpNoBody issues from gocritic

Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
This commit is contained in:
Matthieu MOREL
2026-03-02 19:56:05 +01:00
parent 1751685dd4
commit 026d284c43
17 changed files with 80 additions and 46 deletions

View File

@@ -64,9 +64,6 @@ linters:
# We have stopped at some point to write doc comments on exported symbols.
# TODO(beorn7): Maybe we should enforce this again? There are ~500 offenders right now.
text: exported (.+) should have comment( \(or a comment on this block\))? or be unexported
- linters:
- gocritic
text: "appendAssign"
- linters:
- errcheck
path: _test.go
@@ -118,6 +115,43 @@ linters:
- (*net/http.Server).Shutdown
# Never check for rollback errors as Rollback() is called when a previous error was detected.
- (github.com/prometheus/prometheus/storage.Appender).Rollback
gocritic:
enable-all: true
disabled-checks:
- appendAssign
- appendCombine
- badLock
- boolExprSimplify
- commentedOutCode
- deferInLoop
- docStub
- emptyStringTest
- equalFold
- evalOrder
- exposedSyncMutex
- hexLiteral
- hugeParam
- importShadow
- nestingReduce
- ptrToRefParam
- preferStringWriter
- rangeValCopy
- redundantSprint
- regexpSimplify
- returnAfterHttpError
- sloppyReassign
- sprintfQuotedString
- stringsCompare
- todoCommentWithoutDetail
- tooManyResultsChecker
- typeUnparen
- truncateCmp
- typeDefFirst
- unlabelStmt
- unnamedResult
- unnecessaryBlock
- unnecessaryDefer
- whyNoLint
govet:
disable:
- shadow

View File

@@ -578,7 +578,7 @@ func CheckServerStatus(serverURL *url.URL, checkEndpoint string, roundTripper ht
return err
}
request, err := http.NewRequest(http.MethodGet, config.Address, nil)
request, err := http.NewRequest(http.MethodGet, config.Address, http.NoBody)
if err != nil {
return err
}

View File

@@ -81,7 +81,7 @@ const appListPath string = "/apps"
func fetchApps(ctx context.Context, server string, client *http.Client) (*Applications, error) {
url := fmt.Sprintf("%s%s", server, appListPath)
request, err := http.NewRequest(http.MethodGet, url, nil)
request, err := http.NewRequest(http.MethodGet, url, http.NoBody)
if err != nil {
return nil, err
}

View File

@@ -70,7 +70,7 @@ func newRobotDiscovery(conf *SDConfig, _ *slog.Logger) (*robotDiscovery, error)
}
func (d *robotDiscovery) refresh(context.Context) ([]*targetgroup.Group, error) {
req, err := http.NewRequest(http.MethodGet, d.endpoint+"/server", nil)
req, err := http.NewRequest(http.MethodGet, d.endpoint+"/server", http.NoBody)
if err != nil {
return nil, err
}

View File

@@ -151,7 +151,7 @@ func NewDiscovery(conf *SDConfig, opts discovery.DiscovererOptions) (*Discovery,
}
func (d *Discovery) Refresh(ctx context.Context) ([]*targetgroup.Group, error) {
req, err := http.NewRequest(http.MethodGet, d.url, nil)
req, err := http.NewRequest(http.MethodGet, d.url, http.NoBody)
if err != nil {
return nil, err
}

View File

@@ -339,7 +339,7 @@ type appListClient func(ctx context.Context, client *http.Client, url string) (*
// fetchApps requests a list of applications from a marathon server.
func fetchApps(ctx context.Context, client *http.Client, url string) (*appList, error) {
request, err := http.NewRequest(http.MethodGet, url, nil)
request, err := http.NewRequest(http.MethodGet, url, http.NoBody)
if err != nil {
return nil, err
}

View File

@@ -126,7 +126,7 @@ func (i *iaasDiscovery) refresh(ctx context.Context) ([]*targetgroup.Group, erro
q.Set("details", "true")
apiURL.RawQuery = q.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiURL.String(), nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiURL.String(), http.NoBody)
if err != nil {
return nil, fmt.Errorf("creating request: %w", err)
}

View File

@@ -211,7 +211,7 @@ func (d *Discovery) refresh(ctx context.Context) ([]*targetgroup.Group, error) {
endpoint = fmt.Sprintf("%s?groups=%s", endpoint, groups)
}
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
req, err := http.NewRequest(http.MethodGet, endpoint, http.NoBody)
if err != nil {
return nil, err
}

View File

@@ -734,7 +734,7 @@ var UserAgent = version.PrometheusUserAgent()
func (s *targetScraper) scrape(ctx context.Context) (*http.Response, error) {
if s.req == nil {
req, err := http.NewRequest(http.MethodGet, s.URL().String(), nil)
req, err := http.NewRequest(http.MethodGet, s.URL().String(), http.NoBody)
if err != nil {
return nil, err
}

View File

@@ -75,7 +75,7 @@ func TestCompressionHandler_PlainText(t *testing.T) {
func BenchmarkNewCompressionHandler_MaliciousAcceptEncoding(b *testing.B) {
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/whatever", nil)
req := httptest.NewRequest(http.MethodGet, "/whatever", http.NoBody)
req.Header.Set("Accept-Encoding", strings.Repeat(",", http.DefaultMaxHeaderBytes))
b.ReportAllocs()
@@ -97,7 +97,7 @@ func TestCompressionHandler_Gzip(t *testing.T) {
},
}
req, _ := http.NewRequest(http.MethodGet, server.URL+"/foo_endpoint", nil)
req, _ := http.NewRequest(http.MethodGet, server.URL+"/foo_endpoint", http.NoBody)
req.Header.Set(acceptEncodingHeader, gzipEncoding)
resp, err := client.Do(req)
@@ -132,7 +132,7 @@ func TestCompressionHandler_Deflate(t *testing.T) {
},
}
req, _ := http.NewRequest(http.MethodGet, server.URL+"/foo_endpoint", nil)
req, _ := http.NewRequest(http.MethodGet, server.URL+"/foo_endpoint", http.NoBody)
req.Header.Set(acceptEncodingHeader, deflateEncoding)
resp, err := client.Do(req)

View File

@@ -41,7 +41,7 @@ func TestCORSHandler(t *testing.T) {
dummyOrigin := "https://foo.com"
// OPTIONS with legit origin
req, err := http.NewRequest(http.MethodOptions, server.URL+"/any_path", nil)
req, err := http.NewRequest(http.MethodOptions, server.URL+"/any_path", http.NoBody)
require.NoError(t, err, "could not create request")
req.Header.Set("Origin", dummyOrigin)
@@ -55,7 +55,7 @@ func TestCORSHandler(t *testing.T) {
require.Equal(t, dummyOrigin, AccessControlAllowOrigin, "expected Access-Control-Allow-Origin header")
// OPTIONS with bad origin
req, err = http.NewRequest(http.MethodOptions, server.URL+"/any_path", nil)
req, err = http.NewRequest(http.MethodOptions, server.URL+"/any_path", http.NoBody)
require.NoError(t, err, "could not create request")
req.Header.Set("Origin", "https://not-foo.com")
@@ -69,7 +69,7 @@ func TestCORSHandler(t *testing.T) {
require.Equal(t, "Origin", Vary)
// OPTIONS with no origin
req, err = http.NewRequest(http.MethodOptions, server.URL+"/any_path", nil)
req, err = http.NewRequest(http.MethodOptions, server.URL+"/any_path", http.NoBody)
require.NoError(t, err)
resp, err = client.Do(req)

View File

@@ -69,7 +69,7 @@ func GET(t *testing.T, api *APIWrapper, path string, queryParams ...string) *Res
fullPath = path + "?" + values.Encode()
}
req := httptest.NewRequest(http.MethodGet, fullPath, nil)
req := httptest.NewRequest(http.MethodGet, fullPath, http.NoBody)
return executeRequest(t, api, req)
}

View File

@@ -610,7 +610,7 @@ func TestGetSeries(t *testing.T) {
}
u.RawQuery = q.Encode()
r, err := http.NewRequest(method, u.String(), nil)
r, err := http.NewRequest(method, u.String(), http.NoBody)
if method == http.MethodPost {
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
@@ -718,7 +718,7 @@ func TestQueryExemplars(t *testing.T) {
u, err := url.Parse("http://example.com")
require.NoError(t, err)
u.RawQuery = qs.Encode()
r, err := http.NewRequest(method, u.String(), nil)
r, err := http.NewRequest(method, u.String(), http.NoBody)
if method == http.MethodPost {
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
@@ -836,7 +836,7 @@ func TestLabelNames(t *testing.T) {
}
u.RawQuery = q.Encode()
r, err := http.NewRequest(method, u.String(), nil)
r, err := http.NewRequest(method, u.String(), http.NoBody)
if method == http.MethodPost {
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
@@ -945,7 +945,7 @@ func TestStats(t *testing.T) {
q.Add("step", "10")
u.RawQuery = q.Encode()
r, err := http.NewRequest(method, u.String(), nil)
r, err := http.NewRequest(method, u.String(), http.NoBody)
if method == http.MethodPost {
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
@@ -3806,7 +3806,7 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, testLabelAPI
r.RemoteAddr = "127.0.0.1:20201"
return r, err
}
r, err := http.NewRequest(m, fmt.Sprintf("http://example.com?%s", q.Encode()), nil)
r, err := http.NewRequest(m, fmt.Sprintf("http://example.com?%s", q.Encode()), http.NoBody)
r.RemoteAddr = "127.0.0.1:20201"
return r, err
}
@@ -4119,7 +4119,7 @@ func TestAdminEndpoints(t *testing.T) {
}
endpoint := tc.endpoint(api)
req, err := http.NewRequest(tc.method, fmt.Sprintf("?%s", tc.values.Encode()), nil)
req, err := http.NewRequest(tc.method, fmt.Sprintf("?%s", tc.values.Encode()), http.NoBody)
require.NoError(t, err)
res := setUnavailStatusOnTSDBNotReady(endpoint(req))
@@ -4199,7 +4199,7 @@ func TestRespondSuccess(t *testing.T) {
},
} {
t.Run(tc.name, func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, s.URL, nil)
req, err := http.NewRequest(http.MethodGet, s.URL, http.NoBody)
require.NoError(t, err)
if tc.acceptHeader != "" {
@@ -4233,7 +4233,7 @@ func TestRespondSuccess_DefaultCodecCannotEncodeResponse(t *testing.T) {
}))
defer s.Close()
req, err := http.NewRequest(http.MethodGet, s.URL, nil)
req, err := http.NewRequest(http.MethodGet, s.URL, http.NoBody)
require.NoError(t, err)
resp, err := http.DefaultClient.Do(req)
@@ -4266,7 +4266,7 @@ func TestServeTSDBBlocks(t *testing.T) {
db: db,
}
req := httptest.NewRequest(http.MethodGet, "/api/v1/status/tsdb/blocks", nil)
req := httptest.NewRequest(http.MethodGet, "/api/v1/status/tsdb/blocks", http.NoBody)
w := httptest.NewRecorder()
result := api.serveTSDBBlocks(req)
@@ -4354,7 +4354,7 @@ func TestParseTimeParam(t *testing.T) {
}
for _, test := range tests {
req, err := http.NewRequest(http.MethodGet, "localhost:42/foo?"+test.paramName+"="+test.paramValue, nil)
req, err := http.NewRequest(http.MethodGet, "localhost:42/foo?"+test.paramName+"="+test.paramValue, http.NoBody)
require.NoError(t, err)
result := test.result
@@ -4491,7 +4491,7 @@ func TestOptionsMethod(t *testing.T) {
s := httptest.NewServer(r)
defer s.Close()
req, err := http.NewRequest(http.MethodOptions, s.URL+"/any_path", nil)
req, err := http.NewRequest(http.MethodOptions, s.URL+"/any_path", http.NoBody)
require.NoError(t, err, "Error creating OPTIONS request")
client := &http.Client{}
resp, err := client.Do(req)
@@ -4545,7 +4545,7 @@ func TestTSDBStatus(t *testing.T) {
t.Run(strconv.Itoa(i), func(t *testing.T) {
api := &API{db: tc.db, gatherer: prometheus.DefaultGatherer}
endpoint := tc.endpoint(api)
req, err := http.NewRequest(tc.method, fmt.Sprintf("?%s", tc.values.Encode()), nil)
req, err := http.NewRequest(tc.method, fmt.Sprintf("?%s", tc.values.Encode()), http.NoBody)
require.NoError(t, err, "Error when creating test request")
res := endpoint(req)
assertAPIError(t, res.err, tc.errType)
@@ -4638,7 +4638,7 @@ func BenchmarkRespond(b *testing.B) {
for _, c := range cases {
b.Run(c.name, func(b *testing.B) {
b.ReportAllocs()
request, err := http.NewRequest(http.MethodGet, "/does-not-matter", nil)
request, err := http.NewRequest(http.MethodGet, "/does-not-matter", http.NoBody)
require.NoError(b, err)
b.ResetTimer()
api := API{}
@@ -4873,7 +4873,7 @@ func TestQueryTimeout(t *testing.T) {
"timeout": []string{"1s"},
}
ctx := context.Background()
req, err := http.NewRequest(tc.method, fmt.Sprintf("http://example.com?%s", query.Encode()), nil)
req, err := http.NewRequest(tc.method, fmt.Sprintf("http://example.com?%s", query.Encode()), http.NoBody)
require.NoError(t, err)
req.RemoteAddr = "127.0.0.1:20201"

View File

@@ -110,7 +110,7 @@ func TestApiStatusCodes(t *testing.T) {
r := createPrometheusAPI(t, q, tc.overrideErrorCode)
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/api/v1/query?query=up", nil)
req := httptest.NewRequest(http.MethodGet, "/api/v1/query?query=up", http.NoBody)
r.ServeHTTP(rec, req)

View File

@@ -30,7 +30,7 @@ func TestOpenAPIHTTPHandler(t *testing.T) {
builder := NewOpenAPIBuilder(OpenAPIOptions{}, promslog.NewNopLogger())
// First request.
req1 := httptest.NewRequest(http.MethodGet, "/api/v1/openapi.yaml", nil)
req1 := httptest.NewRequest(http.MethodGet, "/api/v1/openapi.yaml", http.NoBody)
rec1 := httptest.NewRecorder()
builder.ServeOpenAPI(rec1, req1)
@@ -64,7 +64,7 @@ func TestOpenAPIHTTPHandler(t *testing.T) {
require.NotEmpty(t, paths, "paths should not be empty")
// Second request to verify response consistency.
req2 := httptest.NewRequest(http.MethodGet, "/api/v1/openapi.yaml", nil)
req2 := httptest.NewRequest(http.MethodGet, "/api/v1/openapi.yaml", http.NoBody)
rec2 := httptest.NewRecorder()
builder.ServeOpenAPI(rec2, req2)
@@ -118,7 +118,7 @@ func TestOpenAPIPathFiltering(t *testing.T) {
IncludePaths: tc.includePaths,
}, promslog.NewNopLogger())
req := httptest.NewRequest(http.MethodGet, "/api/v1/openapi.yaml", nil)
req := httptest.NewRequest(http.MethodGet, "/api/v1/openapi.yaml", http.NoBody)
rec := httptest.NewRecorder()
builder.ServeOpenAPI(rec, req)
@@ -147,7 +147,7 @@ func TestOpenAPIPathFiltering(t *testing.T) {
func TestOpenAPISchemaCompleteness(t *testing.T) {
builder := NewOpenAPIBuilder(OpenAPIOptions{}, promslog.NewNopLogger())
req := httptest.NewRequest(http.MethodGet, "/api/v1/openapi.yaml", nil)
req := httptest.NewRequest(http.MethodGet, "/api/v1/openapi.yaml", http.NoBody)
rec := httptest.NewRecorder()
builder.ServeOpenAPI(rec, req)
@@ -251,14 +251,14 @@ func TestOpenAPIVersionConsistency(t *testing.T) {
builder := NewOpenAPIBuilder(OpenAPIOptions{}, promslog.NewNopLogger())
// Fetch OpenAPI 3.1 spec (default).
req31 := httptest.NewRequest(http.MethodGet, "/api/v1/openapi.yaml", nil)
req31 := httptest.NewRequest(http.MethodGet, "/api/v1/openapi.yaml", http.NoBody)
rec31 := httptest.NewRecorder()
builder.ServeOpenAPI(rec31, req31)
require.Equal(t, http.StatusOK, rec31.Code)
// Fetch OpenAPI 3.2 spec.
req32 := httptest.NewRequest(http.MethodGet, "/api/v1/openapi.yaml?openapi_version=3.2", nil)
req32 := httptest.NewRequest(http.MethodGet, "/api/v1/openapi.yaml?openapi_version=3.2", http.NoBody)
rec32 := httptest.NewRecorder()
builder.ServeOpenAPI(rec32, req32)

View File

@@ -229,7 +229,7 @@ func TestFederation(t *testing.T) {
for name, scenario := range scenarios {
t.Run(name, func(t *testing.T) {
h.config.GlobalConfig.ExternalLabels = scenario.externalLabels
req := httptest.NewRequest(http.MethodGet, "http://example.org/federate?"+scenario.params, nil)
req := httptest.NewRequest(http.MethodGet, "http://example.org/federate?"+scenario.params, http.NoBody)
res := httptest.NewRecorder()
h.federation(res, req)
@@ -271,7 +271,7 @@ func TestFederation_NotReady(t *testing.T) {
options: &Options{Parser: testParser},
}
req := httptest.NewRequest(http.MethodGet, "http://example.org/federate?"+scenario.params, nil)
req := httptest.NewRequest(http.MethodGet, "http://example.org/federate?"+scenario.params, http.NoBody)
res := httptest.NewRecorder()
h.federation(res, req)
@@ -448,7 +448,7 @@ func TestFederationWithNativeHistograms(t *testing.T) {
options: &Options{Parser: testParser},
}
req := httptest.NewRequest(http.MethodGet, "http://example.org/federate?match[]=test_metric", nil)
req := httptest.NewRequest(http.MethodGet, "http://example.org/federate?match[]=test_metric", http.NoBody)
req.Header.Add("Accept", `application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=delimited,application/openmetrics-text;version=1.0.0;q=0.8,application/openmetrics-text;version=0.0.1;q=0.75,text/plain;version=0.0.4;q=0.5,*/*;q=0.1`)
res := httptest.NewRecorder()

View File

@@ -331,7 +331,7 @@ func TestDebugHandler(t *testing.T) {
w := httptest.NewRecorder()
req, err := http.NewRequest(http.MethodGet, tc.url, nil)
req, err := http.NewRequest(http.MethodGet, tc.url, http.NoBody)
require.NoError(t, err)
@@ -356,7 +356,7 @@ func TestHTTPMetrics(t *testing.T) {
t.Helper()
w := httptest.NewRecorder()
req, err := http.NewRequest(http.MethodGet, "/-/ready", nil)
req, err := http.NewRequest(http.MethodGet, "/-/ready", http.NoBody)
require.NoError(t, err)
handler.router.ServeHTTP(w, req)
@@ -592,7 +592,7 @@ func TestAgentAPIEndPoints(t *testing.T) {
"/admin/tsdb/snapshot": {http.MethodPost, http.MethodPut},
} {
for _, m := range methods {
req, err := http.NewRequest(m, baseURL+path, nil)
req, err := http.NewRequest(m, baseURL+path, http.NoBody)
require.NoError(t, err)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
@@ -613,7 +613,7 @@ func TestAgentAPIEndPoints(t *testing.T) {
"/status/flags": {http.MethodGet},
} {
for _, m := range methods {
req, err := http.NewRequest(m, baseURL+path, nil)
req, err := http.NewRequest(m, baseURL+path, http.NoBody)
require.NoError(t, err)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)