1
0
mirror of https://github.com/golang/go synced 2024-11-25 03:07:56 -07:00

http: Client test for streaming responses (no code changes)

I had a report that this was broken. It seems fine.

I think the reporter was just never flushing their response
headers.  If I omit the test server's initial Flush I get the
same behavior as reported. (a hang at Client.Get)

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/4552062
This commit is contained in:
Brad Fitzpatrick 2011-05-24 09:02:01 -07:00
parent 3933cb2371
commit b32ad8bff5

View File

@ -10,6 +10,7 @@ import (
"fmt" "fmt"
. "http" . "http"
"http/httptest" "http/httptest"
"io"
"io/ioutil" "io/ioutil"
"os" "os"
"strconv" "strconv"
@ -139,3 +140,41 @@ func TestRedirects(t *testing.T) {
t.Errorf("with redirects forbidden, expected error %q, got %q", e, g) t.Errorf("with redirects forbidden, expected error %q, got %q", e, g)
} }
} }
func TestStreamingGet(t *testing.T) {
say := make(chan string)
ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
w.(Flusher).Flush()
for str := range say {
w.Write([]byte(str))
w.(Flusher).Flush()
}
}))
defer ts.Close()
c := &Client{}
res, err := c.Get(ts.URL)
if err != nil {
t.Fatal(err)
}
var buf [10]byte
for _, str := range []string{"i", "am", "also", "known", "as", "comet"} {
say <- str
n, err := io.ReadFull(res.Body, buf[0:len(str)])
if err != nil {
t.Fatalf("ReadFull on %q: %v", str, err)
}
if n != len(str) {
t.Fatalf("Receiving %q, only read %d bytes", str, n)
}
got := string(buf[0:n])
if got != str {
t.Fatalf("Expected %q, got %q", str, got)
}
}
close(say)
_, err = io.ReadFull(res.Body, buf[0:1])
if err != os.EOF {
t.Fatalf("at end expected EOF, got %v", err)
}
}