-
Notifications
You must be signed in to change notification settings - Fork 0
/
response_test.go
149 lines (121 loc) · 3.41 KB
/
response_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package golden
import (
"io/ioutil"
"net/http"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
. "github.com/rzajac/golden/internal"
)
func Test_Response(t *testing.T) {
// --- When ---
gld := NewResponse(Open(t, "testdata/response.yaml", nil))
// --- Then ---
assert.Exactly(t, 200, gld.StatusCode)
exp := []string{
"Authorization: Bearer token",
"Content-Type: application/json",
}
assert.Exactly(t, exp, gld.Headers)
assert.Exactly(t, "{ \"key2\": \"val2\" }\n", gld.Body)
assert.Exactly(t, "val1", gld.Meta["key1"])
assert.Exactly(t, 123, gld.Meta["key2"])
assert.Exactly(t, 12.3, gld.Meta["key3"])
expDate := time.Date(2021, 2, 28, 10, 24, 25, 123000000, time.UTC)
assert.Exactly(t, expDate, gld.Meta["key4"].(time.Time))
}
func Test_Response_Assert(t *testing.T) {
// --- Given ---
body := `{"key2":"val2"}`
rsp := &http.Response{
Header: make(http.Header),
}
rsp.StatusCode = 200
rsp.Header.Add("Authorization", "Bearer token")
rsp.Header.Add("Content-Type", "application/json")
rsp.Body = ioutil.NopCloser(strings.NewReader(body))
// --- When ---
gld := NewResponse(Open(t, "testdata/response.yaml", nil))
// --- Then ---
gld.Assert(rsp)
}
func Test_Response_Assert_diff(t *testing.T) {
// --- Given ---
body := `{"key1":"val1","key3":"val3","key4":"val4"}`
rsp := &http.Response{
Header: make(http.Header),
}
rsp.StatusCode = 200
rsp.Body = ioutil.NopCloser(strings.NewReader(body))
mck := &TMock{}
mck.On("Helper")
mck.On("Fatal", mock.AnythingOfType("string"))
gld := NewResponse(Open(mck, "testdata/response2.yaml", nil))
// --- When ---
gld.Assert(rsp)
// --- Then ---
mck.AssertExpectations(t)
}
func Test_Response_Assert_HeaderDoesNotMatch(t *testing.T) {
// --- Given ---
mck := &TMock{}
mck.On("Helper")
mck.On(
"Fatalf",
"expected response header %s values %v got %v",
"Authorization",
[]string{"Bearer token"},
[]string{"Bearer token 2"},
)
body := `{"key2":"val2"}`
rsp := &http.Response{
Header: make(http.Header),
}
rsp.StatusCode = 200
rsp.Header.Add("Authorization", "Bearer token 2")
rsp.Header.Add("Content-Type", "application/json")
rsp.Body = ioutil.NopCloser(strings.NewReader(body))
// --- When ---
gld := NewResponse(Open(mck, "testdata/response.yaml", nil))
// --- Then ---
gld.Assert(rsp)
}
func Test_Response_Assert_OnlyDefinedHeadersChecked(t *testing.T) {
// --- Given ---
mck := &TMock{}
mck.On("Helper")
body := `{"key2":"val2"}`
rsp := &http.Response{
Header: make(http.Header),
}
rsp.StatusCode = 200
rsp.Header.Add("Authorization", "Bearer token")
rsp.Header.Add("Content-Type", "application/json")
rsp.Header.Add("Custom-Header", "custom data")
rsp.Body = ioutil.NopCloser(strings.NewReader(body))
// --- When ---
gld := NewResponse(Open(mck, "testdata/response.yaml", nil))
// --- Then ---
gld.Assert(rsp)
}
func Test_Response_Unmarshal(t *testing.T) {
// --- Given ---
gld := NewResponse(Open(t, "testdata/response.yaml", nil))
// --- When ---
m := make(map[string]string, 1)
gld.Unmarshal(&m)
// --- Then ---
require.Len(t, m, 1)
require.Contains(t, m, "key2")
assert.Exactly(t, "val2", m["key2"])
}
func Test_Response_Bytes(t *testing.T) {
// --- When ---
gld := NewResponse(Open(t, "testdata/response.yaml", nil))
// --- Then ---
exp := []byte("{ \"key2\": \"val2\" }\n")
assert.Exactly(t, exp, gld.Bytes())
}