Repository navigation
Expand file tree
/
Copy pathclient_codec_test.go
More file actions
568 lines (542 loc) · 20.6 KB
/
Copy pathclient_codec_test.go
File metadata and controls
568 lines (542 loc) · 20.6 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
//go:build linux || darwin || dragonfly || freebsd || netbsd || openbsd || windows
package http
import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"io"
stdhttp "net/http"
"net/http/httptrace"
"net/url"
"reflect"
"strings"
"testing"
"testing/iotest"
"github.com/lesismal/fib/bufferpool"
)
// TestSortHeaderFields holds sortHeaderFields, which tells names apart by
// their first eight bytes where it can, to sorting the names as strings: for
// names that share prefixes of every length up to and past eight, are
// shorter than eight, or hold a zero byte, and for more of them than the
// insertion sort takes.
func TestSortHeaderFields(t *testing.T) {
names := []string{"Accept", "Accept-Encoding", "Accept-Language", "Accept-L", "Accept-", "A", "",
"X-Forwarded-For", "X-Forwarded-Host", "X-Forwarded-Proto", "X-Forwar", "X-Forwarde", "X",
"Content-Length", "Content-Type", "Content-", "Content", "Ab\x00", "Ab", "Ab\x00c", "Ab\x01",
"User-Agent", "Cookie", "Te", "Zz", "\xff", "\xff\xff\xff\xff\xff\xff\xff\xff\x01"}
for n := 1; n <= len(names); n++ {
for shift := range names {
var fields []headerField
for i := range n {
fields = append(fields, newHeaderField(names[(i*7+shift)%len(names)], nil))
}
sortHeaderFields(fields)
for i := 1; i < len(fields); i++ {
if fields[i-1].key > fields[i].key {
t.Fatalf("n=%d shift=%d: %q before %q", n, shift, fields[i-1].key, fields[i].key)
}
}
}
}
}
// stdResponseHead is what ReadResponse makes of head, as parseHead used it
// before the client parsed heads itself.
func stdResponseHead(head []byte, req *stdhttp.Request) (*stdhttp.Response, error) {
resp, err := stdhttp.ReadResponse(bufio.NewReader(bytes.NewReader(head)), req)
if err == nil {
_ = resp.Body.Close()
}
return resp, err
}
// sameResponseHead reports how got, from parseResponseHead, differs from
// want, from ReadResponse, in what a caller of the client sees, or "".
func sameResponseHead(got, want *stdhttp.Response) string {
switch {
case got.Status != want.Status:
return "Status " + got.Status + " / " + want.Status
case got.StatusCode != want.StatusCode, got.Proto != want.Proto, got.ProtoMajor != want.ProtoMajor,
got.ProtoMinor != want.ProtoMinor:
return "status line"
case !reflect.DeepEqual(got.Header, want.Header):
return "Header"
case got.ContentLength != want.ContentLength:
return "ContentLength"
case !reflect.DeepEqual(got.TransferEncoding, want.TransferEncoding):
return "TransferEncoding"
case got.Close != want.Close:
return "Close"
case !reflect.DeepEqual(got.Trailer, want.Trailer):
return "Trailer"
case got.Uncompressed != want.Uncompressed, got.Request != want.Request, got.TLS != want.TLS:
return "other fields"
}
return ""
}
var responseHeads = []string{
"HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 12\r\n\r\n",
"HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nDate: Tue, 20 Oct 2026 10:00:00 GMT\r\nContent-Length: 12\r\n\r\n",
"HTTP/1.1 200\r\nContent-Length: 0\r\n\r\n",
"HTTP/1.1 200 \r\nContent-Length: 0\r\n\r\n",
"HTTP/1.1 404 Not Found\r\n\r\n",
"HTTP/1.0 200 OK\r\nContent-Length: 3\r\n\r\n",
"HTTP/1.0 200 OK\r\nConnection: keep-alive\r\nContent-Length: 3\r\n\r\n",
"HTTP/1.0 200 OK\r\nConnection: Keep-Alive, close\r\n\r\n",
"HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: 3\r\n\r\n",
"HTTP/1.1 200 OK\r\nConnection: upgrade, CLOSE\r\n\r\n",
"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nContent-Length: 5\r\n\r\n",
"HTTP/1.1 200 OK\r\nTransfer-Encoding: Chunked\r\n\r\n",
"HTTP/1.1 200 OK\r\nTransfer-Encoding: gzip, chunked\r\n\r\n",
"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nTransfer-Encoding: chunked\r\n\r\n",
"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunKed\r\n\r\n", // a Kelvin sign
"HTTP/1.0 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n",
"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nTrailer: X-Sum\r\n\r\n",
"HTTP/1.1 200 OK\r\nTrailer: X-Sum\r\nContent-Length: 1\r\n\r\n",
"HTTP/1.1 204 No Content\r\nContent-Length: 10\r\n\r\n",
"HTTP/1.1 304 Not Modified\r\nEtag: \"x\"\r\n\r\n",
"HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n\r\n",
"HTTP/1.1 100 Continue\r\n\r\n",
"HTTP/1.1 200 OK\r\nContent-Length: 5\r\nContent-Length: 5\r\n\r\n",
"HTTP/1.1 200 OK\r\nContent-Length: 5\r\nContent-Length: 6\r\n\r\n",
"HTTP/1.1 200 OK\r\nContent-Length: -1\r\n\r\n",
"HTTP/1.1 200 OK\r\nContent-Length: \r\n\r\n",
"HTTP/1.1 200 OK\r\nContent-Length: 7 \r\n\r\n",
"HTTP/1.1 200 OK\r\ncontent-length: 7\r\nSET-COOKIE: a=1\r\nset-cookie: b=2\r\nx-custom-thing: v\r\n\r\n",
"HTTP/1.1 200 OK\r\nPragma: no-cache\r\n\r\n",
"HTTP/1.1 200 OK\r\nPragma: no-cache\r\nCache-Control: max-age=1\r\n\r\n",
"HTTP/1.1 200 OK\r\nX-Empty:\r\nX-Tab:\tv\t\r\n\r\n",
"HTTP/1.1 200 OK\r\nX-Folded: a\r\n b\r\n\r\n",
"HTTP/1.1 200 OK\r\nX-A : 1\r\n\r\n",
"HTTP/1.1 200 OK\r\nBad\x01: 1\r\n\r\n",
"HTTP/1.1 200 OK\r\nX-A: 1\x00\r\n\r\n",
"HTTP/1.1 200 OK\nContent-Length: 1\n\n",
"HTTP/1.1 200 OK\r\nX-A: 1\nX-B: 2\r\n\r\n",
"HTTP/1.1 200 OK\nX-A: 1\r\n\r\n",
"HTTP/1.1 200 OK\r\nX-A: 1\r\nX-B: 2\n\r\n",
"HTTP/1.1 200 OK\r\n\r\n",
"HTTP/1.1 2000 OK\r\n\r\n",
"HTTP/1.1 20x OK\r\n\r\n",
"HTTP/2.0 200 OK\r\n\r\n",
"HTTP/1.1 999 Odd\r\n\r\n",
"ICY 200 OK\r\n\r\n",
"HTTP/1.1 200 OK\r\n: no name\r\n\r\n",
"HTTP/1.1 200 OK\r\nNo-Colon\r\n\r\n",
"HTTP/1.1 200 OK\r\nX-Unicode: caf\xc3\xa9\r\n\r\n",
}
// TestParseResponseHead holds parseResponseHead to ReadResponse: whatever it
// takes, it reads as ReadResponse does, and it takes the heads most
// responses have.
func TestParseResponseHead(t *testing.T) {
fast := map[string]bool{responseHeads[0]: true, responseHeads[1]: true, responseHeads[4]: true,
responseHeads[8]: true, responseHeads[10]: true, responseHeads[27]: true}
for _, method := range []string{stdhttp.MethodGet, stdhttp.MethodHead, stdhttp.MethodPost} {
for _, head := range responseHeads {
req := &stdhttp.Request{Method: method}
got, ok := parseResponseHead([]byte(head), req)
want, err := stdResponseHead([]byte(head), req)
if !ok {
if fast[head] {
t.Errorf("%s %q: left to net/http", method, head)
}
continue
}
if err != nil {
t.Errorf("%s %q: taken, but net/http refuses it: %v", method, head, err)
continue
}
if d := sameResponseHead(got, want); d != "" {
t.Errorf("%s %q: %s differs:\n got %+v\nwant %+v", method, head, d, got, want)
}
if got.Body != stdhttp.NoBody {
t.Errorf("%s %q: Body %v", method, head, got.Body)
}
}
}
}
// TestParseResponseHeadBlockTakesTheSame holds parseResponseHeadBlock with
// a block to parseResponseHead: it takes the heads the map takes, and none
// besides, frames them the same, and hands over their field lines.
func TestParseResponseHeadBlockTakesTheSame(t *testing.T) {
for _, head := range responseHeads {
req := &stdhttp.Request{Method: stdhttp.MethodGet}
want, ok := parseResponseHead([]byte(head), req)
got, block, okBlock := parseResponseHeadBlock([]byte(head), req, true)
if ok != okBlock {
t.Errorf("%q: taken %v with a block, %v with a map", head, okBlock, ok)
continue
}
if !ok {
continue
}
if got.Header != nil {
t.Errorf("%q: Header %v with a block", head, got.Header)
}
got.Header = want.Header
if d := sameResponseHead(got, want); d != "" {
t.Errorf("%q: %s differs with a block:\n got %+v\nwant %+v", head, d, got, want)
}
if wantBlock := head[strings.Index(head, "\r\n")+2 : len(head)-2]; block != wantBlock {
t.Errorf("%q: block %q, want %q", head, block, wantBlock)
}
}
}
func FuzzParseResponseHead(f *testing.F) {
for _, head := range responseHeads {
f.Add(head, false)
}
f.Fuzz(func(t *testing.T, head string, isHead bool) {
if !strings.HasSuffix(head, "\r\n\r\n") || strings.Contains(head[:len(head)-4], "\r\n\r\n") {
return // parseHead hands over exactly one head, up to its blank line
}
req := &stdhttp.Request{Method: stdhttp.MethodGet}
if isHead {
req.Method = stdhttp.MethodHead
}
got, ok := parseResponseHead([]byte(head), req)
if !ok {
return
}
want, err := stdResponseHead([]byte(head), req)
if err != nil {
t.Fatalf("%q: taken, but net/http refuses it: %v", head, err)
}
if d := sameResponseHead(got, want); d != "" {
t.Fatalf("%q: %s differs:\n got %+v\nwant %+v", head, d, got, want)
}
})
}
// stdMarshalRequest is marshalRequest as it was, through Request.Write alone.
func stdMarshalRequest(req *stdhttp.Request, body []byte) ([]byte, error) {
http10 := req.ProtoMajor == 1 && req.ProtoMinor == 0
out := *req
if http10 {
out.ContentLength = int64(len(body))
out.TransferEncoding = nil
out.Trailer = nil
if len(body) == 0 {
out.Body = nil
}
}
if out.Body != nil && out.Body != stdhttp.NoBody {
out.Body = io.NopCloser(bytes.NewReader(body))
}
var buf bytes.Buffer
if err := out.Write(&buf); err != nil {
return nil, err
}
data := buf.Bytes()
if http10 {
lineEnd := bytes.Index(data, []byte("\r\n"))
if lineEnd < 0 || !bytes.HasSuffix(data[:lineEnd], []byte(" HTTP/1.1")) {
return nil, fmt.Errorf("http: cannot write request line %q as HTTP/1.0", data[:max(lineEnd, 0)])
}
data[lineEnd-1] = '0'
}
return data, nil
}
// stdStreamedStart is what marshalStreamedHead took from Request.Write
// before its own framing: the head without Content-Length: 0 and without the
// blank line.
func stdStreamedStart(req *stdhttp.Request) ([]byte, error) {
clone := *req
clone.Body, clone.GetBody = nil, nil
clone.ContentLength = 0
clone.TransferEncoding = nil
clone.Trailer = nil
var buf bytes.Buffer
if err := clone.Write(&buf); err != nil {
return nil, err
}
data := buf.Bytes()
if i := bytes.Index(data, []byte("\r\nContent-Length: 0\r\n")); i >= 0 {
data = append(data[:i+2], data[i+len("\r\nContent-Length: 0\r\n"):]...)
}
return data[:len(data)-2], nil
}
type requestCase struct {
name string
method string
target string
host string
header stdhttp.Header
body string
declared int64 // ContentLength, -2 for len(body)
http10 bool
close bool
fast bool // the fast path must take it
}
func (c requestCase) request() (*stdhttp.Request, []byte) {
u, err := url.Parse(c.target)
if err != nil {
panic(err)
}
req := &stdhttp.Request{Method: c.method, URL: u, Host: c.host, Header: c.header, Close: c.close,
Proto: "HTTP/1.1", ProtoMajor: 1, ProtoMinor: 1}
if c.http10 {
req.Proto, req.ProtoMinor = "HTTP/1.0", 0
}
if req.Header == nil {
req.Header = stdhttp.Header{}
}
var body []byte
if c.body != "" || c.declared != 0 {
body = []byte(c.body)
req.Body = io.NopCloser(bytes.NewReader(body))
req.ContentLength = c.declared
if c.declared == -2 {
req.ContentLength = int64(len(body))
}
}
return req, body
}
var proxied = stdhttp.Header{"User-Agent": {}, "X-Forwarded-For": {"127.0.0.1"}, "X-Forwarded-Proto": {"http"},
"X-Forwarded-Host": {"127.0.0.1:8080"}, "Forwarded": {`for=127.0.0.1;host="127.0.0.1:8080";proto=http`}}
var requestCases = []requestCase{
{name: "proxied", method: "GET", target: "/proxy", host: "127.0.0.1:8080", header: proxied, fast: true},
{name: "default agent", method: "GET", target: "http://example.com/a?b=c", fast: true},
{name: "no method", target: "/x", host: "h", fast: true},
{name: "post", method: "POST", target: "/p", host: "h", body: "hello", declared: -2,
header: stdhttp.Header{"Content-Type": {"text/plain"}}, fast: true},
{name: "post empty", method: "POST", target: "/p", host: "h", fast: true},
{name: "put empty body", method: "PUT", target: "/p", host: "h", declared: 0, body: ""},
{name: "delete", method: "DELETE", target: "/d", host: "h", fast: true},
{name: "unknown length", method: "POST", target: "/p", host: "h", body: "abc", declared: 0},
{name: "chunked", method: "POST", target: "/p", host: "h", body: "abc", declared: -1},
{name: "http/1.0", method: "POST", target: "/p", host: "h", body: "abc", declared: 0, http10: true, fast: true},
{name: "http/1.0 get", method: "GET", target: "/p", host: "h", http10: true, fast: true},
{name: "close", method: "GET", target: "/c", host: "h", close: true},
{name: "many values", method: "GET", target: "/v", host: "h", fast: true, header: stdhttp.Header{
"Accept": {"a", " b ", "c\r\nInjected: 1"}, "Cookie": {"x=1; y=2"}, "User-Agent": {" agent\n "},
"Content-Length": {"99"}, "Transfer-Encoding": {"chunked"}, "Trailer": {"X"}, "Host": {"other"},
"Bad Name": {"dropped"}, "X-Empty": {}, "X-Blank": {""}}},
{name: "empty agent", method: "GET", target: "/", host: "h", header: stdhttp.Header{"User-Agent": {""}}, fast: true},
{name: "unicode host", method: "GET", target: "/", host: "bücher.example"},
{name: "zone", method: "GET", target: "/", host: "[fe80::1%25en0]:80"},
{name: "bad host", method: "GET", target: "/", host: "a b"},
{name: "escaped path", method: "GET", target: "/a%20b/c?d=%2F", host: "h", fast: true},
{name: "bad method", method: "GE T", target: "/", host: "h"},
{name: "connect", method: "CONNECT", target: "example.com:443", host: "example.com:443"},
{name: "many fields", method: "GET", target: "/", host: "h", fast: true, header: func() stdhttp.Header {
h := stdhttp.Header{}
for _, k := range strings.Fields("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z") {
h["X-"+k] = []string{k}
}
return h
}()},
}
// TestFastRequest holds fastRequest and fastStreamedHead to Request.Write's
// output: whatever they take, they write byte for byte as it does.
func TestFastRequest(t *testing.T) {
for _, c := range requestCases {
req, body := c.request()
want, wantErr := stdMarshalRequest(req, body)
req, body = c.request()
got, ok := fastRequest(req, body, c.http10)
switch {
case !ok && c.fast:
t.Errorf("%s: left to net/http", c.name)
case ok && wantErr != nil:
t.Errorf("%s: taken, but net/http refuses it: %v", c.name, wantErr)
case ok && !bytes.Equal(got, want):
t.Errorf("%s:\n got %q\nwant %q", c.name, got, want)
}
// marshalRequest itself, whichever way it goes.
req, body = c.request()
if got, _, err := marshalRequest(req, body); (err != nil) != (wantErr != nil) || err == nil && !bytes.Equal(got, want) {
t.Errorf("%s: marshalRequest %q, %v; want %q, %v", c.name, got, err, want, wantErr)
}
if c.http10 {
continue
}
req, _ = c.request()
wantStart, wantErr := stdStreamedStart(req)
req, _ = c.request()
gotStart, ok := fastStreamedHead(req)
switch {
case !ok && c.fast:
t.Errorf("%s: streamed head left to net/http", c.name)
case ok && wantErr != nil:
t.Errorf("%s: streamed head taken, but net/http refuses it: %v", c.name, wantErr)
case ok && !bytes.Equal(gotStart, wantStart):
t.Errorf("%s: streamed head:\n got %q\nwant %q", c.name, gotStart, wantStart)
}
}
// A trace on the request hears of each field as net/http writes it, so
// such a request is left to net/http.
req, body := requestCases[0].request()
req = req.WithContext(httptrace.WithClientTrace(context.Background(), &httptrace.ClientTrace{}))
if _, ok := fastRequest(req, body, false); ok {
t.Error("a traced request was taken")
}
}
func FuzzFastRequest(f *testing.F) {
f.Add("GET", "/proxy?a=b", "127.0.0.1:8080", "X-Forwarded-For", "127.0.0.1", "", false, false)
f.Add("POST", "/p", "h", "Content-Type", "text/plain", "hello", true, false)
f.Add("PUT", "/", "h", "User-Agent", " x\r\n", "", false, true)
f.Fuzz(func(t *testing.T, method, target, host, key, value, body string, declare, http10 bool) {
u, err := url.Parse(target)
if err != nil {
return
}
build := func() (*stdhttp.Request, []byte) {
req := &stdhttp.Request{Method: method, URL: u, Host: host, Header: stdhttp.Header{key: {value}},
Proto: "HTTP/1.1", ProtoMajor: 1, ProtoMinor: 1}
if http10 {
req.ProtoMinor = 0
}
var b []byte
if body != "" {
b = []byte(body)
req.Body = io.NopCloser(bytes.NewReader(b))
if declare {
req.ContentLength = int64(len(b))
}
}
return req, b
}
req, b := build()
got, ok := fastRequest(req, b, http10)
if !ok {
return
}
req, b = build()
want, err := stdMarshalRequest(req, b)
if err != nil {
t.Fatalf("taken, but net/http refuses it: %v", err)
}
if !bytes.Equal(got, want) {
t.Fatalf("\n got %q\nwant %q", got, want)
}
})
}
// TestClientCodecAllocs bounds what writing a proxied request and reading a
// plain response head allocate: nothing for the one, whose buffer is the
// pool's and goes back to it, and for the other the Response, its Header (a
// map and its table), the head's string and the values' backing.
func TestClientCodecAllocs(t *testing.T) {
if raceEnabled {
t.Skip("sync.Pool drops buffers under the race detector")
}
req, _ := requestCases[0].request()
if n := testing.AllocsPerRun(100, func() {
data, pooled, _ := marshalRequest(req, nil)
if !pooled {
t.Fatal("marshalRequest: the fast path's buffer is not the pool's")
}
bufferpool.Put(data)
}); n > 0 {
t.Errorf("marshalRequest: %v allocations, want 0", n)
}
head := []byte(responseHeads[1])
get := &stdhttp.Request{Method: stdhttp.MethodGet}
if n := testing.AllocsPerRun(100, func() { _, _ = parseResponseHead(head, get) }); n > 5 {
t.Errorf("parseResponseHead: %v allocations, want 5", n)
}
}
// A request framed by fastRequest takes its buffer from the pool and gives it
// back, so a request that goes that way allocates nothing of its own once the
// pool has a buffer of its size: the request-side counterpart of the
// response head's test above.
func TestMarshalRequestAllocations(t *testing.T) {
req, _ := requestCases[0].request()
data, pooled, err := marshalRequest(req, nil)
if err != nil || !pooled {
t.Fatalf("the request is not fastRequest's: %v", err)
}
bufferpool.Put(data)
allocs := testing.AllocsPerRun(100, func() {
out, pooled, err := marshalRequest(req, nil)
if err != nil || !pooled {
t.Fatalf("marshalRequest: %v, pooled %v", err, pooled)
}
bufferpool.Put(out)
})
if allocs > 1 {
t.Fatalf("%.1f allocations a request, want the pool's buffer", allocs)
}
}
func BenchmarkMarshalRequest(b *testing.B) {
req, _ := requestCases[0].request()
b.Run("fib", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_, _, _ = marshalRequest(req, nil)
}
})
b.Run("net-http", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_, _ = stdMarshalRequest(req, nil)
}
})
}
func BenchmarkParseResponseHead(b *testing.B) {
head := []byte(responseHeads[1])
req := &stdhttp.Request{Method: stdhttp.MethodGet}
b.Run("fib", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_, _ = parseResponseHead(head, req)
}
})
b.Run("net-http", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_, _ = stdResponseHead(head, req)
}
})
}
// FuzzDecodeChunks holds decodeChunks to net/http's chunked reader: a body it
// decodes, net/http decodes the same, with no trailer.
func FuzzDecodeChunks(f *testing.F) {
for _, body := range []string{"5\r\nhello\r\n0\r\n\r\n", "1;ext=1\r\na\r\n0\r\n\r\n", " 1\r\nb\r\n0\r\n\r\n",
"1 \t\r\nb\r\n0\r\n\r\n", "0\r\nX: 1\r\n\r\n", "1\nb\r\n0\r\n\r\n", "00000000000000001\r\nb\r\n0\r\n\r\n"} {
f.Add(body)
}
f.Fuzz(func(t *testing.T, chunks string) {
got, ok := decodeChunks(nil, []byte(chunks))
if !ok {
return
}
head := "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"
resp, err := stdhttp.ReadResponse(bufio.NewReader(strings.NewReader(head+chunks)), &stdhttp.Request{Method: "GET"})
if err != nil {
t.Fatal(err)
}
want, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("%q: decoded, but net/http refuses it: %v", chunks, err)
}
if !bytes.Equal(got, want) || len(resp.Trailer) != 0 {
t.Fatalf("%q: %q, net/http %q with trailer %v", chunks, got, want, resp.Trailer)
}
})
}
// TestReadWhole checks that a body read into a buffer of its declared length
// comes out as io.ReadAll reads it, whether it is as long as declared,
// shorter or longer, and that a read error is reported.
func TestReadWhole(t *testing.T) {
for _, tc := range []struct {
body string
declared int64
}{
{"hello", 5}, {"hello", 3}, {"hello", 9}, {"hello", 0}, {"hello", -1}, {"", 4},
{strings.Repeat("x", 4096), 4096}, {strings.Repeat("x", 4097), 4096},
// A declared length far past the body is the peer's word, not a
// buffer to take before any of it has arrived.
{"hello", 1 << 40}, {"hello", 1<<40 + 1}, {strings.Repeat("x", 1<<20+1), 1 << 40},
} {
got, err := readWhole(iotest.OneByteReader(strings.NewReader(tc.body)), tc.declared)
if err != nil || string(got) != tc.body {
t.Errorf("readWhole(%d bytes, %d) = %d bytes, %v; want the body", len(tc.body), tc.declared, len(got), err)
}
}
fail := errors.New("broken")
for _, declared := range []int64{3, 5, 8} {
r := io.MultiReader(strings.NewReader("hello"), iotest.ErrReader(fail))
if _, err := readWhole(r, declared); !errors.Is(err, fail) {
t.Errorf("readWhole(broken after 5 bytes, %d): %v, want %v", declared, err, fail)
}
}
}