/
body_json_query_equal.go
60 lines (50 loc) · 1.34 KB
/
body_json_query_equal.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
package check
import (
"context"
"fmt"
"github.com/tidwall/gjson"
"net/http"
"reflect"
)
// UnexpectedJSONQueryValueError is returned when a check fails.
type UnexpectedJSONQueryValueError struct {
// Query is the JSON query.
Query string
// Expected is the expected value.
Expected interface{}
// Actual is the actual value.
Actual interface{}
}
// Error returns an error string.
func (e *UnexpectedJSONQueryValueError) Error() string {
return fmt.Sprintf("unexpected value at %v: expected %v, got %v", e.Query, e.Expected, e.Actual)
}
// BodyJSONQueryEqualChecker queries the http response body JSON using `Query` and ensures the value is equal to `Value`
type BodyJSONQueryEqualChecker struct {
Query string
Value interface{}
NullValue bool
DataID string
}
// Check performs the BodyJSONQueryEqual check
func (c *BodyJSONQueryEqualChecker) Check(ctx context.Context, response *http.Response) error {
body, err := readResponseBody(response)
if err != nil {
return err
}
j := gjson.ParseBytes(body)
r := j.Get(c.Query)
if !r.Exists() {
return &JSONQueryValueMissingError{
Query: c.Query,
}
}
if got := r.Value(); !reflect.DeepEqual(c.Value, got) {
return &UnexpectedJSONQueryValueError{
Query: c.Query,
Expected: c.Value,
Actual: got,
}
}
return ContextWithOptionalDataID(ctx, c.DataID, r.Value())
}