Skip to content

Commit

Permalink
chore(lint): add golang lint config
Browse files Browse the repository at this point in the history
Signed-off-by: Bo-Yi Wu <[email protected]>
  • Loading branch information
appleboy committed Jan 1, 2022
1 parent 693ce4a commit 6fb0a95
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
43 changes: 43 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 7 additions & 8 deletions cors_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cors

import (
"context"
"net/http"
"net/http/httptest"
"strings"
Expand All @@ -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))
Expand All @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
})
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 6fb0a95

Please sign in to comment.