Skip to content

Commit

Permalink
Merge pull request #84 from justinabrahms/invalid-json
Browse files Browse the repository at this point in the history
Error if invalid json is passed.
  • Loading branch information
nscuro committed Mar 30, 2023
2 parents c3606c4 + eaa4df6 commit b1105ec
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion decode.go
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
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 b1105ec

Please sign in to comment.