-
Notifications
You must be signed in to change notification settings - Fork 0
/
response_recorder_test.go
129 lines (97 loc) · 2.49 KB
/
response_recorder_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
package httpinfo
import (
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestResponseRecorder_WriteHeader(t *testing.T) {
var (
rw responseWriterMock
rr = responseRecorder{writer: &rw}
status = http.StatusCreated
)
rr.WriteHeader(status)
assert.True(t, rr.statusSet)
assert.False(t, rr.statusWrote)
assert.Equal(t, status, rr.status)
rw.AssertNotCalled(t, "WriteHeader", status)
rw.AssertExpectations(t)
}
func TestResponseRecorder_Write(t *testing.T) {
var (
rw responseWriterMock
rr = responseRecorder{writer: &rw}
status = http.StatusCreated
b = []byte("hello world")
)
rr.WriteHeader(status)
rw.On("WriteHeader", status).Once()
rw.On("Write", b).Return(11, nil).Once()
len, err := rr.Write(b)
require.NoError(t, err)
assert.True(t, rr.statusSet)
assert.True(t, rr.statusWrote)
assert.Equal(t, status, rr.status)
assert.Equal(t, 11, len)
assert.Equal(t, 11, rr.length)
rw.AssertExpectations(t)
}
func TestResponseRecorder_Header(t *testing.T) {
var (
rw responseWriterMock
rr = responseRecorder{writer: &rw}
headers = http.Header{"hello": []string{"wo", "r", "ld"}}
)
rw.On("Header").Return(headers).Once()
h := rr.Header()
assert.Equal(t, headers, h)
rw.AssertExpectations(t)
}
func TestResponseRecorder_WriteHeaderNow(t *testing.T) {
t.Run("no status set", func(t *testing.T) {
t.Parallel()
var (
rw responseWriterMock
rr = responseRecorder{writer: &rw}
)
rw.On("WriteHeader", http.StatusOK).Once()
rr.WriteHeaderNow()
assert.True(t, rr.statusSet)
assert.True(t, rr.statusWrote)
assert.Equal(t, http.StatusOK, rr.status)
rw.AssertExpectations(t)
})
t.Run("status set not wrote", func(t *testing.T) {
t.Parallel()
var (
rw responseWriterMock
rr = responseRecorder{writer: &rw}
status = http.StatusOK
)
assert.False(t, rr.statusSet)
assert.False(t, rr.statusWrote)
rr.WriteHeader(status)
rw.On("WriteHeader", status).Once()
rr.WriteHeaderNow()
assert.True(t, rr.statusSet)
assert.True(t, rr.statusWrote)
assert.Equal(t, http.StatusOK, rr.status)
rw.AssertExpectations(t)
})
t.Run("status set already wrote", func(t *testing.T) {
t.Parallel()
var (
rw responseWriterMock
rr = responseRecorder{
writer: &rw,
statusSet: true,
statusWrote: true,
status: http.StatusOK,
}
)
rr.WriteHeaderNow()
rw.AssertNotCalled(t, "WriteHeader", http.StatusOK)
rw.AssertExpectations(t)
})
}