add tests for error json decode/encode error scenarios

This commit is contained in:
Joel Wetzell
2026-02-09 22:28:55 -06:00
parent a217b729cd
commit 444093d375
2 changed files with 61 additions and 0 deletions

View File

@@ -89,3 +89,36 @@ func TestGoodJsonDecode(t *testing.T) {
}) })
} }
} }
func TestBadJsonDecode(t *testing.T) {
stringEncoder := processor.JsonDecode{}
tests := []struct {
name string
payload any
errorString string
}{
{
name: "non-string input",
payload: []byte("hello"),
errorString: "json.decode processor only accepts a string",
},
{
name: "invalid json",
payload: "{\"address\":\"/hello\",\"args\":}",
errorString: "invalid character '}' looking for beginning of value",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := stringEncoder.Process(t.Context(), test.payload)
if err == nil {
t.Fatalf("json.decode expected to fail but got payload: %+v", got)
}
if err.Error() != test.errorString {
t.Fatalf("json.decode got error '%s', expected '%s'", err.Error(), test.errorString)
}
})
}
}

View File

@@ -83,3 +83,31 @@ func TestGoodJsonEncode(t *testing.T) {
}) })
} }
} }
func TestBadJsonEncode(t *testing.T) {
stringEncoder := processor.JsonEncode{}
tests := []struct {
name string
payload any
errorString string
}{
{
name: "unencodable type",
payload: make(chan int),
errorString: "json: unsupported type: chan int",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := stringEncoder.Process(t.Context(), test.payload)
if err == nil {
t.Fatalf("json.encode expected to fail but got payload: %+v", got)
}
if err.Error() != test.errorString {
t.Fatalf("json.encode got error '%s', expected '%s'", err.Error(), test.errorString)
}
})
}
}