From 6fb0a95dc529663cf3702a96e659d043609c0ed4 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sat, 1 Jan 2022 20:36:31 +0800 Subject: [PATCH] chore(lint): add golang lint config Signed-off-by: Bo-Yi Wu --- .golangci.yml | 43 +++++++++++++++++++++++++++++++++++++++++++ cors.go | 2 +- cors_test.go | 15 +++++++-------- 3 files changed, 51 insertions(+), 9 deletions(-) create mode 100644 .golangci.yml diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..5a0031c --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,43 @@ +linters: + enable-all: false + disable-all: true + fast: false + enable: + - bodyclose + - deadcode + - depguard + - dogsled + - dupl + - errcheck + - exportloopref + - exhaustive + - gochecknoinits + - goconst + - gocritic + - gocyclo + - gofmt + - goimports + - goprintffuncname + - gosec + - gosimple + - govet + - ineffassign + - lll + - misspell + - nakedret + - noctx + - nolintlint + - rowserrcheck + - staticcheck + - structcheck + - stylecheck + - typecheck + - unconvert + - unparam + - unused + - varcheck + - whitespace + - gofumpt + +run: + timeout: 3m diff --git a/cors.go b/cors.go index 6827535..135423b 100644 --- a/cors.go +++ b/cors.go @@ -38,7 +38,7 @@ type Config struct { // API specification ExposeHeaders []string - // MaxAge indicates how long (with second-precision) the results of a preflight request + // MaxAge indicates how long (with second-precision) the results of a preflight request // can be cached MaxAge time.Duration diff --git a/cors_test.go b/cors_test.go index abce415..17ee3d5 100644 --- a/cors_test.go +++ b/cors_test.go @@ -1,6 +1,7 @@ package cors import ( + "context" "net/http" "net/http/httptest" "strings" @@ -11,10 +12,6 @@ import ( "github.com/stretchr/testify/assert" ) -func init() { - gin.SetMode(gin.TestMode) -} - func newTestRouter(config Config) *gin.Engine { router := gin.New() router.Use(New(config)) @@ -35,7 +32,7 @@ func performRequest(r http.Handler, method, origin string) *httptest.ResponseRec } func performRequestWithHeaders(r http.Handler, method, origin string, header http.Header) *httptest.ResponseRecorder { - req, _ := http.NewRequest(method, "/", nil) + req, _ := http.NewRequestWithContext(context.Background(), method, "/", nil) // From go/net/http/request.go: // For incoming requests, the Host header is promoted to the // Request.Host field and removed from the Header map. @@ -67,7 +64,6 @@ func TestConfigAddAllow(t *testing.T) { assert.Equal(t, config.AllowMethods, []string{"POST", "GET", "PUT"}) assert.Equal(t, config.AllowHeaders, []string{"Some", " cool", "header"}) assert.Equal(t, config.ExposeHeaders, []string{"exposed", "header", "hey"}) - } func TestBadConfig(t *testing.T) { @@ -231,7 +227,11 @@ func TestValidateOrigin(t *testing.T) { assert.False(t, cors.validateOrigin("wss://socket-connection")) cors = newCors(Config{ - AllowOrigins: []string{"chrome-extension://*", "safari-extension://my-extension-*-app", "*.some-domain.com"}, + AllowOrigins: []string{ + "chrome-extension://*", + "safari-extension://my-extension-*-app", + "*.some-domain.com", + }, AllowBrowserExtensions: true, AllowWildcard: true, }) @@ -364,7 +364,6 @@ func TestPassesAllowAllOrigins(t *testing.T) { assert.Equal(t, "Content-Type,Testheader", w.Header().Get("Access-Control-Allow-Headers")) assert.Equal(t, "36000", w.Header().Get("Access-Control-Max-Age")) assert.Empty(t, w.Header().Get("Access-Control-Allow-Credentials")) - } func TestWildcard(t *testing.T) {