Commit b32ad8bf authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

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
parent 3933cb23
......@@ -10,6 +10,7 @@ import (
"fmt"
. "http"
"http/httptest"
"io"
"io/ioutil"
"os"
"strconv"
......@@ -139,3 +140,41 @@ func TestRedirects(t *testing.T) {
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)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment