forked from h2non/baloo
/
json.go
105 lines (90 loc) · 2.17 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
package assert
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"reflect"
)
func unmarshal(buf []byte) (interface{}, error) {
var data 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) (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 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 interface{}, data interface{}) error {
var err error
var matchBytes []byte
// taking pointer to json.RawMessage due to regression in go 1.7 (https://github.com/golang/go/issues/14493)
var matchData interface{}
switch data := data.(type) {
case json.Marshaler:
matchData = data
case string:
v := json.RawMessage([]byte(data))
matchData = &v
case []byte:
v := json.RawMessage(data)
matchData = &v
default:
matchData = data
}
matchBytes, err = marshal(matchData)
if err != nil {
return err
}
// Assert via string
bodyBytes, err := marshal(body)
if err != nil {
return err
}
bodyValue, err := unmarshal(bodyBytes)
if err != nil {
return err
}
matchValue, err := unmarshal(matchBytes)
if err != nil {
return err
}
// Compare values so order of keys in maps does not influence the result
if !reflect.DeepEqual(bodyValue, matchValue) {
return fmt.Errorf("failed due to JSON mismatch:\n\thave: %#v\n\twant: %#v", string(bodyBytes), string(matchBytes))
}
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)
}
}