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 7e51b7d commit c84976a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 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"
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module github.com/vicanso/cod-recover

go 1.12

require (
github.com/vicanso/cod v0.0.7
github.com/stretchr/testify v1.3.0
github.com/vicanso/cod v0.1.1
github.com/vicanso/hes v0.1.4
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0
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
33 changes: 25 additions & 8 deletions recover_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package recover

import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/vicanso/cod"
)

func TestRecover(t *testing.T) {
assert := assert.New(t)
var ctx *cod.Context
d := cod.New()
d.Use(New())
Expand All @@ -34,15 +38,28 @@ func TestRecover(t *testing.T) {
})

d.ServeHTTP(resp, req)
if resp.Code != http.StatusInternalServerError ||
resp.Body.String() != "category=cod-recover, message=abc" ||
!ctx.Committed ||
!catchError {
t.Fatalf("recover fail")
}
assert.Equal(resp.Code, http.StatusInternalServerError)
assert.Equal(resp.Body.String(), "category=cod-recover, message=abc")
assert.True(ctx.Committed)
assert.True(catchError)
for _, key := range keys {
if resp.Header().Get(key) != "" {
t.Fatalf("reset response header fail")
assert.Empty(ctx.GetHeader(key), "header should be reseted")
}
}

// 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 c84976a

Please sign in to comment.