Skip to content

Commit

Permalink
Error if invalid json is passed.
Browse files Browse the repository at this point in the history
Signed-off-by: Justin Abrahms <[email protected]>
  • Loading branch information
justinabrahms committed Mar 30, 2023
1 parent c3606c4 commit eaa4df6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/json"
"encoding/xml"
"io"
"io/ioutil"
)

type BOMDecoder interface {
Expand All @@ -40,7 +41,11 @@ type jsonBOMDecoder struct {

// Decode implements the BOMDecoder interface.
func (j jsonBOMDecoder) Decode(bom *BOM) error {
return json.NewDecoder(j.reader).Decode(bom)
bytes, err := ioutil.ReadAll(j.reader)
if err != nil {
return err
}
return json.Unmarshal(bytes, bom)
}

type xmlBOMDecoder struct {
Expand Down
12 changes: 12 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ func TestNewBOMDecoder(t *testing.T) {
assert.IsType(t, &xmlBOMDecoder{}, NewBOMDecoder(nil, BOMFileFormatXML))
}

func TestXmlBOMDecoder_Decode_InvalidXML(t *testing.T) {
var bom BOM
err := NewBOMDecoder(strings.NewReader("invalid"), BOMFileFormatXML).Decode(&bom)
require.Error(t, err)
}

func TestJsonBOMDecoder_Decode_InvalidJson(t *testing.T) {
var bom BOM
err := NewBOMDecoder(strings.NewReader("{}invalid"), BOMFileFormatJSON).Decode(&bom)
require.Error(t, err)
}

func TestXmlBOMDecoder_Decode(t *testing.T) {
t.Run("ShouldSetSpecVersion", func(t *testing.T) {
testCases := []struct {
Expand Down

0 comments on commit eaa4df6

Please sign in to comment.