diff options
| author | Peter Waldschmidt <peter@waldschmidt.com> | 2015-07-27 21:33:53 -0400 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2015-07-28 02:51:55 +0000 |
| commit | 7e70c2468b6db5f4ebfa59714e9c561ba045e41c (patch) | |
| tree | 7420ae0cdcbf6439fea9a3fc1b1650cf4fe69f74 /src/encoding/json/stream_test.go | |
| parent | a01d90744fe5d12ce8b48e7d3c6e67e954a9fe55 (diff) | |
| download | go-7e70c2468b6db5f4ebfa59714e9c561ba045e41c.tar.xz | |
encoding/json: fix EOF bug decoding HTTP stream
Fixes bug referenced in this thread on golang-dev:
https://groups.google.com/d/topic/golang-dev/U4LSpMzL82c/discussion
Change-Id: If01a2644863f9e5625dd2f95f9d344bda772e12c
Reviewed-on: https://go-review.googlesource.com/12726
Reviewed-by: Russ Cox <rsc@golang.org>
Diffstat (limited to 'src/encoding/json/stream_test.go')
| -rw-r--r-- | src/encoding/json/stream_test.go | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/encoding/json/stream_test.go b/src/encoding/json/stream_test.go index 3aff035fef..1e9d3d0dce 100644 --- a/src/encoding/json/stream_test.go +++ b/src/encoding/json/stream_test.go @@ -8,7 +8,10 @@ import ( "bytes" "io" "io/ioutil" + "log" "net" + "net/http" + "net/http/httptest" "reflect" "strings" "testing" @@ -315,3 +318,44 @@ func TestDecodeInStream(t *testing.T) { } } + +const raw = `{ "foo": "bar" }` + +func makeHTTP() io.ReadCloser { + mux := http.NewServeMux() + mux.HandleFunc("/", 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) { + + foo := struct { + Foo string + }{} + + rc := makeHTTP() + defer rc.Close() + + d := NewDecoder(rc) + err := d.Decode(&foo) + if err != nil { + t.Errorf("Unexpected error %v", err) + } + if foo.Foo != "bar" { + t.Errorf("Expected \"bar\", was %v", 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) + } +} |
