diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml new file mode 100644 index 0000000..9f6dc60 --- /dev/null +++ b/.github/workflows/lint.yaml @@ -0,0 +1,21 @@ +name: Lint +on: [push, pull_request] +jobs: + lint: + strategy: + matrix: + go: + - stable + - oldstable + runs-on: ubuntu-latest + steps: + - name: Checkout Code + uses: actions/checkout@v3 + + - name: Setup Go ${{ matrix.go }} + uses: actions/setup-go@v4 + with: + go-version: ${{ matrix.go }} + + - name: Run golangci-lint + uses: golangci/golangci-lint-action@v3 diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..b1698dc --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,21 @@ +name: Test +on: [push, pull_request] +jobs: + lint: + strategy: + matrix: + go: + - stable + - oldstable + runs-on: ubuntu-latest + steps: + - name: Checkout Code + uses: actions/checkout@v3 + + - name: Setup Go ${{ matrix.go }} + uses: actions/setup-go@v4 + with: + go-version: ${{ matrix.go }} + + - name: Run tests + run: go test -v ./... diff --git a/exempt.go b/exempt.go index ee190d4..172a120 100644 --- a/exempt.go +++ b/exempt.go @@ -4,7 +4,6 @@ import ( "fmt" "net/http" pathModule "path" - "reflect" "regexp" ) @@ -79,13 +78,13 @@ func (h *CSRFHandler) ExemptGlobs(patterns ...string) { func (h *CSRFHandler) ExemptRegexp(re interface{}) { var compiled *regexp.Regexp - switch re.(type) { + switch re := re.(type) { case string: - compiled = regexp.MustCompile(re.(string)) + compiled = regexp.MustCompile(re) case *regexp.Regexp: - compiled = re.(*regexp.Regexp) + compiled = re default: - err := fmt.Sprintf("%v isn't a valid type for ExemptRegexp()", reflect.TypeOf(re)) + err := fmt.Errorf("%T is not a valid type for ExemptRegexp()", re) panic(err) } diff --git a/handler_go17.go b/handler_go17.go index 2d8ee9f..1c800ec 100644 --- a/handler_go17.go +++ b/handler_go17.go @@ -1,3 +1,4 @@ +//go:build go1.7 // +build go1.7 package nosurf diff --git a/handler_go17_test.go b/handler_go17_test.go index 09ae72b..2add203 100644 --- a/handler_go17_test.go +++ b/handler_go17_test.go @@ -1,3 +1,4 @@ +//go:build go1.7 // +build go1.7 package nosurf @@ -9,11 +10,13 @@ import ( "testing" ) +type dummyKeyType struct{} + // Confusing test name. Tests that nosurf's context is accessible // when a request with golang's context is passed into Token(). func TestContextIsAccessibleWithContext(t *testing.T) { succHand := func(w http.ResponseWriter, r *http.Request) { - r = r.WithContext(context.WithValue(r.Context(), "dummykey", "dummyval")) + r = r.WithContext(context.WithValue(r.Context(), dummyKeyType{}, "dummyval")) token := Token(r) if token == "" { t.Errorf("Token is inaccessible in the success handler") diff --git a/handler_test.go b/handler_test.go index 128a09b..e8c2c91 100644 --- a/handler_test.go +++ b/handler_test.go @@ -334,16 +334,21 @@ func TestCorrectTokenPasses(t *testing.T) { go func() { for _, v := range vals { - wr.WriteField(v[0], v[1]) + err := wr.WriteField(v[0], v[1]) + if err != nil { + t.Error(err) + return + } } err := wr.Close() if err != nil { - t.Fatal(err) + t.Error(err) + return } err = pwr.Close() if err != nil { - t.Fatal(err) + t.Error(err) } }() diff --git a/testutils_test.go b/testutils_test.go index 0c4fc8b..18e0a1e 100644 --- a/testutils_test.go +++ b/testutils_test.go @@ -27,7 +27,7 @@ func dummyGet() *http.Request { func succHand(w http.ResponseWriter, r *http.Request) { w.WriteHeader(200) - w.Write([]byte("success")) + _, _ = w.Write([]byte("success")) } // Returns a HandlerFunc diff --git a/utils_test.go b/utils_test.go index d651ffb..6c522f5 100644 --- a/utils_test.go +++ b/utils_test.go @@ -5,7 +5,7 @@ import ( "testing" ) -func TestsContains(t *testing.T) { +func TestSContains(t *testing.T) { slice := []string{"abc", "def", "ghi"} s1 := "abc" @@ -19,7 +19,7 @@ func TestsContains(t *testing.T) { } } -func TestsameOrigin(t *testing.T) { +func TestSameOrigin(t *testing.T) { // a little helper that saves us time p := func(rawurl string) *url.URL { u, err := url.Parse(rawurl)