Wait for io.EOF when decoding a stream, fixes #123

This commit is contained in:
Viacheslav Poturaev 2021-02-09 17:56:27 +01:00
parent 29dd8efd6f
commit 1926476780
2 changed files with 40 additions and 1 deletions

View File

@ -75,7 +75,7 @@ func (s *stream) read() bool {
buf[last] = nul
n, err := s.r.Read(buf[:last])
s.length = s.cursor + int64(n)
if n < last || err == io.EOF {
if err == io.EOF {
s.allRead = true
} else if err != nil {
return false

View File

@ -6,6 +6,7 @@ package json_test
import (
"bytes"
"compress/gzip"
"io"
"io/ioutil"
"log"
@ -463,3 +464,41 @@ func TestHTTPDecoding(t *testing.T) {
t.Errorf("err = %v; want io.EOF", err)
}
}
func TestGzipStreaming(t *testing.T) {
type someStruct struct {
ID int `json:"id"`
Text []string `json:"text"`
}
manyItems := strings.Repeat(`"Quis autem vel eum iure reprehenderit, qui in ea voluptate velit esse, quam`+
` nihil molestiae consequatur, vel illum, qui dolorem eum fugiat, quo voluptas nulla pariatur?",`, 400)
longJSON := `{"id":123,"text":[` + manyItems[0:len(manyItems)-1] + `]}`
compressed := bytes.NewBuffer(nil)
gw := gzip.NewWriter(compressed)
_, err := io.Copy(gw, bytes.NewReader([]byte(longJSON)))
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
err = gw.Close()
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
gr, err := gzip.NewReader(bytes.NewReader(compressed.Bytes()))
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
var v someStruct
dec := json.NewDecoder(gr)
err = dec.Decode(&v)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
}