diff --git a/core/condition_test.go b/core/condition_test.go index b3bcbf3a..5c63a7b8 100644 --- a/core/condition_test.go +++ b/core/condition_test.go @@ -155,13 +155,6 @@ func TestCondition_evaluate(t *testing.T) { ExpectedSuccess: false, ExpectedOutput: "[BODY].data.name (INVALID) == john", }, - { - Name: "body-jsonpath-complex-len", - Condition: Condition("len([BODY].data.name) == 4"), - Result: &Result{body: []byte("{\"data\": {\"name\": \"john\"}}")}, - ExpectedSuccess: true, - ExpectedOutput: "len([BODY].data.name) == 4", - }, { Name: "body-jsonpath-complex-len-invalid", Condition: Condition("len([BODY].data.name) == john"), @@ -232,175 +225,6 @@ func TestCondition_evaluate(t *testing.T) { ExpectedSuccess: false, ExpectedOutput: "[BODY].data.id (10) < 5", }, - { - Name: "body-len-array", - Condition: Condition("len([BODY]) == 3"), - Result: &Result{body: []byte("[{\"id\": 1}, {\"id\": 2}, {\"id\": 3}]")}, - ExpectedSuccess: true, - ExpectedOutput: "len([BODY]) == 3", - }, - { - Name: "body-len-keyed-array", - Condition: Condition("len([BODY].data) == 3"), - Result: &Result{body: []byte("{\"data\": [{\"id\": 1}, {\"id\": 2}, {\"id\": 3}]}")}, - ExpectedSuccess: true, - ExpectedOutput: "len([BODY].data) == 3", - }, - { - Name: "body-len-array-invalid", - Condition: Condition("len([BODY].data) == 8"), - Result: &Result{body: []byte("{\"name\": \"john.doe\"}")}, - ExpectedSuccess: false, - ExpectedOutput: "len([BODY].data) (INVALID) == 8", - }, - { - Name: "body-len-string", - Condition: Condition("len([BODY]) == 8"), - Result: &Result{body: []byte("john.doe")}, - ExpectedSuccess: true, - ExpectedOutput: "len([BODY]) == 8", - }, - { - Name: "body-len-keyed-string", - Condition: Condition("len([BODY].name) == 8"), - Result: &Result{body: []byte("{\"name\": \"john.doe\"}")}, - ExpectedSuccess: true, - ExpectedOutput: "len([BODY].name) == 8", - }, - { - Name: "body-pattern", - Condition: Condition("[BODY] == pat(*john*)"), - Result: &Result{body: []byte("{\"name\": \"john.doe\"}")}, - ExpectedSuccess: true, - ExpectedOutput: "[BODY] == pat(*john*)", - }, - { - Name: "body-pattern-2", - Condition: Condition("[BODY].name == pat(john*)"), - Result: &Result{body: []byte("{\"name\": \"john.doe\"}")}, - ExpectedSuccess: true, - ExpectedOutput: "[BODY].name == pat(john*)", - }, - { - Name: "body-pattern-failure", - Condition: Condition("[BODY].name == pat(bob*)"), - Result: &Result{body: []byte("{\"name\": \"john.doe\"}")}, - ExpectedSuccess: false, - ExpectedOutput: "[BODY].name (john.doe) == pat(bob*)", - }, - { - Name: "body-pattern-html", - Condition: Condition("[BODY] == pat(*
john.doe
*)"), - Result: &Result{body: []byte(`
john.doe
`)}, - ExpectedSuccess: true, - ExpectedOutput: "[BODY] == pat(*
john.doe
*)", - }, - { - Name: "body-pattern-html-failure", - Condition: Condition("[BODY] == pat(*
john.doe
*)"), - Result: &Result{body: []byte(`
jane.doe
`)}, - ExpectedSuccess: false, - ExpectedOutput: "[BODY] (john.doe*)", - }, - { - Name: "body-pattern-html-failure-alt", - Condition: Condition("pat(*
john.doe
*) == [BODY]"), - Result: &Result{body: []byte(`
jane.doe
`)}, - ExpectedSuccess: false, - ExpectedOutput: "pat(*
john.doe
*) == [BODY] ( 48h (172800000)", }, + { + Name: "no-placeholders", + Condition: Condition("1 == 2"), + Result: &Result{}, + ExpectedSuccess: false, + ExpectedOutput: "1 == 2", + }, + /////////////// + // Functions // + /////////////// + // len + { + Name: "len-body-jsonpath-complex", + Condition: Condition("len([BODY].data.name) == 4"), + Result: &Result{body: []byte("{\"data\": {\"name\": \"john\"}}")}, + ExpectedSuccess: true, + ExpectedOutput: "len([BODY].data.name) == 4", + }, + { + Name: "len-body-array", + Condition: Condition("len([BODY]) == 3"), + Result: &Result{body: []byte("[{\"id\": 1}, {\"id\": 2}, {\"id\": 3}]")}, + ExpectedSuccess: true, + ExpectedOutput: "len([BODY]) == 3", + }, + { + Name: "len-body-keyed-array", + Condition: Condition("len([BODY].data) == 3"), + Result: &Result{body: []byte("{\"data\": [{\"id\": 1}, {\"id\": 2}, {\"id\": 3}]}")}, + ExpectedSuccess: true, + ExpectedOutput: "len([BODY].data) == 3", + }, + { + Name: "len-body-array-invalid", + Condition: Condition("len([BODY].data) == 8"), + Result: &Result{body: []byte("{\"name\": \"john.doe\"}")}, + ExpectedSuccess: false, + ExpectedOutput: "len([BODY].data) (INVALID) == 8", + }, + { + Name: "len-body-string", + Condition: Condition("len([BODY]) == 8"), + Result: &Result{body: []byte("john.doe")}, + ExpectedSuccess: true, + ExpectedOutput: "len([BODY]) == 8", + }, + { + Name: "len-body-keyed-string", + Condition: Condition("len([BODY].name) == 8"), + Result: &Result{body: []byte("{\"name\": \"john.doe\"}")}, + ExpectedSuccess: true, + ExpectedOutput: "len([BODY].name) == 8", + }, + { + Name: "len-body-keyed-int", + Condition: Condition("len([BODY].age) == 2"), + Result: &Result{body: []byte(`{"age":18}`)}, + ExpectedSuccess: true, + ExpectedOutput: "len([BODY].age) == 2", + }, + { + Name: "len-body-keyed-bool", + Condition: Condition("len([BODY].adult) == 4"), + Result: &Result{body: []byte(`{"adult":true}`)}, + ExpectedSuccess: true, + ExpectedOutput: "len([BODY].adult) == 4", + }, + { + Name: "len-body-object-inside-array", + Condition: Condition("len([BODY][0]) == 23"), + Result: &Result{body: []byte(`[{"age":18,"adult":true}]`)}, + ExpectedSuccess: true, + ExpectedOutput: "len([BODY][0]) == 23", + }, + { + Name: "len-body-object-keyed-int-inside-array", + Condition: Condition("len([BODY][0].age) == 2"), + Result: &Result{body: []byte(`[{"age":18,"adult":true}]`)}, + ExpectedSuccess: true, + ExpectedOutput: "len([BODY][0].age) == 2", + }, + { + Name: "len-body-keyed-bool-inside-array", + Condition: Condition("len([BODY][0].adult) == 4"), + Result: &Result{body: []byte(`[{"age":18,"adult":true}]`)}, + ExpectedSuccess: true, + ExpectedOutput: "len([BODY][0].adult) == 4", + }, + { + Name: "len-body-object", + Condition: Condition("len([BODY]) == 20"), + Result: &Result{body: []byte("{\"name\": \"john.doe\"}")}, + ExpectedSuccess: true, + ExpectedOutput: "len([BODY]) == 20", + }, + // pat + { + Name: "pat-body-1", + Condition: Condition("[BODY] == pat(*john*)"), + Result: &Result{body: []byte("{\"name\": \"john.doe\"}")}, + ExpectedSuccess: true, + ExpectedOutput: "[BODY] == pat(*john*)", + }, + { + Name: "pat-body-2", + Condition: Condition("[BODY].name == pat(john*)"), + Result: &Result{body: []byte("{\"name\": \"john.doe\"}")}, + ExpectedSuccess: true, + ExpectedOutput: "[BODY].name == pat(john*)", + }, + { + Name: "pat-body-failure", + Condition: Condition("[BODY].name == pat(bob*)"), + Result: &Result{body: []byte("{\"name\": \"john.doe\"}")}, + ExpectedSuccess: false, + ExpectedOutput: "[BODY].name (john.doe) == pat(bob*)", + }, + { + Name: "pat-body-html", + Condition: Condition("[BODY] == pat(*
john.doe
*)"), + Result: &Result{body: []byte(`
john.doe
`)}, + ExpectedSuccess: true, + ExpectedOutput: "[BODY] == pat(*
john.doe
*)", + }, + { + Name: "pat-body-html-failure", + Condition: Condition("[BODY] == pat(*
john.doe
*)"), + Result: &Result{body: []byte(`
jane.doe
`)}, + ExpectedSuccess: false, + ExpectedOutput: "[BODY] (john.doe*)", + }, + { + Name: "pat-body-html-failure-alt", + Condition: Condition("pat(*
john.doe
*) == [BODY]"), + Result: &Result{body: []byte(`
jane.doe
`)}, + ExpectedSuccess: false, + ExpectedOutput: "pat(*
john.doe
*) == [BODY] ( 1 { return "", 0, fmt.Errorf("couldn't walk through '%s', because '%s' was a string instead of an object", keys[1], currentKey) @@ -50,7 +59,8 @@ func walk(path string, object interface{}) (string, int, error) { case []interface{}: return fmt.Sprintf("%v", value), len(value), nil case interface{}: - return fmt.Sprintf("%v", value), 1, nil + newValue := fmt.Sprintf("%v", value) + return newValue, len(newValue), nil default: return "", 0, fmt.Errorf("couldn't walk through '%s' because type was '%T', but expected 'map[string]interface{}'", currentKey, value) } diff --git a/jsonpath/jsonpath_test.go b/jsonpath/jsonpath_test.go index c65aaf88..41d657c2 100644 --- a/jsonpath/jsonpath_test.go +++ b/jsonpath/jsonpath_test.go @@ -47,7 +47,7 @@ func TestEval(t *testing.T) { ExpectedError: false, }, { - Name: "array-of-maps", + Name: "array-of-objects", Path: "ids[1].id", Data: `{"ids": [{"id": 1}, {"id": 2}]}`, ExpectedOutput: "2", @@ -87,7 +87,15 @@ func TestEval(t *testing.T) { ExpectedError: false, }, { - Name: "array-of-maps-at-root", + Name: "array-of-objects-at-root", + Path: "[0]", + Data: `[{"id": 1}, {"id": 2}]`, + ExpectedOutput: `{"id":1}`, + ExpectedOutputLength: 8, + ExpectedError: false, + }, + { + Name: "array-of-objects-with-int-at-root", Path: "[0].id", Data: `[{"id": 1}, {"id": 2}]`, ExpectedOutput: "1", @@ -95,7 +103,7 @@ func TestEval(t *testing.T) { ExpectedError: false, }, { - Name: "array-of-maps-at-root-and-invalid-index", + Name: "array-of-objects-at-root-and-invalid-index", Path: "[5].id", Data: `[{"id": 1}, {"id": 2}]`, ExpectedOutput: "", @@ -119,7 +127,7 @@ func TestEval(t *testing.T) { ExpectedError: false, }, { - Name: "map-of-nested-arrays", + Name: "object-with-nested-arrays", Path: "data[1][1]", Data: `{"data": [["a", "b", "c"], ["d", "eeeee", "f"]]}`, ExpectedOutput: "eeeee", @@ -127,7 +135,7 @@ func TestEval(t *testing.T) { ExpectedError: false, }, { - Name: "map-of-arrays-of-maps", + Name: "object-with-arrays-of-objects", Path: "data[0].apps[1].name", Data: `{"data": [{"apps": [{"name":"app1"}, {"name":"app2"}, {"name":"app3"}]}]}`, ExpectedOutput: "app2", @@ -135,7 +143,7 @@ func TestEval(t *testing.T) { ExpectedError: false, }, { - Name: "map-of-arrays-of-maps-with-missing-element", + Name: "object-with-arrays-of-objects-with-missing-element", Path: "data[0].apps[1].name", Data: `{"data": [{"apps": []}]}`, ExpectedOutput: "",