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
|
package http_test
import (
"errors"
"fmt"
"io"
. "net/http"
"os"
"sync"
"testing"
"time"
)
func TestResponseControllerFlush(t *testing.T) { run(t, testResponseControllerFlush) }
func testResponseControllerFlush(t *testing.T, mode testMode) {
continuec := make(chan struct{})
cst := newClientServerTest(t, mode, HandlerFunc(func(w ResponseWriter, r *Request) {
ctl := NewResponseController(w)
w.Write([]byte("one"))
if err := ctl.Flush(); err != nil {
t.Errorf("ctl.Flush() = %v, want nil", err)
return
}
<-continuec
w.Write([]byte("two"))
}))
res, err := cst.c.Get(cst.ts.URL)
if err != nil {
t.Fatalf("unexpected connection error: %v", err)
}
defer res.Body.Close()
buf := make([]byte, 16)
n, err := res.Body.Read(buf)
close(continuec)
if err != nil || string(buf[:n]) != "one" {
t.Fatalf("Body.Read = %q, %v, want %q, nil", string(buf[:n]), err, "one")
}
got, err := io.ReadAll(res.Body)
if err != nil || string(got) != "two" {
t.Fatalf("Body.Read = %q, %v, want %q, nil", string(got), err, "two")
}
}
func TestResponseControllerHijack(t *testing.T) { run(t, testResponseControllerHijack) }
func testResponseControllerHijack(t *testing.T, mode testMode) {
const header = "X-Header"
const value = "set"
cst := newClientServerTest(t, mode, HandlerFunc(func(w ResponseWriter, r *Request) {
ctl := NewResponseController(w)
c, _, err := ctl.Hijack()
if mode == http2Mode {
if err == nil {
t.Errorf("ctl.Hijack = nil, want error")
}
w.Header().Set(header, value)
return
}
if err != nil {
t.Errorf("ctl.Hijack = _, _, %v, want _, _, nil", err)
return
}
fmt.Fprintf(c, "HTTP/1.0 200 OK\r\n%v: %v\r\nContent-Length: 0\r\n\r\n", header, value)
}))
res, err := cst.c.Get(cst.ts.URL)
if err != nil {
t.Fatal(err)
}
if got, want := res.Header.Get(header), value; got != want {
t.Errorf("response header %q = %q, want %q", header, got, want)
}
}
func TestResponseControllerSetPastWriteDeadline(t *testing.T) {
run(t, testResponseControllerSetPastWriteDeadline)
}
func testResponseControllerSetPastWriteDeadline(t *testing.T, mode testMode) {
cst := newClientServerTest(t, mode, HandlerFunc(func(w ResponseWriter, r *Request) {
ctl := NewResponseController(w)
w.Write([]byte("one"))
if err := ctl.Flush(); err != nil {
t.Errorf("before setting deadline: ctl.Flush() = %v, want nil", err)
}
if err := ctl.SetWriteDeadline(time.Now().Add(-10 * time.Second)); err != nil {
t.Errorf("ctl.SetWriteDeadline() = %v, want nil", err)
}
w.Write([]byte("two"))
if err := ctl.Flush(); err == nil {
t.Errorf("after setting deadline: ctl.Flush() = nil, want non-nil")
}
// Connection errors are sticky, so resetting the deadline does not permit
// making more progress. We might want to change this in the future, but verify
// the current behavior for now. If we do change this, we'll want to make sure
// to do so only for writing the response body, not headers.
if err := ctl.SetWriteDeadline(time.Now().Add(1 * time.Hour)); err != nil {
t.Errorf("ctl.SetWriteDeadline() = %v, want nil", err)
}
w.Write([]byte("three"))
if err := ctl.Flush(); err == nil {
t.Errorf("after resetting deadline: ctl.Flush() = nil, want non-nil")
}
}))
res, err := cst.c.Get(cst.ts.URL)
if err != nil {
t.Fatalf("unexpected connection error: %v", err)
}
defer res.Body.Close()
b, _ := io.ReadAll(res.Body)
if string(b) != "one" {
t.Errorf("unexpected body: %q", string(b))
}
}
func TestResponseControllerSetFutureWriteDeadline(t *testing.T) {
run(t, testResponseControllerSetFutureWriteDeadline)
}
func testResponseControllerSetFutureWriteDeadline(t *testing.T, mode testMode) {
errc := make(chan error, 1)
startwritec := make(chan struct{})
cst := newClientServerTest(t, mode, HandlerFunc(func(w ResponseWriter, r *Request) {
ctl := NewResponseController(w)
w.WriteHeader(200)
if err := ctl.Flush(); err != nil {
t.Errorf("ctl.Flush() = %v, want nil", err)
}
<-startwritec // don't set the deadline until the client reads response headers
if err := ctl.SetWriteDeadline(time.Now().Add(1 * time.Millisecond)); err != nil {
t.Errorf("ctl.SetWriteDeadline() = %v, want nil", err)
}
_, err := io.Copy(w, neverEnding('a'))
errc <- err
}))
res, err := cst.c.Get(cst.ts.URL)
close(startwritec)
if err != nil {
t.Fatalf("unexpected connection error: %v", err)
}
defer res.Body.Close()
_, err = io.Copy(io.Discard, res.Body)
if err == nil {
t.Errorf("client reading from truncated request body: got nil error, want non-nil")
}
err = <-errc // io.Copy error
if !errors.Is(err, os.ErrDeadlineExceeded) {
t.Errorf("server timed out writing request body: got err %v; want os.ErrDeadlineExceeded", err)
}
}
func TestResponseControllerSetPastReadDeadline(t *testing.T) {
run(t, testResponseControllerSetPastReadDeadline)
}
func testResponseControllerSetPastReadDeadline(t *testing.T, mode testMode) {
readc := make(chan struct{})
cst := newClientServerTest(t, mode, HandlerFunc(func(w ResponseWriter, r *Request) {
ctl := NewResponseController(w)
b := make([]byte, 3)
n, err := io.ReadFull(r.Body, b)
b = b[:n]
if err != nil || string(b) != "one" {
t.Errorf("before setting read deadline: Read = %v, %q, want nil, %q", err, string(b), "one")
return
}
if err := ctl.SetReadDeadline(time.Now()); err != nil {
t.Errorf("ctl.SetReadDeadline() = %v, want nil", err)
return
}
b, err = io.ReadAll(r.Body)
if err == nil || string(b) != "" {
t.Errorf("after setting read deadline: Read = %q, nil, want error", string(b))
}
close(readc)
// Connection errors are sticky, so resetting the deadline does not permit
// making more progress. We might want to change this in the future, but verify
// the current behavior for now.
if err := ctl.SetReadDeadline(time.Time{}); err != nil {
t.Errorf("ctl.SetReadDeadline() = %v, want nil", err)
return
}
b, err = io.ReadAll(r.Body)
if err == nil {
t.Errorf("after resetting read deadline: Read = %q, nil, want error", string(b))
}
}))
pr, pw := io.Pipe()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
pw.Write([]byte("one"))
<-readc
pw.Write([]byte("two"))
pw.Close()
}()
defer wg.Wait()
res, err := cst.c.Post(cst.ts.URL, "text/foo", pr)
if err == nil {
defer res.Body.Close()
}
}
func TestResponseControllerSetFutureReadDeadline(t *testing.T) {
run(t, testResponseControllerSetFutureReadDeadline)
}
func testResponseControllerSetFutureReadDeadline(t *testing.T, mode testMode) {
respBody := "response body"
cst := newClientServerTest(t, mode, HandlerFunc(func(w ResponseWriter, req *Request) {
ctl := NewResponseController(w)
if err := ctl.SetReadDeadline(time.Now().Add(1 * time.Millisecond)); err != nil {
t.Errorf("ctl.SetReadDeadline() = %v, want nil", err)
}
_, err := io.Copy(io.Discard, req.Body)
if !errors.Is(err, os.ErrDeadlineExceeded) {
t.Errorf("server timed out reading request body: got err %v; want os.ErrDeadlineExceeded", err)
}
w.Write([]byte(respBody))
}))
pr, pw := io.Pipe()
res, err := cst.c.Post(cst.ts.URL, "text/apocryphal", pr)
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
got, err := io.ReadAll(res.Body)
if string(got) != respBody || err != nil {
t.Errorf("client read response body: %q, %v; want %q, nil", string(got), err, respBody)
}
pw.Close()
}
type wrapWriter struct {
ResponseWriter
}
func (w wrapWriter) Unwrap() ResponseWriter {
return w.ResponseWriter
}
func TestWrappedResponseController(t *testing.T) { run(t, testWrappedResponseController) }
func testWrappedResponseController(t *testing.T, mode testMode) {
cst := newClientServerTest(t, mode, HandlerFunc(func(w ResponseWriter, r *Request) {
w = wrapWriter{w}
ctl := NewResponseController(w)
if err := ctl.Flush(); err != nil {
t.Errorf("ctl.Flush() = %v, want nil", err)
}
if err := ctl.SetReadDeadline(time.Time{}); err != nil {
t.Errorf("ctl.SetReadDeadline() = %v, want nil", err)
}
if err := ctl.SetWriteDeadline(time.Time{}); err != nil {
t.Errorf("ctl.SetWriteDeadline() = %v, want nil", err)
}
}))
res, err := cst.c.Get(cst.ts.URL)
if err != nil {
t.Fatalf("unexpected connection error: %v", err)
}
io.Copy(io.Discard, res.Body)
defer res.Body.Close()
}
|