-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrwb_test.go
223 lines (214 loc) · 5.91 KB
/
rwb_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
* rwb_test.go
*
* Copyright (c) 2021 Forest Hoffman. All Rights Reserved.
* License: MIT License (see the included LICENSE file) or download at
* https://raw.githubusercontent.com/foresthoffman/rwb/master/LICENSE
*/
package rwb
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
var writeTestCases = []struct {
Name string
ResponseBody []byte
ExpectedBody []byte
Flush bool
}{
{
Name: "write",
ResponseBody: []byte("hello 123"),
ExpectedBody: []byte("hello 123"),
Flush: true,
},
{
Name: "write_no_flush",
ResponseBody: []byte("hello 123"),
ExpectedBody: []byte(""),
Flush: false,
},
}
func TestResponseWriterBuffer_Write(t *testing.T) {
for _, testCase := range writeTestCases {
t.Run(testCase.Name, func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rwb := New(w)
// Writing to ResponseWriterBuffer.
n, err := rwb.Write(testCase.ResponseBody)
if err != nil {
t.Error(err)
}
if n != len(testCase.ResponseBody) {
t.Errorf("expected: %d got: %d", len(testCase.ResponseBody), n)
}
if testCase.Flush {
// Send the buffered data to the ResponseWriter.
n, err := rwb.Flush()
if err != nil {
t.Error(err)
}
if n != len(testCase.ResponseBody) {
t.Errorf("expected: %d got: %d", len(testCase.ResponseBody), n)
}
}
}))
defer ts.Close()
res, err := http.Get(ts.URL)
if err != nil {
t.Error(err)
}
b, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Error(err)
}
if string(b) != string(testCase.ExpectedBody) {
t.Errorf("expected: %q got: %q", string(b), string(testCase.ExpectedBody))
}
})
}
}
var (
headerTestCases = []struct {
Name string
StartingHeaders http.Header
AdditionalHeaders http.Header
ExpectedHeaders http.Header
Flush bool
}{
{
Name: "one_single_value",
StartingHeaders: http.Header{"Content-Type": []string{"application/json"}},
AdditionalHeaders: http.Header{},
ExpectedHeaders: http.Header{"Content-Type": []string{"application/json"}},
Flush: true,
},
{
Name: "one_single_value_no_flush",
StartingHeaders: http.Header{"Content-Type": []string{"application/json"}},
AdditionalHeaders: http.Header{},
ExpectedHeaders: http.Header{"Content-Type": []string{"application/json"}},
Flush: false,
},
{
Name: "multiple_single_value",
StartingHeaders: http.Header{
"Content-Type": []string{"application/json"},
"sandwich": []string{"BLT"},
},
AdditionalHeaders: http.Header{},
ExpectedHeaders: http.Header{
"Content-Type": []string{"application/json"},
"sandwich": []string{"BLT"},
},
Flush: true,
},
{
Name: "multiple_single_value_no_flush",
StartingHeaders: http.Header{
"Content-Type": []string{"application/json"},
"sandwich": []string{"BLT"},
},
AdditionalHeaders: http.Header{},
ExpectedHeaders: http.Header{
"Content-Type": []string{"application/json"},
"sandwich": []string{"BLT"},
},
Flush: false,
},
{
Name: "one_single_value_additional",
StartingHeaders: http.Header{},
AdditionalHeaders: http.Header{"Content-Type": []string{"application/json"}},
ExpectedHeaders: http.Header{"Content-Type": []string{"application/json"}},
Flush: true,
},
{
Name: "one_single_value_additional_no_flush",
StartingHeaders: http.Header{},
AdditionalHeaders: http.Header{"Content-Type": []string{"application/json"}},
ExpectedHeaders: http.Header{},
Flush: false,
},
{
Name: "multiple_single_value_additional",
StartingHeaders: http.Header{},
AdditionalHeaders: http.Header{
"Content-Type": []string{"application/json"},
"sandwich": []string{"BLT"},
},
ExpectedHeaders: http.Header{
"Content-Type": []string{"application/json"},
"sandwich": []string{"BLT"},
},
Flush: true,
},
{
Name: "multiple_single_value_additional_no_flush",
StartingHeaders: http.Header{},
AdditionalHeaders: http.Header{
"Content-Type": []string{"application/json"},
"sandwich": []string{"BLT"},
},
ExpectedHeaders: http.Header{},
Flush: false,
},
}
)
func TestResponseWriterBuffer_Header(t *testing.T) {
for _, testCase := range headerTestCases {
t.Run(testCase.Name, func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Header has some values before the ResponseWriterBuffer takes over.
for key, values := range testCase.StartingHeaders {
for _, value := range values {
w.Header().Add(key, value)
}
}
rwb := New(w)
// Header has some new values.
for key, values := range testCase.AdditionalHeaders {
for _, value := range values {
rwb.Header().Add(key, value)
}
}
if testCase.Flush {
// Send the buffered data to the ResponseWriter.
_, err := rwb.Flush()
if err != nil {
t.Error(err)
}
}
}))
defer ts.Close()
res, err := http.Get(ts.URL)
if err != nil {
t.Error(err)
}
for key, expectedValues := range testCase.ExpectedHeaders {
actualValues := res.Header.Values(key)
if len(actualValues) == 0 {
t.Errorf("missing header: %q with value %v", key, expectedValues)
}
if len(actualValues) > len(expectedValues) {
t.Errorf("expected: %v got: %v", expectedValues, actualValues)
}
for _, expectedValue := range expectedValues {
found := false
for _, actualValue := range actualValues {
if actualValue == expectedValue {
found = true
break
}
}
if !found {
t.Errorf("expected: %v got: %v", expectedValues, actualValues)
}
}
}
})
}
}