diff --git a/core/condition.go b/core/condition.go index 6b11d8e9..f5a26cd8 100644 --- a/core/condition.go +++ b/core/condition.go @@ -224,6 +224,14 @@ func isEqual(first, second string) bool { return false } } + + // test if inputs are integers + firstInt, err1 := strconv.ParseInt(first, 0, 64) + secondInt, err2 := strconv.ParseInt(second, 0, 64) + if err1 == nil && err2 == nil { + return firstInt == secondInt + } + return first == second } @@ -304,7 +312,7 @@ func sanitizeAndResolveNumerical(list []string, result *Result) (parameters []st if duration, err := time.ParseDuration(element); duration != 0 && err == nil { // If the string is a duration, convert it to milliseconds resolvedNumericalParameters = append(resolvedNumericalParameters, duration.Milliseconds()) - } else if number, err := strconv.ParseInt(element, 10, 64); err != nil { + } else if number, err := strconv.ParseInt(element, 0, 64); err != nil { // It's not an int, so we'll check if it's a float if f, err := strconv.ParseFloat(element, 64); err == nil { // It's a float, but we'll convert it to an int. We're losing precision here, but it's better than diff --git a/core/condition_test.go b/core/condition_test.go index b3cdad56..e4daf237 100644 --- a/core/condition_test.go +++ b/core/condition_test.go @@ -287,6 +287,111 @@ func TestCondition_evaluate(t *testing.T) { ExpectedSuccess: true, ExpectedOutput: "[BODY].data.id > 0", }, + { + Name: "body-jsonpath-hexadecimal-int-using-greater-than", + Condition: Condition("[BODY].data > 0"), + Result: &Result{Body: []byte("{\"data\": \"0x1\"}")}, + ExpectedSuccess: true, + ExpectedOutput: "[BODY].data > 0", + }, + { + Name: "body-jsonpath-hexadecimal-int-using-equal-to-0x1", + Condition: Condition("[BODY].data == 1"), + Result: &Result{Body: []byte("{\"data\": \"0x1\"}")}, + ExpectedSuccess: true, + ExpectedOutput: "[BODY].data == 1", + }, + { + Name: "body-jsonpath-hexadecimal-int-using-equals", + Condition: Condition("[BODY].data == 0x1"), + Result: &Result{Body: []byte("{\"data\": \"0x1\"}")}, + ExpectedSuccess: true, + ExpectedOutput: "[BODY].data == 0x1", + }, + { + Name: "body-jsonpath-hexadecimal-int-using-equal-to-0x2", + Condition: Condition("[BODY].data == 2"), + Result: &Result{Body: []byte("{\"data\": \"0x2\"}")}, + ExpectedSuccess: true, + ExpectedOutput: "[BODY].data == 2", + }, + { + Name: "body-jsonpath-hexadecimal-int-using-equal-to-0xF", + Condition: Condition("[BODY].data == 15"), + Result: &Result{Body: []byte("{\"data\": \"0xF\"}")}, + ExpectedSuccess: true, + ExpectedOutput: "[BODY].data == 15", + }, + { + Name: "body-jsonpath-hexadecimal-int-using-equal-to-0xC0ff33", + Condition: Condition("[BODY].data == 12648243"), + Result: &Result{Body: []byte("{\"data\": \"0xC0ff33\"}")}, + ExpectedSuccess: true, + ExpectedOutput: "[BODY].data == 12648243", + }, + { + Name: "body-jsonpath-hexadecimal-int-len", + Condition: Condition("len([BODY].data) == 3"), + Result: &Result{Body: []byte("{\"data\": \"0x1\"}")}, + ExpectedSuccess: true, + ExpectedOutput: "len([BODY].data) == 3", + }, + { + Name: "body-jsonpath-hexadecimal-int-greater", + Condition: Condition("[BODY].data >= 1"), + Result: &Result{Body: []byte("{\"data\": \"0x01\"}")}, + ExpectedSuccess: true, + ExpectedOutput: "[BODY].data >= 1", + }, + { + Name: "body-jsonpath-hexadecimal-int-0x01-len", + Condition: Condition("len([BODY].data) == 4"), + Result: &Result{Body: []byte("{\"data\": \"0x01\"}")}, + ExpectedSuccess: true, + ExpectedOutput: "len([BODY].data) == 4", + }, + { + Name: "body-jsonpath-octal-int-using-greater-than", + Condition: Condition("[BODY].data > 0"), + Result: &Result{Body: []byte("{\"data\": \"0o1\"}")}, + ExpectedSuccess: true, + ExpectedOutput: "[BODY].data > 0", + }, + { + Name: "body-jsonpath-octal-int-using-equal", + Condition: Condition("[BODY].data == 2"), + Result: &Result{Body: []byte("{\"data\": \"0o2\"}")}, + ExpectedSuccess: true, + ExpectedOutput: "[BODY].data == 2", + }, + { + Name: "body-jsonpath-octal-int-using-equals", + Condition: Condition("[BODY].data == 0o2"), + Result: &Result{Body: []byte("{\"data\": \"0o2\"}")}, + ExpectedSuccess: true, + ExpectedOutput: "[BODY].data == 0o2", + }, + { + Name: "body-jsonpath-binary-int-using-greater-than", + Condition: Condition("[BODY].data > 0"), + Result: &Result{Body: []byte("{\"data\": \"0b1\"}")}, + ExpectedSuccess: true, + ExpectedOutput: "[BODY].data > 0", + }, + { + Name: "body-jsonpath-binary-int-using-equal", + Condition: Condition("[BODY].data == 2"), + Result: &Result{Body: []byte("{\"data\": \"0b0010\"}")}, + ExpectedSuccess: true, + ExpectedOutput: "[BODY].data == 2", + }, + { + Name: "body-jsonpath-binary-int-using-equals", + Condition: Condition("[BODY].data == 0b10"), + Result: &Result{Body: []byte("{\"data\": \"0b0010\"}")}, + ExpectedSuccess: true, + ExpectedOutput: "[BODY].data == 0b10", + }, { Name: "body-jsonpath-complex-int-using-greater-than-failure", Condition: Condition("[BODY].data.id > 5"),