-
-
Notifications
You must be signed in to change notification settings - Fork 384
/
util.go
274 lines (252 loc) · 6.69 KB
/
util.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package testdb
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"reflect"
"regexp"
"strconv"
"strings"
"go.keploy.io/server/v2/pkg"
"go.keploy.io/server/v2/pkg/models"
"go.keploy.io/server/v2/pkg/platform/yaml"
"go.keploy.io/server/v2/utils"
"go.uber.org/zap"
)
func EncodeTestcase(tc models.TestCase, logger *zap.Logger) (*yaml.NetworkTrafficDoc, error) {
header := pkg.ToHTTPHeader(tc.HTTPReq.Header)
curl := pkg.MakeCurlCommand(string(tc.HTTPReq.Method), tc.HTTPReq.URL, pkg.ToYamlHTTPHeader(header), tc.HTTPReq.Body)
doc := &yaml.NetworkTrafficDoc{
Version: tc.Version,
Kind: tc.Kind,
Name: tc.Name,
Curl: curl,
}
// find noisy fields
m, err := FlattenHTTPResponse(pkg.ToHTTPHeader(tc.HTTPResp.Header), tc.HTTPResp.Body)
if err != nil {
msg := "error in flattening http response"
utils.LogError(logger, err, msg)
}
noise := tc.Noise
noiseFieldsFound := FindNoisyFields(m, func(_ string, vals []string) bool {
// check if k is date
for _, v := range vals {
if pkg.IsTime(v) {
return true
}
}
// maybe we need to concatenate the values
return pkg.IsTime(strings.Join(vals, ", "))
})
for _, v := range noiseFieldsFound {
noise[v] = []string{}
}
switch tc.Kind {
case models.HTTP:
err := doc.Spec.Encode(models.HTTPSchema{
Request: tc.HTTPReq,
Response: tc.HTTPResp,
Created: tc.Created,
Assertions: map[string]interface{}{
"noise": noise,
},
})
if err != nil {
utils.LogError(logger, err, "failed to encode testcase into a yaml doc")
return nil, err
}
default:
utils.LogError(logger, nil, "failed to marshal the testcase into yaml due to invalid kind of testcase")
return nil, errors.New("type of testcases is invalid")
}
return doc, nil
}
func FindNoisyFields(m map[string][]string, comparator func(string, []string) bool) []string {
var noise []string
for k, v := range m {
if comparator(k, v) {
noise = append(noise, k)
}
}
return noise
}
func FlattenHTTPResponse(h http.Header, body string) (map[string][]string, error) {
m := map[string][]string{}
for k, v := range h {
m["header."+k] = []string{strings.Join(v, "")}
}
err := AddHTTPBodyToMap(body, m)
if err != nil {
return m, err
}
return m, nil
}
func AddHTTPBodyToMap(body string, m map[string][]string) error {
// add body
if json.Valid([]byte(body)) {
var result interface{}
err := json.Unmarshal([]byte(body), &result)
if err != nil {
return err
}
j := Flatten(result)
for k, v := range j {
nk := "body"
if k != "" {
nk = nk + "." + k
}
m[nk] = v
}
} else {
// add it as raw text
m["body"] = []string{body}
}
return nil
}
// Flatten takes a map and returns a new one where nested maps are replaced
// by dot-delimited keys.
// examples of valid jsons - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#examples
func Flatten(j interface{}) map[string][]string {
if j == nil {
return map[string][]string{"": {""}}
}
o := make(map[string][]string)
x := reflect.ValueOf(j)
switch x.Kind() {
case reflect.Map:
m, ok := j.(map[string]interface{})
if !ok {
return map[string][]string{}
}
for k, v := range m {
nm := Flatten(v)
for nk, nv := range nm {
fk := k
if nk != "" {
fk = fk + "." + nk
}
o[fk] = nv
}
}
case reflect.Bool:
o[""] = []string{strconv.FormatBool(x.Bool())}
case reflect.Float64:
o[""] = []string{strconv.FormatFloat(x.Float(), 'E', -1, 64)}
case reflect.String:
o[""] = []string{x.String()}
case reflect.Slice:
child, ok := j.([]interface{})
if !ok {
return map[string][]string{}
}
for _, av := range child {
nm := Flatten(av)
for nk, nv := range nm {
if ov, exists := o[nk]; exists {
o[nk] = append(ov, nv...)
} else {
o[nk] = nv
}
}
}
default:
fmt.Println(utils.Emoji, "found invalid value in json", j, x.Kind())
}
return o
}
func ContainsMatchingURL(urlMethods []string, urlStr string, requestURL string, requestMethod models.Method) (bool, error) {
urlMatched := false
parsedURL, err := url.Parse(requestURL)
if err != nil {
return false, err
}
// Check for URL path and method
regex, err := regexp.Compile(urlStr)
if err != nil {
return false, err
}
urlMatch := regex.MatchString(parsedURL.Path)
if urlMatch && len(urlStr) != 0 {
urlMatched = true
}
if len(urlMethods) != 0 && urlMatched {
urlMatched = false
for _, method := range urlMethods {
if string(method) == string(requestMethod) {
urlMatched = true
}
}
}
return urlMatched, nil
}
func HasBannedHeaders(object map[string]string, bannedHeaders map[string]string) (bool, error) {
for headerName, headerNameValue := range object {
for bannedHeaderName, bannedHeaderValue := range bannedHeaders {
regex, err := regexp.Compile(headerName)
if err != nil {
return false, err
}
headerNameMatch := regex.MatchString(bannedHeaderName)
regex, err = regexp.Compile(bannedHeaderValue)
if err != nil {
return false, err
}
headerValueMatch := regex.MatchString(headerNameValue)
if headerNameMatch && headerValueMatch {
return true, nil
}
}
}
return false, nil
}
func Decode(yamlTestcase *yaml.NetworkTrafficDoc, logger *zap.Logger) (*models.TestCase, error) {
tc := models.TestCase{
Version: yamlTestcase.Version,
Kind: yamlTestcase.Kind,
Name: yamlTestcase.Name,
Curl: yamlTestcase.Curl,
}
switch tc.Kind {
case models.HTTP:
httpSpec := models.HTTPSchema{}
err := yamlTestcase.Spec.Decode(&httpSpec)
if err != nil {
utils.LogError(logger, err, "failed to unmarshal a yaml doc into the http testcase")
return nil, err
}
tc.Created = httpSpec.Created
tc.HTTPReq = httpSpec.Request
tc.HTTPResp = httpSpec.Response
tc.Noise = map[string][]string{}
switch reflect.ValueOf(httpSpec.Assertions["noise"]).Kind() {
case reflect.Map:
for k, v := range httpSpec.Assertions["noise"].(map[string]interface{}) {
tc.Noise[k] = []string{}
for _, val := range v.([]interface{}) {
tc.Noise[k] = append(tc.Noise[k], val.(string))
}
}
case reflect.Slice:
for _, v := range httpSpec.Assertions["noise"].([]interface{}) {
tc.Noise[v.(string)] = []string{}
}
}
// unmarshal its mocks from yaml docs to go struct
case models.GRPC_EXPORT:
grpcSpec := models.GrpcSpec{}
err := yamlTestcase.Spec.Decode(&grpcSpec)
if err != nil {
utils.LogError(logger, err, "failed to unmarshal a yaml doc into the gRPC testcase")
return nil, err
}
tc.GrpcReq = grpcSpec.GrpcReq
tc.GrpcResp = grpcSpec.GrpcResp
default:
utils.LogError(logger, nil, "failed to unmarshal yaml doc of unknown type", zap.Any("type of yaml doc", tc.Kind))
return nil, errors.New("yaml doc of unknown type")
}
return &tc, nil
}