2012-11-13 23:38:25 -07:00
|
|
|
// Copyright 2012 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2014-01-29 05:44:21 -07:00
|
|
|
"io"
|
2012-11-13 23:38:25 -07:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBodyReadBadTrailer(t *testing.T) {
|
|
|
|
b := &body{
|
2014-01-07 11:40:56 -07:00
|
|
|
src: strings.NewReader("foobar"),
|
|
|
|
hdr: true, // force reading the trailer
|
|
|
|
r: bufio.NewReader(strings.NewReader("")),
|
2012-11-13 23:38:25 -07:00
|
|
|
}
|
|
|
|
buf := make([]byte, 7)
|
|
|
|
n, err := b.Read(buf[:3])
|
|
|
|
got := string(buf[:n])
|
|
|
|
if got != "foo" || err != nil {
|
2012-11-16 18:24:43 -07:00
|
|
|
t.Fatalf(`first Read = %d (%q), %v; want 3 ("foo")`, n, got, err)
|
2012-11-13 23:38:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
n, err = b.Read(buf[:])
|
|
|
|
got = string(buf[:n])
|
|
|
|
if got != "bar" || err != nil {
|
2012-11-16 18:24:43 -07:00
|
|
|
t.Fatalf(`second Read = %d (%q), %v; want 3 ("bar")`, n, got, err)
|
2012-11-13 23:38:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
n, err = b.Read(buf[:])
|
|
|
|
got = string(buf[:n])
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("final Read was successful (%q), expected error from trailer read", got)
|
|
|
|
}
|
|
|
|
}
|
2014-01-29 05:44:21 -07:00
|
|
|
|
|
|
|
func TestFinalChunkedBodyReadEOF(t *testing.T) {
|
|
|
|
res, err := ReadResponse(bufio.NewReader(strings.NewReader(
|
|
|
|
"HTTP/1.1 200 OK\r\n"+
|
|
|
|
"Transfer-Encoding: chunked\r\n"+
|
|
|
|
"\r\n"+
|
|
|
|
"0a\r\n"+
|
|
|
|
"Body here\n\r\n"+
|
|
|
|
"09\r\n"+
|
|
|
|
"continued\r\n"+
|
|
|
|
"0\r\n"+
|
|
|
|
"\r\n")), nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
want := "Body here\ncontinued"
|
|
|
|
buf := make([]byte, len(want))
|
|
|
|
n, err := res.Body.Read(buf)
|
|
|
|
if n != len(want) || err != io.EOF {
|
|
|
|
t.Logf("body = %#v", res.Body)
|
|
|
|
t.Errorf("Read = %v, %v; want %d, EOF", n, err, len(want))
|
|
|
|
}
|
|
|
|
if string(buf) != want {
|
|
|
|
t.Errorf("buf = %q; want %q", buf, want)
|
|
|
|
}
|
|
|
|
}
|