-
Notifications
You must be signed in to change notification settings - Fork 32
/
json.go
111 lines (95 loc) · 2.21 KB
/
json.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
package assert
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"reflect"
)
func unmarshal(buf []byte) (map[string]interface{}, error) {
data := make(map[string]interface{})
if err := json.Unmarshal(buf, &data); err != nil {
return data, fmt.Errorf("failed to Unmarshal: %s", err)
}
return data, nil
}
func unmarshalBody(res *http.Response) (map[string]interface{}, error) {
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
if len(body) == 0 {
return nil, nil
}
return unmarshal(body)
}
func marshal(data map[string]interface{}) ([]byte, error) {
return json.Marshal(data)
}
func readBodyJSON(res *http.Response) ([]byte, error) {
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return []byte{}, err
}
// Re-fill body reader stream after reading it
res.Body = ioutil.NopCloser(bytes.NewBuffer(body))
return body, err
}
func compare(body map[string]interface{}, data interface{}) error {
var err error
var match map[string]interface{}
// Infer and cast string input
if jsonStr, ok := data.(string); ok {
data = []byte(jsonStr)
}
// Infer and cast string input
if reader, ok := data.(io.Reader); ok {
data, err = ioutil.ReadAll(reader)
if err != nil {
return err
}
}
// Infer and cast input as map
if fields, ok := data.(map[string]interface{}); ok {
data, err = marshal(fields)
if err != nil {
return err
}
}
// Infer and cast bytes input
buf, ok := data.([]byte)
if ok {
match, err = unmarshal(buf)
if err != nil {
return err
}
}
// Assert via string
bodyBuf, err := marshal(body)
if err != nil {
return err
}
matchBuf, err := marshal(match)
if err != nil {
return err
}
// Compare by byte sequences
if !reflect.DeepEqual(bodyBuf, matchBuf) {
return fmt.Errorf("failed due to JSON mismatch:\n\thave: %#v\n\twant: %#v\n", string(bodyBuf), string(matchBuf))
}
return nil
}
// JSON deeply and strictly compares the JSON
// response body with the given JSON structure.
func JSON(data interface{}) Func {
return func(res *http.Response, req *http.Request) error {
// Read and unmarshal response body as JSON
body, err := unmarshalBody(res)
if err != nil {
return err
}
return compare(body, data)
}
}