/
endpoint-mock-compare.go
254 lines (194 loc) · 7.11 KB
/
endpoint-mock-compare.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package service
import (
"fmt"
queryUtils "github.com/aaronchen2k/deeptest/internal/agent/exec/utils/query"
"github.com/aaronchen2k/deeptest/internal/pkg/consts"
httpHelper "github.com/aaronchen2k/deeptest/internal/pkg/helper/http"
"github.com/aaronchen2k/deeptest/internal/server/modules/model"
"github.com/aaronchen2k/deeptest/internal/server/modules/repo"
"github.com/tidwall/gjson"
"regexp"
"strconv"
"strings"
)
type EndpointMockCompareService struct {
EndpointInterfaceRepo *repo.EndpointInterfaceRepo `inject:""`
EndpointRepo *repo.EndpointRepo `inject:""`
}
func (s *EndpointMockCompareService) CompareBody(expectRequest model.EndpointMockExpectRequest, contentType consts.HttpContentType,
body string, bodyForm map[string][]string) (ret bool) {
if httpHelper.IsJsonContent(contentType.String()) { // json
if expectRequest.SelectType == consts.KeyValue {
expectValue := expectRequest.Value
var actualValue interface{}
actualJson := gjson.Get(body, expectRequest.Name) // get value of key on first level
typ := actualJson.Type
if typ == gjson.Number {
actualValue = actualJson.Float()
} else {
actualValue = actualJson.String()
}
ret = s.compareObject(actualValue, expectValue, expectRequest.CompareWay)
} else if expectRequest.SelectType == consts.Xpath { // use xpath
xpath := expectRequest.Name
expectValue := expectRequest.Value
actualValue := queryUtils.JsonQueryWithType(body, xpath)
ret = s.compareObject(actualValue, expectValue, expectRequest.CompareWay)
} else if expectRequest.SelectType == consts.FullText || expectRequest.SelectType == "" {
expectValue := expectRequest.Value
actualValue := body
ret = s.compareObject(actualValue, expectValue, expectRequest.CompareWay)
}
} else if contentType == consts.ContentTypeFormData {
if expectRequest.SelectType == consts.KeyValue { // key/value
expectValue := expectRequest.Value
items := bodyForm[expectRequest.Name]
for _, item := range items {
actualValue := item
result := s.compareObject(actualValue, expectValue, expectRequest.CompareWay)
if result {
return true
}
}
}
} else if contentType == consts.ContentTypeFormUrlencoded {
if expectRequest.SelectType == consts.KeyValue { // key/value
expectValue := expectRequest.Value
items := bodyForm[expectRequest.Name]
for _, item := range items {
actualValue := item
result := s.compareObject(actualValue, expectValue, expectRequest.CompareWay)
if result {
return true
}
}
}
} else { // xml, html, text etc.
if expectRequest.SelectType == consts.FullText {
expectValue := expectRequest.Value
actualValue := body
ret = s.compareObject(actualValue, expectValue, expectRequest.CompareWay)
}
}
return
}
func (s *EndpointMockCompareService) CompareString(actualValue interface{}, expectValue string,
comparator consts.ComparisonOperator) (ret bool) {
if comparator == consts.Equal {
ret = fmt.Sprintf("%v", actualValue) == expectValue
} else if comparator == consts.NotEqual {
ret = fmt.Sprintf("%v", actualValue) != expectValue
} else if comparator == consts.Contain {
ret = strings.Contains(fmt.Sprintf("%v", actualValue), expectValue)
} else if comparator == consts.NotContain {
ret = !strings.Contains(fmt.Sprintf("%v", actualValue), expectValue)
} else if comparator == consts.RegularMatch {
regx := regexp.MustCompile(expectValue)
ret = regx.MatchString(fmt.Sprintf("%v", actualValue))
} else if comparator == consts.GreaterThan {
actualFloat, err2 := s.interfaceToNumber(actualValue)
expectFloat, err1 := s.strToNumber(expectValue)
if err1 != nil || err2 != nil {
return false
}
ret = actualFloat > expectFloat
} else if comparator == consts.GreaterThanOrEqual {
actualFloat, err2 := s.interfaceToNumber(actualValue)
expectFloat, err1 := s.strToNumber(expectValue)
if err1 != nil || err2 != nil {
return false
}
ret = actualFloat >= expectFloat
} else if comparator == consts.LessThan {
actualFloat, err2 := s.interfaceToNumber(actualValue)
expectFloat, err1 := s.strToNumber(expectValue)
if err1 != nil || err2 != nil {
return false
}
ret = actualFloat < expectFloat
} else if comparator == consts.LessThanOrEqual {
actualFloat, err2 := s.interfaceToNumber(actualValue)
expectFloat, err1 := s.strToNumber(expectValue)
if err1 != nil || err2 != nil {
return false
}
ret = actualFloat <= expectFloat
}
return
}
func (s *EndpointMockCompareService) compareObject(actualValue interface{}, expectValue string, comparator consts.ComparisonOperator) (ret bool) {
isFloat := s.isFloat(actualValue)
if comparator == consts.Equal {
if isFloat {
actualFloat, expectFloat, err := s.getFloatValues(actualValue, expectValue)
ret = err == nil && actualFloat == expectFloat
} else {
ret = fmt.Sprintf("%v", actualValue) == expectValue
}
} else if comparator == consts.NotEqual {
if isFloat {
actualFloat, expectFloat, err := s.getFloatValues(actualValue, expectValue)
ret = err == nil && actualFloat != expectFloat
} else {
ret = fmt.Sprintf("%v", actualValue) != expectValue
}
} else if comparator == consts.GreaterThan {
actualFloat, err2 := s.interfaceToNumber(actualValue)
expectFloat, err1 := s.strToNumber(expectValue)
if err1 != nil || err2 != nil {
return false
}
ret = actualFloat > expectFloat
} else if comparator == consts.GreaterThanOrEqual {
actualFloat, err2 := s.interfaceToNumber(actualValue)
expectFloat, err1 := s.strToNumber(expectValue)
if err1 != nil || err2 != nil {
return false
}
ret = actualFloat >= expectFloat
} else if comparator == consts.LessThan {
actualFloat, err2 := s.interfaceToNumber(actualValue)
expectFloat, err1 := s.strToNumber(expectValue)
if err1 != nil || err2 != nil {
return false
}
ret = actualFloat < expectFloat
} else if comparator == consts.LessThanOrEqual {
actualFloat, err2 := s.interfaceToNumber(actualValue)
expectFloat, err1 := s.strToNumber(expectValue)
if err1 != nil || err2 != nil {
return false
}
ret = actualFloat <= expectFloat
} else if comparator == consts.Contain {
ret = strings.Contains(fmt.Sprintf("%v", actualValue), expectValue)
} else if comparator == consts.NotContain {
ret = !strings.Contains(fmt.Sprintf("%v", actualValue), expectValue)
} else if comparator == consts.RegularMatch {
regx := regexp.MustCompile(expectValue)
ret = regx.MatchString(fmt.Sprintf("%v", actualValue))
}
return
}
func (s *EndpointMockCompareService) strToNumber(val string) (ret float64, err error) {
ret, err = strconv.ParseFloat(val, 64)
return
}
func (s *EndpointMockCompareService) interfaceToNumber(val interface{}) (ret float64, err error) {
str := fmt.Sprintf("%v", val)
ret, err = s.strToNumber(str)
return
}
func (s *EndpointMockCompareService) getFloatValues(actual interface{}, expect string) (actualFloat, expectFloat float64, err error) {
actualFloat = actual.(float64)
expectFloat, err = strconv.ParseFloat(expect, 64)
return
}
func (s *EndpointMockCompareService) isFloat(val interface{}) (ret bool) {
switch val.(type) {
case float64:
return true
default:
}
return false
}