forked from ivpusic/httpcheck
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtester_body_json_test.go
88 lines (78 loc) · 1.7 KB
/
tester_body_json_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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package httpcheck
import (
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestTester_WithJSON(t *testing.T) {
mockT := new(testing.T)
checker := newTestChecker()
person := &testPerson{
Name: "Some",
Age: 30,
}
checker.Test(mockT, "GET", "/mirrorbody").
WithJSON(person).
Check().
HasJSON(person).
HasJSON(`{"Age":30, "Name":"Some"}`)
assert.False(t, mockT.Failed())
}
func TestHasJSON(t *testing.T) {
testdata := []struct {
name string
expected testPerson
want bool
}{
{
name: "OK",
expected: testPerson{
Name: "Some",
Age: 30,
},
want: false,
},
{
name: "NG",
expected: testPerson{
Name: "Unknown",
Age: 30,
},
want: true,
},
}
for _, tt := range testdata {
t.Run(tt.name, func(t *testing.T) {
mockT := new(testing.T)
checker := newTestChecker()
checker.Test(mockT, "GET", "/json").
Check().
HasJSON(tt.expected)
assert.Equal(t, tt.want, mockT.Failed())
})
}
t.Run("OK: json string", func(t *testing.T) {
checker := newTestChecker()
checker.Test(t, "GET", "/json").
Check().
HasJSON(`{"Age": 30, "Name": "Some"}`)
})
t.Run("NG: json string", func(t *testing.T) {
mockT := new(testing.T)
checker := newTestChecker()
checker.Test(mockT, "GET", "/json").
Check().
HasJSON(`{"Age": 20, "Name": "Some"}`)
assert.True(t, mockT.Failed())
})
}
func TestTester_MustHasJSON(t *testing.T) {
t.Skip("skip this test because it expects failure.")
checker := newTestChecker()
checker.Test(t, "GET", "/json").
Check().
MustHasJSON(`{"Age": 20, "Name": "Some"}`).
Cb(func(response *http.Response) {
t.Fatal("it is expected that this assertion will not be executed.")
})
}