mirror of
https://github.com/TwiN/gatus.git
synced 2024-11-21 23:43:27 +01:00
fix: Prevent jsonpath from causing panic when body is expected to be array but isn't (#392)
* fix: Prevent jsonpath from causing panic when body is expected to be array but isn't Fixes #391
This commit is contained in:
parent
a81a83e2d4
commit
947173bf71
@ -231,6 +231,13 @@ func TestCondition_evaluate(t *testing.T) {
|
|||||||
ExpectedSuccess: true,
|
ExpectedSuccess: true,
|
||||||
ExpectedOutput: "[BODY][0].id == 1",
|
ExpectedOutput: "[BODY][0].id == 1",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "body-jsonpath-when-body-is-array-but-actual-body-is-not",
|
||||||
|
Condition: Condition("[BODY][0].name == test"),
|
||||||
|
Result: &Result{body: []byte("{\"statusCode\": 500, \"message\": \"Internal Server Error\"}")},
|
||||||
|
ExpectedSuccess: false,
|
||||||
|
ExpectedOutput: "[BODY][0].name (INVALID) == test",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
Name: "body-jsonpath-complex-int",
|
Name: "body-jsonpath-complex-int",
|
||||||
Condition: Condition("[BODY].data.id == 1"),
|
Condition: Condition("[BODY].data.id == 1"),
|
||||||
|
@ -93,7 +93,7 @@ func extractValue(currentKey string, value interface{}) interface{} {
|
|||||||
currentKeyWithoutIndex := currentKey[:startOfBracket]
|
currentKeyWithoutIndex := currentKey[:startOfBracket]
|
||||||
// if currentKeyWithoutIndex contains only an index (i.e. [0] or 0)
|
// if currentKeyWithoutIndex contains only an index (i.e. [0] or 0)
|
||||||
if len(currentKeyWithoutIndex) == 0 {
|
if len(currentKeyWithoutIndex) == 0 {
|
||||||
array := value.([]interface{})
|
array, _ := value.([]interface{})
|
||||||
if len(array) > arrayIndex {
|
if len(array) > arrayIndex {
|
||||||
if isNestedArray {
|
if isNestedArray {
|
||||||
return extractValue(currentKey[endOfBracket+1:], array[arrayIndex])
|
return extractValue(currentKey[endOfBracket+1:], array[arrayIndex])
|
||||||
@ -106,7 +106,7 @@ func extractValue(currentKey string, value interface{}) interface{} {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// if currentKeyWithoutIndex contains both a key and an index (i.e. data[0])
|
// if currentKeyWithoutIndex contains both a key and an index (i.e. data[0])
|
||||||
array := value.(map[string]interface{})[currentKeyWithoutIndex].([]interface{})
|
array, _ := value.(map[string]interface{})[currentKeyWithoutIndex].([]interface{})
|
||||||
if len(array) > arrayIndex {
|
if len(array) > arrayIndex {
|
||||||
if isNestedArray {
|
if isNestedArray {
|
||||||
return extractValue(currentKey[endOfBracket+1:], array[arrayIndex])
|
return extractValue(currentKey[endOfBracket+1:], array[arrayIndex])
|
||||||
|
Loading…
Reference in New Issue
Block a user