From 7e48eb1eb2239d84cea8f59e5466fc7b1ae2f8de Mon Sep 17 00:00:00 2001 From: Anton Zadvorny Date: Mon, 26 Jun 2023 02:10:30 +0600 Subject: [PATCH] Fix lint errors --- auth/jwt/jwt.go | 7 +++---- auth/jwt/jwt_test.go | 16 +++++++-------- auth/token/token.go | 4 ++-- find_token_test.go | 14 ++++++------- logger/logger.go | 4 ++-- logger/tracking_response_writer.go | 2 +- no_cache_test.go | 2 +- ping.go | 2 +- real_ip_test.go | 8 ++++---- recoverer/recoverer.go | 2 +- throttle/throttle.go | 2 +- throttle/throttle_test.go | 32 +++++++++++++++--------------- timeout/timeout.go | 2 +- timeout/timeout_test.go | 3 ++- 14 files changed, 50 insertions(+), 50 deletions(-) diff --git a/auth/jwt/jwt.go b/auth/jwt/jwt.go index c1733da..a13032e 100644 --- a/auth/jwt/jwt.go +++ b/auth/jwt/jwt.go @@ -112,8 +112,7 @@ type auth struct { // Middleware returns jwt auth middleware func Middleware(key interface{}, alg jwa.SignatureAlgorithm, opts ...Option) func(next http.Handler) http.Handler { a := &auth{} - opts = append(opts, SetKey(key)) - opts = append(opts, SetAlgorithm(alg)) + opts = append(opts, SetKey(key), SetAlgorithm(alg)) opts = append([]Option{DefaultOptions}, opts...) for _, opt := range opts { @@ -162,10 +161,10 @@ func Middleware(key interface{}, alg jwa.SignatureAlgorithm, opts ...Option) fun } // RespondWithUnauthorized is a default response handler -func RespondWithUnauthorized(w http.ResponseWriter, r *http.Request, err error) { +func RespondWithUnauthorized(w http.ResponseWriter, _ *http.Request, _ error) { http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) } -func allowAll(token jwt.Token) (bool, interface{}) { +func allowAll(_ jwt.Token) (bool, interface{}) { return true, nil } diff --git a/auth/jwt/jwt_test.go b/auth/jwt/jwt_test.go index a6f7dcb..66af558 100644 --- a/auth/jwt/jwt_test.go +++ b/auth/jwt/jwt_test.go @@ -97,8 +97,8 @@ func TestJWTAuthParseVerify(t *testing.T) { assert.Equal(t, http.StatusUnauthorized, res.StatusCode) now := time.Now() - token.Set(jwt.IssuedAtKey, now.Add(-1*time.Hour)) // nolint:errcheck - token.Set(jwt.ExpirationKey, now.Add(-58*time.Minute)) // nolint:errcheck + token.Set(jwt.IssuedAtKey, now.Add(-1*time.Hour)) // nolint:errcheck // No need to check error here + token.Set(jwt.ExpirationKey, now.Add(-58*time.Minute)) // nolint:errcheck // No need to check error here payload, err = jwt.Sign(token, jwa.HS512, []byte("tkey")) assert.NoError(t, err) @@ -120,7 +120,7 @@ func TestJWTAuthVerifyOptions(t *testing.T) { token := jwt.New() - token.Set(jwt.IssuerKey, "wrongissuer") // nolint:errcheck + token.Set(jwt.IssuerKey, "wrongissuer") // nolint:errcheck // No need to check error here payload, err := jwt.Sign(token, jwa.HS256, []byte("changethis")) assert.NoError(t, err) @@ -129,7 +129,7 @@ func TestJWTAuthVerifyOptions(t *testing.T) { defer res.Body.Close() assert.Equal(t, http.StatusUnauthorized, res.StatusCode) - token.Set(jwt.IssuerKey, "tissuer") // nolint:errcheck + token.Set(jwt.IssuerKey, "tissuer") // nolint:errcheck // No need to check error here payload, err = jwt.Sign(token, jwa.HS256, []byte("changethis")) assert.NoError(t, err) @@ -155,16 +155,16 @@ func TestJWTAuthContext(t *testing.T) { ) token := jwt.New() - token.Set(jwt.JwtIDKey, "tid") // nolint:errcheck + token.Set(jwt.JwtIDKey, "tid") // nolint:errcheck // No need to check error here testCtxHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { data, ok := r.Context().Value(DataCtxKey).(*data) require.True(t, ok) assert.Equal(t, "test data", data.inner) - token, ok := r.Context().Value(JWTCtxKey).(jwt.Token) + ctxToken, ok := r.Context().Value(JWTCtxKey).(jwt.Token) require.True(t, ok) - assert.Equal(t, "tid", token.JwtID()) + assert.Equal(t, "tid", ctxToken.JwtID()) _, err := w.Write([]byte("resp")) require.NoError(t, err) @@ -181,7 +181,7 @@ func TestJWTAuthContext(t *testing.T) { defer res.Body.Close() assert.Equal(t, http.StatusOK, res.StatusCode) - token.Set(jwt.JwtIDKey, "invalid") // nolint:errcheck + token.Set(jwt.JwtIDKey, "invalid") // nolint:errcheck // No need to check error here payload, err = jwt.Sign(token, jwa.HS256, []byte("changethis")) assert.NoError(t, err) diff --git a/auth/token/token.go b/auth/token/token.go index 56c6fdb..bb8a5d8 100644 --- a/auth/token/token.go +++ b/auth/token/token.go @@ -114,10 +114,10 @@ func Middleware(opts ...Option) func(next http.Handler) http.Handler { } // RespondWithUnauthorized is a default response handler -func RespondWithUnauthorized(w http.ResponseWriter, r *http.Request, err error) { +func RespondWithUnauthorized(w http.ResponseWriter, _ *http.Request, _ error) { http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) } -func rejectAll(token string) (bool, interface{}) { +func rejectAll(_ string) (bool, interface{}) { return false, nil } diff --git a/find_token_test.go b/find_token_test.go index 8c0f7f9..485ee43 100644 --- a/find_token_test.go +++ b/find_token_test.go @@ -9,42 +9,42 @@ import ( ) func TestTokenFromAuthorizationHeader(t *testing.T) { - req, err := http.NewRequest("GET", "/", nil) + req, err := http.NewRequest("GET", "/", http.NoBody) require.Nil(t, err) req.Header.Set("Authorization", "abc") assert.Equal(t, "abc", TokenFromAuthorizationHeader(req)) - req, err = http.NewRequest("GET", "/", nil) + req, err = http.NewRequest("GET", "/", http.NoBody) require.Nil(t, err) req.Header.Set("Authorization", "abcdefghe") assert.Equal(t, "abcdefghe", TokenFromAuthorizationHeader(req)) - req, err = http.NewRequest("GET", "/", nil) + req, err = http.NewRequest("GET", "/", http.NoBody) require.Nil(t, err) req.Header.Set("Authorization", "Bearer abc") assert.Equal(t, "abc", TokenFromAuthorizationHeader(req)) } func TestTokenFromHeader(t *testing.T) { - req, err := http.NewRequest("GET", "/", nil) + req, err := http.NewRequest("GET", "/", http.NoBody) require.Nil(t, err) req.Header.Set("X-Token", "abc") assert.Equal(t, "abc", TokenFromHeader("X-Token")(req)) } func TestTokenFromQuery(t *testing.T) { - req, err := http.NewRequest("GET", "/?token=abc", nil) + req, err := http.NewRequest("GET", "/?token=abc", http.NoBody) require.Nil(t, err) assert.Equal(t, "abc", TokenFromQuery("token")(req)) } func TestTokenFromCookie(t *testing.T) { - req, err := http.NewRequest("GET", "/", nil) + req, err := http.NewRequest("GET", "/", http.NoBody) require.Nil(t, err) req.AddCookie(&http.Cookie{Name: "token", Value: "abc"}) assert.Equal(t, "abc", TokenFromCookie("token")(req)) - req, err = http.NewRequest("GET", "/", nil) + req, err = http.NewRequest("GET", "/", http.NoBody) require.Nil(t, err) assert.Equal(t, "", TokenFromCookie("token")(req)) } diff --git a/logger/logger.go b/logger/logger.go index cd6f9b1..64ee29d 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -113,7 +113,7 @@ func (s *logger) body(r *http.Request) string { r.Body = ioutil.NopCloser(rdr) if len(body) > 0 { - body = strings.Replace(body, "\n", " ", -1) + body = strings.ReplaceAll(body, "\n", " ") body = regexpMultiWhitespace.ReplaceAllString(body, " ") } @@ -186,7 +186,7 @@ func Middleware(opts ...Option) func(http.Handler) http.Handler { } // DefaultLogHandler is a default log handler -func DefaultLogHandler(entry LogEntry) { +func DefaultLogHandler(entry LogEntry) { // nolint:gocritic // For backwards compatibility log.Printf( "%s - %s - %s - %d (%d) - %v", entry.Method, diff --git a/logger/tracking_response_writer.go b/logger/tracking_response_writer.go index ae80f67..5854b37 100644 --- a/logger/tracking_response_writer.go +++ b/logger/tracking_response_writer.go @@ -8,7 +8,7 @@ import ( ) var ( - errWriterNotImplentsHijacker = errors.New("ResponseWriter does not implement the Hijacker interface") // nolint:golint + errWriterNotImplentsHijacker = errors.New("ResponseWriter does not implement the Hijacker interface") ) type trackingResponseWriter struct { diff --git a/no_cache_test.go b/no_cache_test.go index d848a35..59c1c0c 100644 --- a/no_cache_test.go +++ b/no_cache_test.go @@ -24,7 +24,7 @@ func TestNoCache(t *testing.T) { client := http.Client{} - req, err := http.NewRequest("GET", server.URL, nil) + req, err := http.NewRequest("GET", server.URL, http.NoBody) require.Nil(t, err) req.Header.Set("ETag", "ETagValue") diff --git a/ping.go b/ping.go index ee2f092..80a1b88 100644 --- a/ping.go +++ b/ping.go @@ -11,7 +11,7 @@ func Ping(next http.Handler) http.Handler { if r.Method == "GET" && strings.HasSuffix(strings.ToLower(r.URL.Path), "/ping") { w.Header().Set("Content-Type", "text/plain") w.WriteHeader(http.StatusOK) - w.Write([]byte("pong")) // nolint:errcheck + w.Write([]byte("pong")) // nolint:errcheck // No need to check error here return } diff --git a/real_ip_test.go b/real_ip_test.go index ffd31b1..673d1b9 100644 --- a/real_ip_test.go +++ b/real_ip_test.go @@ -26,7 +26,7 @@ func TestRealIPXRealIP(t *testing.T) { client := http.Client{} - req, err := http.NewRequest("GET", server.URL, nil) + req, err := http.NewRequest("GET", server.URL, http.NoBody) require.Nil(t, err) req.Header.Set("X-Real-IP", "3.3.3.3") @@ -46,7 +46,7 @@ func TestRealIPXForwardedFor(t *testing.T) { client := http.Client{} - req, err := http.NewRequest("GET", server.URL, nil) + req, err := http.NewRequest("GET", server.URL, http.NoBody) require.Nil(t, err) req.Header.Set("X-Forwarded-For", "3.3.3.3") @@ -55,7 +55,7 @@ func TestRealIPXForwardedFor(t *testing.T) { defer res.Body.Close() assert.Equal(t, http.StatusOK, res.StatusCode) - req, err = http.NewRequest("GET", server.URL, nil) + req, err = http.NewRequest("GET", server.URL, http.NoBody) require.Nil(t, err) req.Header.Set("X-Forwarded-For", "3.3.3.3, 4.4.4.4, 5.5.5.5") @@ -71,7 +71,7 @@ func TestRealIPBothHeaders(t *testing.T) { client := http.Client{} - req, err := http.NewRequest("GET", server.URL, nil) + req, err := http.NewRequest("GET", server.URL, http.NoBody) require.Nil(t, err) req.Header.Set("X-Real-IP", "3.3.3.3") req.Header.Set("X-Forwarded-For", "4.4.4.4") diff --git a/recoverer/recoverer.go b/recoverer/recoverer.go index 24d35d4..d8ba1fe 100644 --- a/recoverer/recoverer.go +++ b/recoverer/recoverer.go @@ -79,7 +79,7 @@ func Middleware(opts ...Option) func(next http.Handler) http.Handler { } // RespondWithInternalServerError is a default response handler -func RespondWithInternalServerError(w http.ResponseWriter, r *http.Request, err error) { +func RespondWithInternalServerError(w http.ResponseWriter, _ *http.Request, _ error) { http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) } diff --git a/throttle/throttle.go b/throttle/throttle.go index 541f8bd..55b7db5 100644 --- a/throttle/throttle.go +++ b/throttle/throttle.go @@ -171,6 +171,6 @@ func Middleware(opts ...Option) func(http.Handler) http.Handler { } // RespondWithTooManyRequests is a default response handler -func RespondWithTooManyRequests(w http.ResponseWriter, r *http.Request, err error) { +func RespondWithTooManyRequests(w http.ResponseWriter, _ *http.Request, _ error) { http.Error(w, http.StatusText(http.StatusTooManyRequests), http.StatusTooManyRequests) } diff --git a/throttle/throttle_test.go b/throttle/throttle_test.go index e931ff1..9d467c2 100644 --- a/throttle/throttle_test.go +++ b/throttle/throttle_test.go @@ -35,7 +35,7 @@ func TestThrottleBacklog(t *testing.T) { var wg sync.WaitGroup for i := 0; i < 1; i++ { wg.Add(1) - go func(i int) { + go func() { defer wg.Done() res, err := client.Get(server.URL) @@ -46,7 +46,7 @@ func TestThrottleBacklog(t *testing.T) { b, err := ioutil.ReadAll(res.Body) assert.NoError(t, err) assert.Equal(t, "resp", string(b)) - }(i) + }() } wg.Wait() @@ -75,12 +75,12 @@ func TestThrottleClientTimeout(t *testing.T) { var wg sync.WaitGroup for i := 0; i < 10; i++ { wg.Add(1) - go func(i int) { + go func() { defer wg.Done() _, err := client.Get(server.URL) assert.Error(t, err) - }(i) + }() } wg.Wait() @@ -110,7 +110,7 @@ func TestThrottleTriggerGatewayTimeout(t *testing.T) { for i := 0; i < 50; i++ { wg.Add(1) - go func(i int) { + go func() { defer wg.Done() res, err := client.Get(server.URL) @@ -118,21 +118,21 @@ func TestThrottleTriggerGatewayTimeout(t *testing.T) { defer res.Body.Close() assert.Equal(t, http.StatusOK, res.StatusCode) - }(i) + }() } time.Sleep(time.Second * 1) for i := 0; i < 50; i++ { wg.Add(1) - go func(i int) { + go func() { defer wg.Done() res, err := client.Get(server.URL) assert.NoError(t, err) defer res.Body.Close() assert.Equal(t, http.StatusTooManyRequests, res.StatusCode) - }(i) + }() } wg.Wait() @@ -162,7 +162,7 @@ func TestThrottleMaximum(t *testing.T) { for i := 0; i < 20; i++ { wg.Add(1) - go func(i int) { + go func() { defer wg.Done() res, err := client.Get(server.URL) @@ -173,21 +173,21 @@ func TestThrottleMaximum(t *testing.T) { b, err := ioutil.ReadAll(res.Body) assert.NoError(t, err) assert.Equal(t, "resp", string(b)) - }(i) + }() } time.Sleep(time.Second * 1) for i := 0; i < 20; i++ { wg.Add(1) - go func(i int) { + go func() { defer wg.Done() res, err := client.Get(server.URL) assert.NoError(t, err) defer res.Body.Close() assert.Equal(t, http.StatusTooManyRequests, res.StatusCode) - }(i) + }() } wg.Wait() @@ -216,21 +216,21 @@ func TestThrottleRetryAfter(t *testing.T) { for i := 0; i < 10; i++ { wg.Add(1) - go func(i int) { + go func() { defer wg.Done() res, err := client.Get(server.URL) assert.NoError(t, err) defer res.Body.Close() assert.Equal(t, http.StatusOK, res.StatusCode) - }(i) + }() } time.Sleep(time.Second * 1) for i := 0; i < 10; i++ { wg.Add(1) - go func(i int) { + go func() { defer wg.Done() res, err := client.Get(server.URL) @@ -238,7 +238,7 @@ func TestThrottleRetryAfter(t *testing.T) { defer res.Body.Close() assert.Equal(t, http.StatusTooManyRequests, res.StatusCode) assert.Equal(t, res.Header.Get("Retry-After"), "3600") - }(i) + }() } wg.Wait() diff --git a/timeout/timeout.go b/timeout/timeout.go index da4da93..5f8f849 100644 --- a/timeout/timeout.go +++ b/timeout/timeout.go @@ -76,6 +76,6 @@ func Middleware(opts ...Option) func(next http.Handler) http.Handler { } // RespondWithTimeout is a default response handler -func RespondWithTimeout(w http.ResponseWriter, r *http.Request, err error) { +func RespondWithTimeout(w http.ResponseWriter, _ *http.Request, _ error) { http.Error(w, http.StatusText(http.StatusGatewayTimeout), http.StatusGatewayTimeout) } diff --git a/timeout/timeout_test.go b/timeout/timeout_test.go index e67a1a7..a81dfa8 100644 --- a/timeout/timeout_test.go +++ b/timeout/timeout_test.go @@ -48,7 +48,8 @@ func TestTimeoutTimedOut(t *testing.T) { return case <-time.After(time.Second * 1): - w.Write([]byte("resp")) // nolint:errcheck + _, err := w.Write([]byte("resp")) + require.NoError(t, err) } })