Skip to content

Commit

Permalink
jsonstream: added test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
jkowalski committed Dec 24, 2018
1 parent 641b25a commit 53b4a9a
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions internal/jsonstream/stream_test.go
Expand Up @@ -115,3 +115,49 @@ func TestInvalidHeader(t *testing.T) {
t.Errorf("got incorrect error: %v", err)
}
}

func TestInvalidStream(t *testing.T) {
cases := []string{
`x`,
`{}`,
`{"not-stream":"hdr"}`,
`{{}}`,
`{"stream":"non-hdr"}`,
`{"stream":"hdr","nonEntries":[]}`,
`{"stream":"hdr","entries":{}}`,
`{"stream":"hdr","entries":[]}`,
`{"stream":"hdr","entries":[`,
`{"stream":"hdr","entries":[}`,
`{"stream":"hdr","entries":[]`,
`{"stream":"hdr","entries":[],"summary"`,
`{"stream":"hdr","entries":[],1.222.33`,
`{"stream":"hdr","entries":[],"sxummary":{"x":"1",`,
}

for _, tc := range cases {
r, err := NewReader(strings.NewReader(tc), "hdr", nil)
if err != nil {
if !isInvalidStream(err) {
t.Errorf("got invalid error when creating reader: %v", err)
}
continue
}

for {
v := map[string]interface{}{}
if err := r.Read(v); err != nil {
if err == io.EOF {
break
}
if !isInvalidStream(err) {
t.Errorf("got invalid error when creating reader: %v", err)
}
break
}
}
}
}

func isInvalidStream(e error) bool {
return e != nil && strings.Contains(e.Error(), "invalid stream format")
}

0 comments on commit 53b4a9a

Please sign in to comment.