diff --git a/core/condition.go b/core/condition.go index cd3ce9a7..ec2c7931 100644 --- a/core/condition.go +++ b/core/condition.go @@ -155,7 +155,11 @@ func sanitizeAndResolve(list []string, result *Result) []string { if err.Error() != "unexpected end of JSON input" { result.Errors = append(result.Errors, err.Error()) } - element = fmt.Sprintf("%s %s", element, InvalidConditionElementSuffix) + if wantLength { + element = fmt.Sprintf("len(%s) %s", element, InvalidConditionElementSuffix) + } else { + element = fmt.Sprintf("%s %s", element, InvalidConditionElementSuffix) + } } else { if wantLength { element = fmt.Sprintf("%d", resolvedElementLength) diff --git a/core/condition_test.go b/core/condition_test.go index 45be961a..5ec95e25 100644 --- a/core/condition_test.go +++ b/core/condition_test.go @@ -126,6 +126,19 @@ func TestCondition_evaluateWithInvalidBodyJSONPathComplex(t *testing.T) { } } +func TestCondition_evaluateWithInvalidBodyJSONPathComplexWithLengthFunction(t *testing.T) { + expectedResolvedCondition := "len([BODY].data.name) (INVALID) == john" + condition := Condition("len([BODY].data.name) == john") + result := &Result{Body: []byte("{\"data\": {\"id\": 1}}")} + condition.evaluate(result) + if result.ConditionResults[0].Success { + t.Errorf("Condition '%s' should have been a failure, because the path was invalid", condition) + } + if result.ConditionResults[0].Condition != expectedResolvedCondition { + t.Errorf("Condition '%s' should have resolved to '%s', but resolved to '%s' instead", condition, expectedResolvedCondition, result.ConditionResults[0].Condition) + } +} + func TestCondition_evaluateWithBodyJSONPathDoublePlaceholders(t *testing.T) { condition := Condition("[BODY].user.firstName != [BODY].user.lastName") result := &Result{Body: []byte("{\"user\": {\"firstName\": \"john\", \"lastName\": \"doe\"}}")}