Skip to content

Commit

Permalink
Add GitHub Actions CI, fix lints and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
justinas committed Apr 6, 2024
1 parent 4d86df7 commit e5c9c1f
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 13 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -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 ./...
9 changes: 4 additions & 5 deletions exempt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"net/http"
pathModule "path"
"reflect"
"regexp"
)

Expand Down Expand Up @@ -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)
}

Expand Down
1 change: 1 addition & 0 deletions handler_go17.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build go1.7
// +build go1.7

package nosurf
Expand Down
5 changes: 4 additions & 1 deletion handler_go17_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build go1.7
// +build go1.7

package nosurf
Expand All @@ -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")
Expand Down
11 changes: 8 additions & 3 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}()

Expand Down
2 changes: 1 addition & 1 deletion testutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"
)

func TestsContains(t *testing.T) {
func TestSContains(t *testing.T) {
slice := []string{"abc", "def", "ghi"}

s1 := "abc"
Expand All @@ -14,12 +14,12 @@ func TestsContains(t *testing.T) {
}

s2 := "xyz"
if !sContains(slice, s2) {
if sContains(slice, s2) {
t.Errorf("sContains said that %v contains %v, but it doesn't.", slice, s2)
}
}

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)
Expand Down

0 comments on commit e5c9c1f

Please sign in to comment.