github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/pkg/net/http/transfer_test.go (about)

     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.
     4  
     5  package http
     6  
     7  import (
     8  	"bufio"
     9  	"strings"
    10  	"testing"
    11  )
    12  
    13  func TestBodyReadBadTrailer(t *testing.T) {
    14  	b := &body{
    15  		Reader: strings.NewReader("foobar"),
    16  		hdr:    true, // force reading the trailer
    17  		r:      bufio.NewReader(strings.NewReader("")),
    18  	}
    19  	buf := make([]byte, 7)
    20  	n, err := b.Read(buf[:3])
    21  	got := string(buf[:n])
    22  	if got != "foo" || err != nil {
    23  		t.Fatalf(`first Read = %d (%q), %v; want 3 ("foo")`, n, got, err)
    24  	}
    25  
    26  	n, err = b.Read(buf[:])
    27  	got = string(buf[:n])
    28  	if got != "bar" || err != nil {
    29  		t.Fatalf(`second Read = %d (%q), %v; want 3 ("bar")`, n, got, err)
    30  	}
    31  
    32  	n, err = b.Read(buf[:])
    33  	got = string(buf[:n])
    34  	if err == nil {
    35  		t.Errorf("final Read was successful (%q), expected error from trailer read", got)
    36  	}
    37  }