Skip to content
This repository has been archived by the owner on Apr 11, 2020. It is now read-only.

Commit

Permalink
test: update test
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Jun 7, 2019
1 parent 4973d02 commit 90bc605
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 17 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ language: go
sudo: required

go:
- "1.11"
- "1.12"
- master

Expand Down
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"go.formatTool": "goimports"
}
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
6 changes: 4 additions & 2 deletions tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package tracker

import (
"encoding/json"
"errors"
"regexp"

"github.com/vicanso/cod"
Expand All @@ -29,7 +30,8 @@ const (
)

var (
defaultMaskFields = regexp.MustCompile(`password`)
defaultMaskFields = regexp.MustCompile(`password`)
errNoTrackFunction = errors.New("require on track function")
)

type (
Expand Down Expand Up @@ -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 {
Expand Down
66 changes: 55 additions & 11 deletions tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
})
Expand All @@ -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)
}

0 comments on commit 90bc605

Please sign in to comment.