-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
184 lines (156 loc) · 3.81 KB
/
helpers.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
package httpexpect
import (
"encoding/json"
"fmt"
"reflect"
"regexp"
"github.com/xeipuuv/gojsonschema"
"github.com/yalp/jsonpath"
"github.com/yudai/gojsondiff"
"github.com/yudai/gojsondiff/formatter"
)
func toString(str interface{}) (s string, ok bool) {
ok = true
defer func() {
if err := recover(); err != nil {
ok = false
}
}()
s = reflect.ValueOf(str).Convert(reflect.TypeOf("")).String()
return
}
func getPath(chain *chain, value interface{}, path string) *Value {
if chain.failed() {
return &Value{*chain, nil}
}
result, err := jsonpath.Read(value, path)
if err != nil {
chain.fail(err.Error())
return &Value{*chain, nil}
}
return &Value{*chain, result}
}
func checkSchema(chain *chain, value, schema interface{}) {
if chain.failed() {
return
}
valueLoader := gojsonschema.NewGoLoader(value)
var schemaLoader gojsonschema.JSONLoader
if str, ok := toString(schema); ok {
if ok, _ := regexp.MatchString(`^\w+://`, str); ok {
schemaLoader = gojsonschema.NewReferenceLoader(str)
} else {
schemaLoader = gojsonschema.NewStringLoader(str)
}
} else {
schemaLoader = gojsonschema.NewGoLoader(schema)
}
result, err := gojsonschema.Validate(schemaLoader, valueLoader)
if err != nil {
chain.fail("\n%s\n\nschema:\n%s\n\nvalue:\n%s",
err.Error(),
dumpSchema(schema),
dumpValue(value))
return
}
if !result.Valid() {
errors := ""
for _, err := range result.Errors() {
errors += fmt.Sprintf(" %s\n", err)
}
chain.fail(
"\njson schema validation failed, schema:\n%s\n\nvalue:%s\n\nerrors:\n%s",
dumpSchema(schema),
dumpValue(value),
errors)
return
}
}
func dumpSchema(schema interface{}) string {
if s, ok := toString(schema); ok {
schema = s
}
return regexp.MustCompile(`(?m:^)`).
ReplaceAllString(fmt.Sprintf("%v", schema), " ")
}
func canonNumber(chain *chain, number interface{}) (f float64, ok bool) {
ok = true
defer func() {
if err := recover(); err != nil {
chain.fail("%v", err)
ok = false
}
}()
f = reflect.ValueOf(number).Convert(reflect.TypeOf(float64(0))).Float()
return
}
func canonArray(chain *chain, in interface{}) ([]interface{}, bool) {
var out []interface{}
data, ok := canonValue(chain, in)
if ok {
out, ok = data.([]interface{})
if !ok {
chain.fail("expected array, got %v", out)
}
}
return out, ok
}
func canonMap(chain *chain, in interface{}) (map[string]interface{}, bool) {
var out map[string]interface{}
data, ok := canonValue(chain, in)
if ok {
out, ok = data.(map[string]interface{})
if !ok {
chain.fail("expected map, got %v", out)
}
}
return out, ok
}
func canonValue(chain *chain, in interface{}) (interface{}, bool) {
b, err := json.Marshal(in)
if err != nil {
chain.fail(err.Error())
return nil, false
}
var out interface{}
if err := json.Unmarshal(b, &out); err != nil {
chain.fail(err.Error())
return nil, false
}
return out, true
}
func dumpValue(value interface{}) string {
b, err := json.MarshalIndent(value, " ", " ")
if err != nil {
return " " + fmt.Sprintf("%#v", value)
}
return " " + string(b)
}
func diffValues(expected, actual interface{}) string {
differ := gojsondiff.New()
var diff gojsondiff.Diff
if ve, ok := expected.(map[string]interface{}); ok {
if va, ok := actual.(map[string]interface{}); ok {
diff = differ.CompareObjects(ve, va)
} else {
return " (unavailable)"
}
} else if ve, ok := expected.([]interface{}); ok {
if va, ok := actual.([]interface{}); ok {
diff = differ.CompareArrays(ve, va)
} else {
return " (unavailable)"
}
} else {
return " (unavailable)"
}
config := formatter.AsciiFormatterConfig{
ShowArrayIndex: true,
}
formatter := formatter.NewAsciiFormatter(expected, config)
str, err := formatter.Format(diff)
if err != nil {
return " (unavailable)"
}
return "--- expected\n+++ actual\n" + str
}