forked from danielgtaylor/openapi-cli-generator
-
Notifications
You must be signed in to change notification settings - Fork 5
/
matcher.go
160 lines (137 loc) · 3.72 KB
/
matcher.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
package cli
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"strings"
jmespath "github.com/danielgtaylor/go-jmespath-plus"
"github.com/rs/zerolog/log"
"gopkg.in/h2non/gentleman.v2/context"
)
// The following equality functions are from stretchr/testify/assert.
// objectsAreEqual determines if two objects are considered equal.
func objectsAreEqual(expected, actual interface{}) bool {
if expected == nil || actual == nil {
return expected == actual
}
exp, ok := expected.([]byte)
if !ok {
return reflect.DeepEqual(expected, actual)
}
act, ok := actual.([]byte)
if !ok {
return false
}
if exp == nil || act == nil {
return exp == nil && act == nil
}
return bytes.Equal(exp, act)
}
// objectsAreEqualValues gets whether two objects are equal, or if their
// values are equal.
func objectsAreEqualValues(expected, actual interface{}) bool {
if objectsAreEqual(expected, actual) {
return true
}
actualType := reflect.TypeOf(actual)
if actualType == nil {
return false
}
expectedValue := reflect.ValueOf(expected)
if expectedValue.IsValid() && expectedValue.Type().ConvertibleTo(actualType) {
// Attempt comparison after type conversion
return reflect.DeepEqual(expectedValue.Convert(actualType).Interface(), actual)
}
return false
}
// GetMatchValue returns a value for the given selector query.
func GetMatchValue(ctx *context.Context, selector string, reqParams map[string]interface{}, decoded interface{}) (interface{}, error) {
parts := strings.Split(selector, "#")
base := parts[0]
args := strings.Join(parts[1:], "#")
l := log.Debug().Str("selector", base)
if args != "" {
l = l.Str("query", args)
}
var actual interface{}
switch base {
case "request.param":
actual = reqParams[args]
case "request.body":
var decoded interface{}
if err := UnmarshalRequest(ctx, &decoded); err != nil {
return nil, err
}
result, err := jmespath.Search(args, decoded)
if err != nil {
return nil, err
}
actual = result
case "response.status":
actual = ctx.Response.StatusCode
case "response.header":
actual = ctx.Response.Header.Get(args)
case "response.body":
// Perform a JMESPath query to match the expected value.
result, err := jmespath.Search(args, decoded)
if err != nil {
return nil, err
}
actual = result
default:
return false, fmt.Errorf("Cannot match selector: %s", selector)
}
l.Interface("actual", actual).Msg("Found matcher value")
return actual, nil
}
// Match returns `true` if the expected value of the match type is found in the
// given response data.
func Match(test string, expected json.RawMessage, actual interface{}) (bool, error) {
var exp interface{}
if err := json.Unmarshal(expected, &exp); err != nil {
return false, err
}
var matches bool
switch test {
case "equal":
if objectsAreEqualValues(exp, actual) {
matches = true
}
case "any":
if list, ok := actual.([]interface{}); ok {
// We have a list of items, so at least one must match.
matches = false
for _, item := range list {
if objectsAreEqualValues(exp, item) {
matches = true
break
}
}
} else {
return false, fmt.Errorf("Expected a list but got: %v", actual)
}
case "all":
if list, ok := actual.([]interface{}); ok {
// We have a list of items, so each one must match the expected value.
matches = true
for _, item := range list {
if !objectsAreEqualValues(exp, item) {
matches = false
break
}
}
} else {
return false, fmt.Errorf("Expected a list but got: %v", actual)
}
default:
return false, fmt.Errorf("Unknown test: %s", test)
}
l := log.Debug().Str("test", test).Interface("expected", exp).Interface("actual", actual)
if matches {
l.Msg("Found match")
} else {
l.Msg("No match")
}
return matches, nil
}