-
Notifications
You must be signed in to change notification settings - Fork 43
/
response.go
75 lines (61 loc) · 1.69 KB
/
response.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"strings"
"time"
)
type Response struct {
client *Client
Request *Request
ID requestID
Body []byte
Err error
Elapsed time.Duration
}
func (r *Response) Equal(other Response) bool {
if r.Err == nil && other.Err == nil {
// TODO: Use github.com/nsf/jsondiff to detect subsets and for pretty printing diffs?
return bytes.Equal(r.Body, other.Body) || jsonEqual(r.Body, other.Body)
}
if r.Err != nil && other.Err != nil {
return r.Err.Error() == other.Err.Error() && bytes.Equal(r.Body, other.Body)
}
return false
}
type Responses []Response
func (resps Responses) String() string {
var buf strings.Builder
// TODO: Sort before printing
last := resps[0]
for i, resp := range resps {
fmt.Fprintf(&buf, "\t%s", resp.Elapsed)
if resp.Err == nil && last.Err == nil {
if !bytes.Equal(resp.Body, last.Body) {
fmt.Fprintf(&buf, "[%d: body mismatch:\n%s\n\t%s\n%s\n\t%s]", i, resp.client.Endpoint, resp.Body, last.client.Endpoint, last.Body)
}
} else if resp.Err != nil && last.Err != nil && resp.Err.Error() != last.Err.Error() {
fmt.Fprintf(&buf, "[%d: error mismatch: %s != %s]", i, resp.Err, last.Err)
} else {
fmt.Fprintf(&buf, "[%d: error mismatch: %s != %s]", i, resp.Err, last.Err)
}
}
return buf.String()
}
// jsonEqual returns true if a and b are JSON objects (starting with '{') and equal.
func jsonEqual(a, b []byte) bool {
if len(a) == 0 || a[0] != '{' {
return false
}
// Normalize a and b
var aObj, bObj interface{}
if err := json.Unmarshal(a, &aObj); err != nil {
return false
}
if err := json.Unmarshal(b, &bObj); err != nil {
return false
}
return reflect.DeepEqual(aObj, bObj)
}