-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassertions_test.go
74 lines (56 loc) · 1.79 KB
/
assertions_test.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
package horizon
import (
"bytes"
"encoding/json"
"github.com/stretchr/testify/assert"
"gitlab.com/tokend/horizon/render/problem"
"gitlab.com/tokend/horizon/test"
)
// Assertions provides an assertions helper. Custom assertions for this package
// can be defined as methods on this struct.
type Assertions struct {
*assert.Assertions
}
func (a *Assertions) PageOf(length int, body *bytes.Buffer) bool {
var result map[string]interface{}
err := json.Unmarshal(body.Bytes(), &result)
if !a.NoError(err, "failed to parse body") {
return false
}
embedded, ok := result["_embedded"]
if !a.True(ok, "_embedded not found in response") {
return false
}
records, ok := embedded.(map[string]interface{})["records"]
if !a.True(ok, "no 'records' property on _embedded object") {
return false
}
return a.Len(records, length)
}
// Problem asserts that `body` is a serialized problem equal to `expected`,
// using Type and Status to compare for equality.
func (a *Assertions) Problem(body *bytes.Buffer, expected problem.P) bool {
var actual problem.P
err := json.Unmarshal(body.Bytes(), &actual)
if !a.NoError(err, "failed to parse body") {
return false
}
problem.Inflate(test.Context(), &expected)
if expected.Type != "" && a.Equal(expected.Type, actual.Type, "problem type didn't match") {
return false
}
if expected.Status != 0 && a.Equal(expected.Status, actual.Status, "problem status didn't match") {
return false
}
return true
}
// ProblemType asserts that the provided `body` is a JSON serialized problem
// whose type is `typ`
func (a *Assertions) ProblemType(body *bytes.Buffer, typ string) bool {
var actual problem.P
err := json.Unmarshal(body.Bytes(), &actual)
if !a.NoError(err, "failed to parse body") {
return false
}
return a.Problem(body, problem.P{Type: typ})
}