-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathsetprotobuf_test.go
64 lines (49 loc) · 1.25 KB
/
setprotobuf_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
package gout
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"github.com/guonaihong/gout/testdata"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/proto"
"github.com/gin-gonic/gin"
)
func setupEcho(total *int32, t *testing.T) *gin.Engine {
router := gin.Default()
cb := func(c *gin.Context) {
atomic.AddInt32(total, 1)
_, err := io.Copy(c.Writer, c.Request.Body)
assert.NoError(t, err)
}
router.GET("/echo", cb)
return router
}
func Test_SetProtoBuf(t *testing.T) {
total := int32(0)
router := setupEcho(&total, t)
ts := httptest.NewServer(http.HandlerFunc(router.ServeHTTP))
defer ts.Close()
data1, err := proto.Marshal(&testdata.Req{Seq: 1, Res: "fk"})
assert.NoError(t, err)
testCount := 0
for i, v := range []interface{}{
data1,
&testdata.Req{Seq: 1, Res: "fk"},
} {
code := 0
var gotRes []byte
err := GET(ts.URL + "/echo").SetProtoBuf(v).BindBody(&gotRes).Code(&code).Do()
assert.NoError(t, err)
assert.Equal(t, code, 200)
got := &testdata.Req{}
err = proto.Unmarshal(gotRes, got)
assert.NoError(t, err)
assert.Equal(t, got.Seq, int32(1), fmt.Sprintf("fail index:%d", i))
assert.Equal(t, got.Res, "fk")
testCount++
}
assert.Equal(t, total, int32(testCount))
}