-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathgout_test.go
112 lines (86 loc) · 1.98 KB
/
gout_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
package gout
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/guonaihong/gout/dataflow"
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
)
func TestDebug(t *testing.T) {
server := func() *gin.Engine {
r := gin.New()
r.GET("/", func(c *gin.Context) {
all, err := ioutil.ReadAll(c.Request.Body)
assert.NoError(t, err)
c.String(200, string(all))
})
return r
}()
ts := httptest.NewServer(http.HandlerFunc(server.ServeHTTP))
test := []func() DebugOpt{
// 没有颜色输出
NoColor,
Trace,
}
s := ""
for k, v := range test {
s = ""
err := GET(ts.URL).
Debug(v()).
SetBody(fmt.Sprintf("%d test debug.", k)).
BindBody(&s).
Do()
assert.NoError(t, err)
assert.Equal(t, fmt.Sprintf("%d test debug.", k), s)
}
}
func TestNew(t *testing.T) {
c := &http.Client{}
tests := []*dataflow.Gout{
New(nil),
New(),
New(c),
}
for _, v := range tests {
assert.NotNil(t, v)
}
}
func setupMethod(total *int32) *gin.Engine {
router := gin.Default()
cb := func(c *gin.Context) {
atomic.AddInt32(total, 1)
}
router.GET("/someGet", cb)
router.POST("/somePost", cb)
router.PUT("/somePut", cb)
router.DELETE("/someDelete", cb)
router.PATCH("/somePatch", cb)
router.HEAD("/someHead", cb)
router.OPTIONS("/someOptions", cb)
return router
}
func TestTopMethod(t *testing.T) {
var total int32
router := setupMethod(&total)
ts := httptest.NewServer(http.HandlerFunc(router.ServeHTTP))
defer ts.Close()
err := GET(ts.URL + "/someGet").Do()
assert.NoError(t, err)
err = POST(ts.URL + "/somePost").Do()
assert.NoError(t, err)
err = PUT(ts.URL + "/somePut").Do()
assert.NoError(t, err)
err = DELETE(ts.URL + "/someDelete").Do()
assert.NoError(t, err)
err = PATCH(ts.URL + "/somePatch").Do()
assert.NoError(t, err)
err = HEAD(ts.URL + "/someHead").Do()
assert.NoError(t, err)
err = OPTIONS(ts.URL + "/someOptions").Do()
assert.NoError(t, err)
assert.Equal(t, int(total), 7)
}