Add tests for comparing two placeholders
This commit is contained in:
parent
d019942278
commit
bc16ae1794
@ -125,7 +125,7 @@ Here are some examples of conditions you can use:
|
|||||||
| `[BODY].data[0].id == 1` | JSONPath value of `$.data[0].id` is equal to 1 | `{"data":[{"id":1}]}` | |
|
| `[BODY].data[0].id == 1` | JSONPath value of `$.data[0].id` is equal to 1 | `{"data":[{"id":1}]}` | |
|
||||||
| `len([BODY].data) < 5` | Array at JSONPath `$.data` has less than 5 elements | `{"data":[{"id":1}]}` | |
|
| `len([BODY].data) < 5` | Array at JSONPath `$.data` has less than 5 elements | `{"data":[{"id":1}]}` | |
|
||||||
| `len([BODY].name) == 8` | String at JSONPath `$.name` has a length of 8 | `{"name":"john.doe"}` | `{"name":"bob"}` |
|
| `len([BODY].name) == 8` | String at JSONPath `$.name` has a length of 8 | `{"name":"john.doe"}` | `{"name":"bob"}` |
|
||||||
|
| `[BODY].age == [BODY].id` | JSONPath value of `$.age` is equal JSONPath `$.id` | `{"user":{"name":"john"}}` | |
|
||||||
|
|
||||||
### Alerting
|
### Alerting
|
||||||
|
|
||||||
|
@ -113,6 +113,24 @@ func TestCondition_evaluateWithBodyJsonPathComplex(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCondition_evaluateWithBodyJsonPathDoublePlaceholders(t *testing.T) {
|
||||||
|
condition := Condition("[BODY].user.firstName != [BODY].user.lastName")
|
||||||
|
result := &Result{Body: []byte("{\"user\": {\"firstName\": \"john\", \"lastName\": \"doe\"}}")}
|
||||||
|
condition.evaluate(result)
|
||||||
|
if !result.ConditionResults[0].Success {
|
||||||
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCondition_evaluateWithBodyJsonPathDoublePlaceholdersFailure(t *testing.T) {
|
||||||
|
condition := Condition("[BODY].user.firstName == [BODY].user.lastName")
|
||||||
|
result := &Result{Body: []byte("{\"user\": {\"firstName\": \"john\", \"lastName\": \"doe\"}}")}
|
||||||
|
condition.evaluate(result)
|
||||||
|
if result.ConditionResults[0].Success {
|
||||||
|
t.Errorf("Condition '%s' should have been a failure", condition)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithBodyJsonPathLongInt(t *testing.T) {
|
func TestCondition_evaluateWithBodyJsonPathLongInt(t *testing.T) {
|
||||||
condition := Condition("[BODY].data.id == 1")
|
condition := Condition("[BODY].data.id == 1")
|
||||||
result := &Result{Body: []byte("{\"data\": {\"id\": 1}}")}
|
result := &Result{Body: []byte("{\"data\": {\"id\": 1}}")}
|
||||||
|
@ -19,6 +19,7 @@ const (
|
|||||||
InvalidConditionElementSuffix = "(INVALID)"
|
InvalidConditionElementSuffix = "(INVALID)"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// sanitizeAndResolve sanitizes and resolves a list of element and returns the list of resolved elements
|
||||||
func sanitizeAndResolve(list []string, result *Result) []string {
|
func sanitizeAndResolve(list []string, result *Result) []string {
|
||||||
var sanitizedList []string
|
var sanitizedList []string
|
||||||
body := strings.TrimSpace(string(result.Body))
|
body := strings.TrimSpace(string(result.Body))
|
||||||
@ -34,7 +35,7 @@ func sanitizeAndResolve(list []string, result *Result) []string {
|
|||||||
case BodyPlaceHolder:
|
case BodyPlaceHolder:
|
||||||
element = body
|
element = body
|
||||||
default:
|
default:
|
||||||
// if starts with BodyPlaceHolder, then evaluate json path
|
// if contains the BodyPlaceHolder, then evaluate json path
|
||||||
if strings.Contains(element, BodyPlaceHolder) {
|
if strings.Contains(element, BodyPlaceHolder) {
|
||||||
wantLength := false
|
wantLength := false
|
||||||
if strings.HasPrefix(element, LengthFunctionPrefix) && strings.HasSuffix(element, FunctionSuffix) {
|
if strings.HasPrefix(element, LengthFunctionPrefix) && strings.HasSuffix(element, FunctionSuffix) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user