diff --git a/.travis.yml b/.travis.yml index b39eb17..9352cec 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,6 @@ language: go sudo: required go: - - "1.11" - "1.12" - master diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..f06b593 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "go.formatTool": "goimports" +} \ No newline at end of file diff --git a/go.mod b/go.mod index ad3134d..91de102 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,8 @@ module github.com/vicanso/cod-tracker -require github.com/vicanso/cod v0.0.7 +go 1.12 + +require ( + github.com/stretchr/testify v1.3.0 + github.com/vicanso/cod v0.1.1 +) diff --git a/go.sum b/go.sum index a0c2e2b..4bc57c8 100644 --- a/go.sum +++ b/go.sum @@ -7,8 +7,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/vicanso/cod v0.0.7 h1:CHyIL7zkNOhEv0JosDDyRHCUALovry1P+dLISaANj1w= -github.com/vicanso/cod v0.0.7/go.mod h1:tHGTs/JDTcp/OE4VaCutVMpYL69aR1rKUggsHviTuFc= +github.com/vicanso/cod v0.1.1 h1:l4dbZSGhGRVnrxLbArC/60GXNxDo0O/RlY5z4iHSa7I= +github.com/vicanso/cod v0.1.1/go.mod h1:T5GOazXuYrwwE1qMA0G3zka7NVwwkoL2fYIWNfeEpJw= github.com/vicanso/hes v0.1.4 h1:n8kG8krvNJF4Sj1PvZOEUzdUsmDSbCcGr8C1nYnoP+o= github.com/vicanso/hes v0.1.4/go.mod h1:bG0UJ3EihDKObFcLLwJYjxHHr9saFllsFcepyDIvFlo= github.com/vicanso/keygrip v0.1.0 h1:/zYzoVIbREAvaxSM7bo3/oSXuuYztaP71dPBfhRoNkM= diff --git a/tracker.go b/tracker.go index d48b93b..42960cc 100644 --- a/tracker.go +++ b/tracker.go @@ -16,6 +16,7 @@ package tracker import ( "encoding/json" + "errors" "regexp" "github.com/vicanso/cod" @@ -29,7 +30,8 @@ const ( ) var ( - defaultMaskFields = regexp.MustCompile(`password`) + defaultMaskFields = regexp.MustCompile(`password`) + errNoTrackFunction = errors.New("require on track function") ) type ( @@ -74,7 +76,7 @@ func New(config Config) cod.Handler { mask = defaultMaskFields } if config.OnTrack == nil { - panic("require on track function") + panic(errNoTrackFunction) } skipper := config.Skipper if skipper == nil { diff --git a/tracker_test.go b/tracker_test.go index c5ae8db..077ce3f 100644 --- a/tracker_test.go +++ b/tracker_test.go @@ -2,23 +2,51 @@ package tracker import ( "errors" + "fmt" "net/http/httptest" + "os" "testing" + "github.com/stretchr/testify/assert" "github.com/vicanso/cod" ) +func TestNoTrackPanic(t *testing.T) { + assert := assert.New(t) + done := false + defer func() { + r := recover() + assert.Equal(r.(error), errNoTrackFunction) + done = true + }() + + New(Config{}) + assert.True(done) +} + +func TestConverMap(t *testing.T) { + assert := assert.New(t) + assert.Nil(convertMap(nil, nil)) + assert.Equal(convertMap(map[string]string{ + "password": "123", + "foo": "bar", + }, defaultMaskFields), map[string]string{ + "foo": "bar", + "password": "***", + }) +} + func TestTracker(t *testing.T) { + assert := assert.New(t) + customErr := errors.New("abcd") done := false fn := New(Config{ OnTrack: func(info *Info, _ *cod.Context) { - if info.Result != HandleFail || - info.Query["type"] != "1" || - info.Query["passwordType"] != "***" || - info.Params["category"] != "login" || - info.Form["password"] != "***" { - t.Fatalf("tracker info is invalid") - } + assert.Equal(info.Result, HandleFail) + assert.Equal(info.Query["type"], "1") + assert.Equal(info.Query["passwordType"], "***") + assert.Equal(info.Params["category"], "login") + assert.Equal(info.Form["password"], "***") done = true }, }) @@ -32,10 +60,26 @@ func TestTracker(t *testing.T) { "category": "login", } c.Next = func() error { - return errors.New("abcd") + return customErr } - fn(c) - if !done { - t.Fatalf("tracker middleware fail") + err := fn(c) + assert.Equal(err, customErr) + assert.True(done, "tracker middleware fail") +} + +// https://stackoverflow.com/questions/50120427/fail-unit-tests-if-coverage-is-below-certain-percentage +func TestMain(m *testing.M) { + // call flag.Parse() here if TestMain uses flags + rc := m.Run() + + // rc 0 means we've passed, + // and CoverMode will be non empty if run with -cover + if rc == 0 && testing.CoverMode() != "" { + c := testing.Coverage() + if c < 0.9 { + fmt.Println("Tests passed but coverage failed at", c) + rc = -1 + } } + os.Exit(rc) }