-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrecorder_test.go
121 lines (94 loc) · 3 KB
/
recorder_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
package gorr
import (
"encoding/json"
"fmt"
"github.com/golang/protobuf/jsonpb"
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"os"
"strings"
"testing"
"time"
)
func TestRecordHttp(t *testing.T) {
curTm := time.Now()
SetUnixTime(curTm)
SetTimeIncDelta(0)
HookTimeNow()
*RegressionOutputDir = "/tmp"
body1 := "miliao-http-test-data-set"
req, err := http.NewRequest("POST", "http://some-no-exist.url.com", strings.NewReader(body1))
assert.Nil(t, err)
req.Header.Add("Content-Type", "application/json")
body2 := "miliao-http-test-data-set"
rsp := &http.Response{
Body: ioutil.NopCloser(strings.NewReader(body2)),
}
db1 := "./reg_db1.db"
db2 := "./reg_db2.db"
ioutil.WriteFile(db1, []byte("ddddd"), 0644)
ioutil.WriteFile(db2, []byte("ddddd"), 0644)
var out string
outDir := createOutputDir("cases")
out, err = RecordHttp(outDir, "miliao_http_test", req, rsp, []string{db1, db2})
assert.Nil(t, err)
reqData, err1 := ioutil.ReadFile(out + "/reg_req_http.dat")
rspData, err2 := ioutil.ReadFile(out + "/reg_rsp_http.dat")
assert.Nil(t, err1)
assert.Nil(t, err2)
assert.Equal(t, body1, string(reqData))
assert.Equal(t, body2, string(rspData))
os.Remove(db1)
os.Remove(db2)
var ti TestItem
conf, err3 := ioutil.ReadFile(out + "/reg_config.json")
assert.Nil(t, err3)
err = json.Unmarshal(conf, &ti)
assert.Nil(t, err)
assert.Equal(t, 2, len(ti.DB))
assert.Equal(t, 3, len(ti.Flags))
assert.Equal(t, 1, len(ti.TestCases))
assert.Equal(t, "-gorr_run_type=2", ti.Flags[0])
assert.Equal(t, fmt.Sprintf("-server_time=%s", curTm.Format(time.RFC3339)), ti.Flags[1])
assert.Equal(t, []string{"reg_db1.db", "reg_db2.db"}, ti.DB)
assert.Equal(t, &TestCase{Req: "reg_req_http.dat", Rsp: "reg_rsp_http.dat", Desc: "miliao_http_test", ReqType: RecorderDataTypeJson, RspType: RecorderDataTypeJson}, ti.TestCases[0])
os.RemoveAll(out)
}
func TestRecordGrpc(t *testing.T) {
*RegressionOutputDir = "/tmp"
req := &GrpcHookRequest{
ReqId: 2333,
ReqName: "miliao-test-grpc-record",
ReqData: "12345678900987654321qwertyuioplkjhgfdsazxcvbnm",
}
rsp := &GrpcHookResponse{
RspId: 45678,
ReqId: 12345,
RspName: "miliao-test-for-response",
RspData: "9999999999999999999999999999999999999999999999999999999999",
}
db1 := "./reg_db1.db"
db2 := "./reg_db2.db"
ioutil.WriteFile(db1, []byte("ddddd"), 0644)
ioutil.WriteFile(db2, []byte("ddddd"), 0644)
var out string
outDir := createOutputDir("cases")
out, err := RecordGrpc(outDir, "miliao_test_grpc_record", req, rsp, []string{db1, db2})
assert.Nil(t, err)
os.Remove(db1)
os.Remove(db2)
reqData, err1 := ioutil.ReadFile(out + "/reg_req_grpc.dat")
rspData, err2 := ioutil.ReadFile(out + "/reg_rsp_grpc.dat")
assert.Nil(t, err1)
assert.Nil(t, err2)
req2 := &GrpcHookRequest{}
rsp2 := &GrpcHookResponse{}
err1 = json.Unmarshal(reqData, req2)
err2 = jsonpb.UnmarshalString(string(rspData), rsp2)
assert.Nil(t, err1)
assert.Nil(t, err2)
assert.Equal(t, req, req2)
assert.Equal(t, rsp, rsp2)
assert.Nil(t, os.RemoveAll(out))
}