diff --git a/src/encoding/json/stream_test.go b/src/encoding/json/stream_test.go index 1e9d3d0dce6..c2e30408cd8 100644 --- a/src/encoding/json/stream_test.go +++ b/src/encoding/json/stream_test.go @@ -319,43 +319,36 @@ func TestDecodeInStream(t *testing.T) { } -const raw = `{ "foo": "bar" }` +// Test from golang.org/issue/11893 +func TestHTTPDecoding(t *testing.T) { + const raw = `{ "foo": "bar" }` -func makeHTTP() io.ReadCloser { - mux := http.NewServeMux() - mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(raw)) - }) - ts := httptest.NewServer(mux) + })) defer ts.Close() res, err := http.Get(ts.URL) if err != nil { log.Fatalf("GET failed: %v", err) } - return res.Body -} - -func TestHttpDecoding(t *testing.T) { + defer res.Body.Close() foo := struct { Foo string }{} - rc := makeHTTP() - defer rc.Close() - - d := NewDecoder(rc) - err := d.Decode(&foo) + d := NewDecoder(res.Body) + err = d.Decode(&foo) if err != nil { - t.Errorf("Unexpected error %v", err) + t.Fatalf("Decode: %v", err) } if foo.Foo != "bar" { - t.Errorf("Expected \"bar\", was %v", foo.Foo) + t.Errorf("decoded %q; want \"bar\"", foo.Foo) } // make sure we get the EOF the second time err = d.Decode(&foo) if err != io.EOF { - t.Errorf("Expected io.EOF, was %v", err) + t.Errorf("err = %v; want io.EOF", err) } }