This repository has been archived by the owner on Apr 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats_test.go
129 lines (118 loc) · 2.79 KB
/
stats_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package stats
import (
"bytes"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/vicanso/hes"
"github.com/stretchr/testify/assert"
"github.com/vicanso/elton"
)
func TestNoStatsPanic(t *testing.T) {
assert := assert.New(t)
done := false
defer func() {
r := recover()
assert.Equal(r.(error), errNoStatsFunction)
done = true
}()
New(Config{})
assert.True(done)
}
func TestSkip(t *testing.T) {
assert := assert.New(t)
fn := New(Config{
OnStats: func(info *Info, _ *elton.Context) {
},
})
c := elton.NewContext(nil, nil)
done := false
c.Next = func() error {
done = true
return nil
}
c.Committed = true
err := fn(c)
assert.Nil(err)
assert.True(done)
}
func TestStats(t *testing.T) {
t.Run("normal", func(t *testing.T) {
assert := assert.New(t)
req := httptest.NewRequest("GET", "http://127.0.0.1/users/me", nil)
resp := httptest.NewRecorder()
c := elton.NewContext(resp, req)
c.BodyBuffer = bytes.NewBufferString("abcd")
done := false
fn := New(Config{
OnStats: func(info *Info, _ *elton.Context) {
if info.Status != http.StatusOK {
t.Fatalf("status code should be 200")
}
done = true
},
})
c.Next = func() error {
return nil
}
err := fn(c)
assert.Nil(err)
assert.True(done)
})
t.Run("return hes error", func(t *testing.T) {
assert := assert.New(t)
req := httptest.NewRequest("GET", "http://127.0.0.1/users/me", nil)
resp := httptest.NewRecorder()
c := elton.NewContext(resp, req)
done := false
fn := New(Config{
OnStats: func(info *Info, _ *elton.Context) {
assert.Equal(info.Status, http.StatusBadRequest)
done = true
},
})
c.Next = func() error {
return hes.New("abc")
}
err := fn(c)
assert.NotNil(err)
assert.True(done, "on stats shouldn be called when return error")
})
t.Run("return normal error", func(t *testing.T) {
assert := assert.New(t)
req := httptest.NewRequest("GET", "http://127.0.0.1/users/me", nil)
resp := httptest.NewRecorder()
c := elton.NewContext(resp, req)
done := false
fn := New(Config{
OnStats: func(info *Info, _ *elton.Context) {
assert.Equal(info.Status, http.StatusInternalServerError)
done = true
},
})
c.Next = func() error {
return errors.New("abc")
}
err := fn(c)
assert.NotNil(err)
assert.True(done, "on stats shouldn be called when return error")
})
}
// 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)
}