1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
16 func TestBodyReadBadTrailer(t
*testing
.T
) {
18 src
: strings
.NewReader("foobar"),
19 hdr
: true, // force reading the trailer
20 r
: bufio
.NewReader(strings
.NewReader("")),
22 buf
:= make([]byte, 7)
23 n
, err
:= b
.Read(buf
[:3])
24 got
:= string(buf
[:n
])
25 if got
!= "foo" || err
!= nil {
26 t
.Fatalf(`first Read = %d (%q), %v; want 3 ("foo")`, n
, got
, err
)
29 n
, err
= b
.Read(buf
[:])
31 if got
!= "bar" || err
!= nil {
32 t
.Fatalf(`second Read = %d (%q), %v; want 3 ("bar")`, n
, got
, err
)
35 n
, err
= b
.Read(buf
[:])
38 t
.Errorf("final Read was successful (%q), expected error from trailer read", got
)
42 func TestFinalChunkedBodyReadEOF(t
*testing
.T
) {
43 res
, err
:= ReadResponse(bufio
.NewReader(strings
.NewReader(
44 "HTTP/1.1 200 OK\r\n"+
45 "Transfer-Encoding: chunked\r\n"+
56 want
:= "Body here\ncontinued"
57 buf
:= make([]byte, len(want
))
58 n
, err
:= res
.Body
.Read(buf
)
59 if n
!= len(want
) || err
!= io
.EOF
{
60 t
.Logf("body = %#v", res
.Body
)
61 t
.Errorf("Read = %v, %v; want %d, EOF", n
, err
, len(want
))
63 if string(buf
) != want
{
64 t
.Errorf("buf = %q; want %q", buf
, want
)
68 func TestDetectInMemoryReaders(t
*testing
.T
) {
76 {bytes
.NewReader(nil), true},
77 {bytes
.NewBuffer(nil), true},
78 {strings
.NewReader(""), true},
80 {ioutil
.NopCloser(pr
), false},
82 {ioutil
.NopCloser(bytes
.NewReader(nil)), true},
83 {ioutil
.NopCloser(bytes
.NewBuffer(nil)), true},
84 {ioutil
.NopCloser(strings
.NewReader("")), true},
86 for i
, tt
:= range tests
{
87 got
:= isKnownInMemoryReader(tt
.r
)
89 t
.Errorf("%d: got = %v; want %v", i
, got
, tt
.want
)