Repository navigation
Expand file tree
/
Copy pathstream_writer_test.go
More file actions
284 lines (222 loc) · 8.8 KB
/
Copy pathstream_writer_test.go
File metadata and controls
284 lines (222 loc) · 8.8 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
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package httpserver
import (
"errors"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// fakeResponseWriter is a purpose-built [http.ResponseWriter] for tests that
// need to observe every [http.ResponseWriter.WriteHeader] and
// [http.ResponseWriter.Write] call, including informational (1xx) responses.
// It does NOT depend on [httptest.ResponseRecorder], whose simplified header
// behavior is inadequate for verifying 1xx semantics. It deliberately
// implements none of the connection control features.
type fakeResponseWriter struct {
header http.Header
statuses []int
writes [][]byte
}
func newFakeResponseWriter() *fakeResponseWriter {
return &fakeResponseWriter{header: make(http.Header)}
}
func (f *fakeResponseWriter) Header() http.Header {
return f.header
}
func (f *fakeResponseWriter) WriteHeader(status int) {
f.statuses = append(f.statuses, status)
}
func (f *fakeResponseWriter) Write(b []byte) (int, error) {
f.writes = append(f.writes, b)
return len(b), nil
}
func TestStreamWriter_InitialState(t *testing.T) {
fake := newFakeResponseWriter()
sw := &StreamWriter{writer: fake}
assert.Equal(t, 0, sw.status)
assert.Equal(t, 0, sw.bytesWritten)
}
func TestStreamWriter_FirstWriteImplicit200(t *testing.T) {
fake := newFakeResponseWriter()
sw := &StreamWriter{writer: fake}
n, err := sw.Write([]byte("hello"))
assert.NoError(t, err)
assert.Equal(t, 5, n)
assert.Equal(t, http.StatusOK, sw.status)
assert.Equal(t, 5, sw.bytesWritten)
}
func TestStreamWriter_MultipleWritesAccumulate(t *testing.T) {
fake := newFakeResponseWriter()
sw := &StreamWriter{writer: fake}
n1, _ := sw.Write([]byte("foo"))
n2, _ := sw.Write([]byte("barbaz"))
assert.Equal(t, 3, n1)
assert.Equal(t, 6, n2)
assert.Equal(t, 9, sw.bytesWritten)
}
func TestStreamWriter_RepeatedFinalStatus_PreservesFirst(t *testing.T) {
fake := newFakeResponseWriter()
sw := &StreamWriter{writer: fake}
sw.WriteHeader(http.StatusOK)
sw.WriteHeader(http.StatusNotFound)
// First committed 2xx-5xx status wins, matching net/http semantics.
assert.Equal(t, http.StatusOK, sw.status)
// Both calls are forwarded to the underlying writer.
assert.Equal(t, []int{http.StatusOK, http.StatusNotFound}, fake.statuses)
}
func TestStreamWriter_InformationalThenFinal_TracksFinal(t *testing.T) {
fake := newFakeResponseWriter()
sw := &StreamWriter{writer: fake}
sw.WriteHeader(http.StatusEarlyHints) // 103
sw.WriteHeader(http.StatusOK) // 200
// 1xx is informational; the subsequent 200 is the first final status.
assert.Equal(t, http.StatusOK, sw.status)
assert.Equal(t, []int{http.StatusEarlyHints, http.StatusOK}, fake.statuses)
}
func TestStreamWriter_MultipleInformationalThenBodyWrite_Tracks200(t *testing.T) {
fake := newFakeResponseWriter()
sw := &StreamWriter{writer: fake}
sw.WriteHeader(http.StatusContinue) // 100
sw.WriteHeader(http.StatusEarlyHints) // 103
_, _ = sw.Write([]byte("body"))
// Multiple 1xx responses don't commit; first body write implies 200.
assert.Equal(t, http.StatusOK, sw.status)
assert.Equal(t, 4, sw.bytesWritten)
}
func TestStreamWriter_InformationalThenFinalError_TracksError(t *testing.T) {
fake := newFakeResponseWriter()
sw := &StreamWriter{writer: fake}
sw.WriteHeader(http.StatusEarlyHints) // 103
sw.WriteHeader(http.StatusInternalServerError) // 500
// 1xx is informational; the subsequent 500 is the first final status.
assert.Equal(t, http.StatusInternalServerError, sw.status)
}
func TestStreamWriter_InformationalDoesNotCommitFinal(t *testing.T) {
fake := newFakeResponseWriter()
sw := &StreamWriter{writer: fake}
sw.WriteHeader(http.StatusEarlyHints) // 103
// 1xx is informational, not a final commit; status stays 0.
// This is critical for panic recovery: server.go checks
// streamWriter.status == 0 to decide whether to write a 500.
assert.Equal(t, 0, sw.status)
}
// flushRecorderWriter counts Flush calls on a writer that otherwise does not
// implement any connection control feature.
type flushRecorderWriter struct {
http.ResponseWriter
flushes int
}
func (f *flushRecorderWriter) Flush() { f.flushes++ }
// ============================================================================
// Connection control delegation (Flush / deadlines / full duplex)
// ============================================================================
// allFeaturesResponseWriter implements every connection control feature the
// [StreamWriter] probes for, each recording its invocation.
type allFeaturesResponseWriter struct {
*fakeResponseWriter
flushes int
readDeadline time.Time
writeDeadline time.Time
fullDuplex bool
}
func (a *allFeaturesResponseWriter) Flush() { a.flushes++ }
func (a *allFeaturesResponseWriter) SetReadDeadline(deadline time.Time) error {
a.readDeadline = deadline
return nil
}
func (a *allFeaturesResponseWriter) SetWriteDeadline(deadline time.Time) error {
a.writeDeadline = deadline
return nil
}
func (a *allFeaturesResponseWriter) EnableFullDuplex() error {
a.fullDuplex = true
return nil
}
func newAllFeaturesResponseWriter() *allFeaturesResponseWriter {
return &allFeaturesResponseWriter{
fakeResponseWriter: newFakeResponseWriter(),
}
}
func TestStreamWriter_ConnectionControls_Supported(t *testing.T) {
fake := newAllFeaturesResponseWriter()
sw := &StreamWriter{writer: fake}
sw.Flush() // Flush carries no error; unsupported flush is swallowed
assert.Equal(t, 1, fake.flushes)
readDeadline := time.Now().Add(time.Second)
require.NoError(t, sw.SetReadDeadline(readDeadline))
assert.Equal(t, readDeadline, fake.readDeadline)
writeDeadline := time.Now().Add(2 * time.Second)
require.NoError(t, sw.SetWriteDeadline(writeDeadline))
assert.Equal(t, writeDeadline, fake.writeDeadline)
require.NoError(t, sw.EnableFullDuplex())
assert.True(t, fake.fullDuplex)
}
func TestStreamWriter_ConnectionControls_Unsupported(t *testing.T) {
fake := newFakeResponseWriter()
sw := &StreamWriter{writer: fake}
sw.Flush() // no Flush support: a swallowed no-op
assert.Empty(t, fake.statuses)
assert.Empty(t, fake.writes)
assert.ErrorIs(t, sw.SetReadDeadline(time.Now()), http.ErrNotSupported)
assert.ErrorIs(t, sw.SetWriteDeadline(time.Now()), http.ErrNotSupported)
assert.ErrorIs(t, sw.EnableFullDuplex(), http.ErrNotSupported)
}
// Every connection control matches a probe [http.ResponseController]
// performs, so controllers created by wrapping code resolve all of them —
// Flush included, through the plain [http.Flusher] probe — through the
// StreamWriter itself.
func TestStreamWriter_ResponseControllerInterop(t *testing.T) {
fake := newAllFeaturesResponseWriter()
controller := http.NewResponseController(&StreamWriter{writer: fake})
require.NoError(t, controller.Flush())
assert.Equal(t, 1, fake.flushes)
readDeadline := time.Now().Add(time.Second)
require.NoError(t, controller.SetReadDeadline(readDeadline))
assert.Equal(t, readDeadline, fake.readDeadline)
writeDeadline := time.Now().Add(2 * time.Second)
require.NoError(t, controller.SetWriteDeadline(writeDeadline))
assert.Equal(t, writeDeadline, fake.writeDeadline)
require.NoError(t, controller.EnableFullDuplex())
assert.True(t, fake.fullDuplex)
}
// ============================================================================
// StreamWriter.Hijack
// ============================================================================
// A successful Hijack returns the underlying connection and ReadWriter and
// detaches the writer, which the server reads as the committed takeover.
func TestStreamWriter_Hijack_Success_DetachesWriter(t *testing.T) {
w, clientConn := newHijackableWriter(t)
defer clientConn.Close()
sw := &StreamWriter{writer: w}
conn, readWriter, err := sw.Hijack()
require.NoError(t, err)
assert.Same(t, w.conn, conn)
assert.Same(t, w.readWriter, readWriter)
assert.True(t, w.hijacked, "underlying connection taken over")
assert.Nil(t, sw.writer, "writer detached by the successful hijack")
}
// An underlying hijack error is returned as-is and leaves the writer
// attached.
func TestStreamWriter_Hijack_UnderlyingError_KeepsWriter(t *testing.T) {
w, clientConn := newHijackableWriter(t)
defer clientConn.Close()
w.hijackErr = errors.New("hijack refused")
sw := &StreamWriter{writer: w}
_, _, err := sw.Hijack()
assert.Error(t, err)
assert.False(t, w.hijacked)
assert.NotNil(t, sw.writer, "writer stays attached on hijack error")
}
// A writer without hijack support yields [http.ErrNotSupported].
func TestStreamWriter_Hijack_Unsupported(t *testing.T) {
sw := &StreamWriter{writer: newFakeResponseWriter()}
_, _, err := sw.Hijack()
assert.ErrorIs(t, err, http.ErrNotSupported)
assert.NotNil(t, sw.writer)
}