-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
51 lines (41 loc) · 1.34 KB
/
main_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
package main
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/golang/protobuf/proto"
"bytes"
"encoding/json"
"gin-proto-test/example"
. "github.com/smartystreets/goconvey/convey"
)
// protoc build code =>
// protoc HouseContractListInput.proto HouseContractListOutput.proto --go_out=../
func TestProtoMarshal(t *testing.T) {
Convey("Proto请求测试", t, func() {
router := setupRouter()
w := httptest.NewRecorder()
body := &example.HouseContractListInput{KeyWordName: "搜索"}
inData, _ := proto.Marshal(body)
req, _ := http.NewRequest("POST", "/test", bytes.NewReader(inData))
req.Header.Set("Content-Type", "application/x-protobuf")
router.ServeHTTP(w, req)
var output example.HouseContractListOutput
_ = proto.Unmarshal(w.Body.Bytes(), &output)
So(int(output.TotalRow), ShouldEqual, 2)
})
}
func TestJsonMarshal(t *testing.T) {
Convey("Json请求测试", t, func() {
router := setupRouter()
w := httptest.NewRecorder()
body := &example.HouseContractListInput{KeyWordName: "搜索"}
inData, _ := json.Marshal(body)
req, _ := http.NewRequest("POST", "/test", bytes.NewReader(inData))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
var output example.HouseContractListOutput
_ = json.Unmarshal(w.Body.Bytes(), &output)
So(2, ShouldEqual, int(output.TotalRow))
})
}