Fix lint errors

This commit is contained in:
Anton Zadvorny 2023-06-26 02:10:30 +06:00
parent 393801368b
commit 7e48eb1eb2
14 changed files with 50 additions and 50 deletions

View File

@ -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
}

View File

@ -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)

View File

@ -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
}

View File

@ -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))
}

View File

@ -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,

View File

@ -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 {

View File

@ -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")

View File

@ -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
}

View File

@ -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")

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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()

View File

@ -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)
}

View File

@ -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)
}
})