diff --git a/core/condition_test.go b/core/condition_test.go index e4daf237..ef2e79ed 100644 --- a/core/condition_test.go +++ b/core/condition_test.go @@ -259,6 +259,27 @@ func TestCondition_evaluate(t *testing.T) { ExpectedSuccess: true, ExpectedOutput: "[BODY][0].id == 1", }, + { + Name: "body-jsonpath-when-body-has-null-parameter", + Condition: Condition("[BODY].data == OK"), + Result: &Result{Body: []byte(`{"data": null}"`)}, + ExpectedSuccess: false, + ExpectedOutput: "[BODY].data (INVALID) == OK", + }, + { + Name: "body-jsonpath-when-body-has-array-with-null", + Condition: Condition("[BODY].items[0] == OK"), + Result: &Result{Body: []byte(`{"items": [null, null]}"`)}, + ExpectedSuccess: false, + ExpectedOutput: "[BODY].items[0] (INVALID) == OK", + }, + { + Name: "body-jsonpath-when-body-is-null", + Condition: Condition("[BODY].data == OK"), + Result: &Result{Body: []byte(`null`)}, + ExpectedSuccess: false, + ExpectedOutput: "[BODY].data (INVALID) == OK", + }, { Name: "body-jsonpath-when-body-is-array-but-actual-body-is-not", Condition: Condition("[BODY][0].name == test"), diff --git a/jsonpath/jsonpath.go b/jsonpath/jsonpath.go index 2c7c10dd..b41e393f 100644 --- a/jsonpath/jsonpath.go +++ b/jsonpath/jsonpath.go @@ -117,8 +117,15 @@ func extractValue(currentKey string, value interface{}) interface{} { } if valueAsSlice, ok := value.([]interface{}); ok { // If the type is a slice, return it + // This happens when the body (value) is a JSON array return valueAsSlice } - // otherwise, it's a map - return value.(map[string]interface{})[currentKey] + if valueAsMap, ok := value.(map[string]interface{}); ok { + // If the value is a map, then we get the currentKey from that map + // This happens when the body (value) is a JSON object + return valueAsMap[currentKey] + } + // If the value is neither a map, nor a slice, nor an index, then we cannot retrieve the currentKey + // from said value. This usually happens when the body (value) is null. + return value }