-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwriter_test.go
More file actions
293 lines (247 loc) · 6.98 KB
/
Copy pathwriter_test.go
File metadata and controls
293 lines (247 loc) · 6.98 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package idemkit
import (
"net/http"
"net/http/httptest"
"testing"
)
type flushRecorder struct {
*httptest.ResponseRecorder
flushCount int
}
func (f *flushRecorder) Flush() {
f.flushCount++
}
type nonFlusherWriter struct {
hdr http.Header
code int
written []byte
writeCalls int
}
func (n *nonFlusherWriter) Header() http.Header {
if n.hdr == nil {
n.hdr = http.Header{}
}
return n.hdr
}
func (n *nonFlusherWriter) WriteHeader(code int) {
n.code = code
}
func (n *nonFlusherWriter) Write(b []byte) (int, error) {
n.writeCalls++
n.written = append(n.written, b...)
return len(b), nil
}
func TestResponseWriter_StatusCodeCapturedAndPassedThrough(t *testing.T) {
rec := httptest.NewRecorder()
w := newResponseWriter(rec, 1024)
w.WriteHeader(201)
if w.statusCode != 201 {
t.Fatalf("captured statusCode: %d, want 201", w.statusCode)
}
if rec.Code != 201 {
t.Fatalf("base statusCode: %d, want 201", rec.Code)
}
if !w.wroteHeader {
t.Fatal("wroteHeader should be true")
}
}
func TestResponseWriter_ImplicitStatusOKOnFirstWrite(t *testing.T) {
rec := httptest.NewRecorder()
w := newResponseWriter(rec, 1024)
if _, err := w.Write([]byte("hi")); err != nil {
t.Fatal(err)
}
if !w.wroteHeader {
t.Fatal("wroteHeader should be true after Write")
}
if w.statusCode != http.StatusOK {
t.Fatalf("statusCode: %d, want 200", w.statusCode)
}
if rec.Code != http.StatusOK {
t.Fatalf("base statusCode: %d, want 200", rec.Code)
}
}
func TestResponseWriter_DoubleWriteHeaderSecondIgnored(t *testing.T) {
rec := httptest.NewRecorder()
w := newResponseWriter(rec, 1024)
w.WriteHeader(200)
w.WriteHeader(500)
if w.statusCode != 200 {
t.Fatalf("captured statusCode: %d, want 200 (first call wins)", w.statusCode)
}
}
func TestResponseWriter_HeaderSnapshotTakenAtWriteHeader(t *testing.T) {
rec := httptest.NewRecorder()
w := newResponseWriter(rec, 1024)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("X-Trace", "abc")
w.WriteHeader(200)
w.Header().Set("X-After", "ignored")
if got := w.capturedHdr.Get("Content-Type"); got != "application/json" {
t.Fatalf("Content-Type captured: %q", got)
}
if got := w.capturedHdr.Get("X-Trace"); got != "abc" {
t.Fatalf("X-Trace captured: %q", got)
}
if got := w.capturedHdr.Get("X-After"); got != "" {
t.Fatalf("X-After should not be in snapshot: %q", got)
}
}
func TestResponseWriter_BodyPassedThroughToBase(t *testing.T) {
rec := httptest.NewRecorder()
w := newResponseWriter(rec, 1024)
w.Write([]byte("hello"))
if got := rec.Body.String(); got != "hello" {
t.Fatalf("base body: %q", got)
}
}
func TestResponseWriter_BodyCapturedUpToLimit(t *testing.T) {
rec := httptest.NewRecorder()
w := newResponseWriter(rec, 1024)
w.Write([]byte("hello world"))
if got := string(w.body.Bytes()); got != "hello world" {
t.Fatalf("captured body: %q", got)
}
if !w.cacheable() {
t.Fatal("should be cacheable")
}
}
func TestResponseWriter_BodyExactlyAtLimitIsCacheable(t *testing.T) {
rec := httptest.NewRecorder()
w := newResponseWriter(rec, 5)
w.Write([]byte("hello"))
if w.oversize {
t.Fatal("exactly at limit must not be oversize")
}
if got := string(w.body.Bytes()); got != "hello" {
t.Fatalf("captured body: %q", got)
}
}
func TestResponseWriter_OversizeSingleWriteDropsCapture(t *testing.T) {
rec := httptest.NewRecorder()
w := newResponseWriter(rec, 4)
w.Write([]byte("hello"))
if !w.oversize {
t.Fatal("expected oversize")
}
if w.body != nil {
t.Fatal("body buffer must be dropped on oversize")
}
if w.cacheable() {
t.Fatal("oversize must not be cacheable")
}
if got := rec.Body.String(); got != "hello" {
t.Fatalf("base must still receive full data: %q", got)
}
}
func TestResponseWriter_OversizeAcrossWritesDropsAllCapturedBytes(t *testing.T) {
rec := httptest.NewRecorder()
w := newResponseWriter(rec, 6)
w.Write([]byte("hello"))
w.Write([]byte(" world!"))
if !w.oversize {
t.Fatal("expected oversize after second write")
}
if w.body != nil {
t.Fatal("body buffer must be fully dropped, not partial")
}
if got := rec.Body.String(); got != "hello world!" {
t.Fatalf("base: %q", got)
}
}
func TestResponseWriter_FlushMarksUncacheableAndDropsBody(t *testing.T) {
rec := httptest.NewRecorder()
w := newResponseWriter(rec, 1024)
w.Write([]byte("partial"))
w.Flush()
if !w.flushed {
t.Fatal("flushed flag should be set")
}
if w.body != nil {
t.Fatal("body buffer must be dropped on Flush")
}
if w.cacheable() {
t.Fatal("flushed must not be cacheable")
}
}
func TestResponseWriter_FlushForwardedToBaseWhenSupported(t *testing.T) {
fr := &flushRecorder{ResponseRecorder: httptest.NewRecorder()}
w := newResponseWriter(fr, 1024)
w.Flush()
if fr.flushCount != 1 {
t.Fatalf("base Flush calls: %d, want 1", fr.flushCount)
}
}
func TestResponseWriter_FlushSafeWhenBaseNotFlusher(t *testing.T) {
base := &nonFlusherWriter{}
w := newResponseWriter(base, 1024)
defer func() {
if r := recover(); r != nil {
t.Fatalf("Flush on non-Flusher base panicked: %v", r)
}
}()
w.Flush()
if !w.flushed {
t.Fatal("flushed flag should be set regardless of base support")
}
}
func TestResponseWriter_WritesAfterOversizeStillPassThrough(t *testing.T) {
rec := httptest.NewRecorder()
w := newResponseWriter(rec, 3)
w.Write([]byte("foo"))
w.Write([]byte("bar"))
w.Write([]byte("baz"))
if got := rec.Body.String(); got != "foobarbaz" {
t.Fatalf("base body: %q, want full pass-through", got)
}
}
func TestResponseWriter_SnapshotProducesUsableResult(t *testing.T) {
rec := httptest.NewRecorder()
w := newResponseWriter(rec, 1024)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(201)
w.Write([]byte(`{"id":1}`))
res := w.snapshot()
if res.StatusCode != 201 {
t.Fatalf("snapshot status: %d", res.StatusCode)
}
if got := res.Header.Get("Content-Type"); got != "application/json" {
t.Fatalf("snapshot header: %q", got)
}
if string(res.Body) != `{"id":1}` {
t.Fatalf("snapshot body: %q", res.Body)
}
}
func TestResponseWriter_SnapshotEmptyHandlerProducesImplicit200(t *testing.T) {
rec := httptest.NewRecorder()
w := newResponseWriter(rec, 1024)
res := w.snapshot()
if res.StatusCode != http.StatusOK {
t.Fatalf("empty handler status: %d, want 200", res.StatusCode)
}
if len(res.Body) != 0 {
t.Fatalf("empty handler body: %q", res.Body)
}
if res.Header == nil {
t.Fatal("snapshot Header should never be nil")
}
}
func TestResponseWriter_SnapshotCapturesHeadersSetBeforeAnyWrite(t *testing.T) {
rec := httptest.NewRecorder()
w := newResponseWriter(rec, 1024)
w.Header().Set("X-Custom", "value")
res := w.snapshot()
if got := res.Header.Get("X-Custom"); got != "value" {
t.Fatalf("snapshot header: %q", got)
}
}
func TestResponseWriter_SnapshotBodyIsIndependentCopy(t *testing.T) {
rec := httptest.NewRecorder()
w := newResponseWriter(rec, 1024)
w.Write([]byte("original"))
res := w.snapshot()
res.Body[0] = 'X'
if got := string(w.body.Bytes()); got != "original" {
t.Fatalf("snapshot body is aliased to internal buffer: internal now %q", got)
}
}