fix: resolve all 47 noctx lint findings in tests
All checks were successful
Check / check (pull_request) Successful in 3m10s
All checks were successful
Check / check (pull_request) Successful in 3m10s
Replace every httptest.NewRequest call with httptest.NewRequestWithContext using the test's t.Context(). Thread t *testing.T through the createSetupFormRequest and createLoginFormRequest helpers so they can supply a context. make lint under golangci-lint 2.12.2 drops from 94 findings to 47 (remaining: 23 gosec, 24 goconst, tracked in #176/#177/#178). make test and make fmt-check pass unchanged. Closes #175
This commit is contained in:
@@ -47,7 +47,12 @@ func setupAPITest(t *testing.T) (*testContext, []*http.Cookie) {
|
||||
r := apiRouter(tc)
|
||||
|
||||
loginBody := `{"username":"admin","password":"password123"}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/login", strings.NewReader(loginBody))
|
||||
req := httptest.NewRequestWithContext(
|
||||
t.Context(),
|
||||
http.MethodPost,
|
||||
"/api/v1/login",
|
||||
strings.NewReader(loginBody),
|
||||
)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
@@ -70,7 +75,7 @@ func apiGet(
|
||||
) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, path, nil)
|
||||
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, path, nil)
|
||||
|
||||
for _, c := range cookies {
|
||||
req.AddCookie(c)
|
||||
@@ -95,7 +100,12 @@ func TestAPILoginSuccess(t *testing.T) {
|
||||
r := apiRouter(tc)
|
||||
|
||||
body := `{"username":"admin","password":"password123"}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/login", strings.NewReader(body))
|
||||
req := httptest.NewRequestWithContext(
|
||||
t.Context(),
|
||||
http.MethodPost,
|
||||
"/api/v1/login",
|
||||
strings.NewReader(body),
|
||||
)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
@@ -122,7 +132,12 @@ func TestAPILoginInvalidCredentials(t *testing.T) {
|
||||
r := apiRouter(tc)
|
||||
|
||||
body := `{"username":"admin","password":"wrong"}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/login", strings.NewReader(body))
|
||||
req := httptest.NewRequestWithContext(
|
||||
t.Context(),
|
||||
http.MethodPost,
|
||||
"/api/v1/login",
|
||||
strings.NewReader(body),
|
||||
)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
@@ -139,7 +154,12 @@ func TestAPILoginMissingFields(t *testing.T) {
|
||||
r := apiRouter(tc)
|
||||
|
||||
body := `{"username":"","password":""}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/login", strings.NewReader(body))
|
||||
req := httptest.NewRequestWithContext(
|
||||
t.Context(),
|
||||
http.MethodPost,
|
||||
"/api/v1/login",
|
||||
strings.NewReader(body),
|
||||
)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
@@ -155,7 +175,9 @@ func TestAPIRejectsUnauthenticated(t *testing.T) {
|
||||
|
||||
r := apiRouter(tc)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/apps", nil)
|
||||
req := httptest.NewRequestWithContext(
|
||||
t.Context(), http.MethodGet, "/api/v1/apps", nil,
|
||||
)
|
||||
rr := httptest.NewRecorder()
|
||||
r.ServeHTTP(rr, req)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user