2019-09-07 02:34:10 +02:00
package core
import (
2022-12-06 07:37:05 +01:00
"errors"
"fmt"
2020-11-15 18:47:28 +01:00
"strconv"
2019-09-07 02:34:10 +02:00
"testing"
2020-04-10 22:34:20 +02:00
"time"
2019-09-07 02:34:10 +02:00
)
2022-12-06 07:37:05 +01:00
func TestCondition_Validate ( t * testing . T ) {
scenarios := [ ] struct {
condition Condition
expectedErr error
} {
{ condition : "[STATUS] == 200" , expectedErr : nil } ,
{ condition : "[STATUS] != 200" , expectedErr : nil } ,
{ condition : "[STATUS] <= 200" , expectedErr : nil } ,
{ condition : "[STATUS] >= 200" , expectedErr : nil } ,
{ condition : "[STATUS] < 200" , expectedErr : nil } ,
{ condition : "[STATUS] > 200" , expectedErr : nil } ,
{ condition : "[STATUS] == any(200, 201, 202, 203)" , expectedErr : nil } ,
{ condition : "[STATUS] == [BODY].status" , expectedErr : nil } ,
2022-12-07 03:57:24 +01:00
{ condition : "[CONNECTED] == true" , expectedErr : nil } ,
{ condition : "[RESPONSE_TIME] < 500" , expectedErr : nil } ,
{ condition : "[IP] == 127.0.0.1" , expectedErr : nil } ,
{ condition : "[BODY] == 1" , expectedErr : nil } ,
2022-12-06 07:37:05 +01:00
{ condition : "[BODY].test == wat" , expectedErr : nil } ,
{ condition : "[BODY].test.wat == wat" , expectedErr : nil } ,
2022-12-07 03:57:24 +01:00
{ condition : "[BODY].age == [BODY].id" , expectedErr : nil } ,
2022-12-06 07:37:05 +01:00
{ condition : "[BODY].users[0].id == 1" , expectedErr : nil } ,
{ condition : "len([BODY].users) == 100" , expectedErr : nil } ,
2022-12-07 03:57:24 +01:00
{ condition : "len([BODY].data) < 5" , expectedErr : nil } ,
{ condition : "has([BODY].errors) == false" , expectedErr : nil } ,
{ condition : "has([BODY].users[0].name) == true" , expectedErr : nil } ,
{ condition : "[BODY].name == pat(john*)" , expectedErr : nil } ,
{ condition : "[CERTIFICATE_EXPIRATION] > 48h" , expectedErr : nil } ,
{ condition : "[DOMAIN_EXPIRATION] > 720h" , expectedErr : nil } ,
2022-12-06 07:37:05 +01:00
{ condition : "raw == raw" , expectedErr : nil } ,
{ condition : "[STATUS] ? 201" , expectedErr : errors . New ( "invalid condition: [STATUS] ? 201" ) } ,
{ condition : "[STATUS]==201" , expectedErr : errors . New ( "invalid condition: [STATUS]==201" ) } ,
{ condition : "[STATUS] = = 201" , expectedErr : errors . New ( "invalid condition: [STATUS] = = 201" ) } ,
2022-12-07 03:57:24 +01:00
{ condition : "[STATUS] ==" , expectedErr : errors . New ( "invalid condition: [STATUS] ==" ) } ,
{ condition : "[STATUS]" , expectedErr : errors . New ( "invalid condition: [STATUS]" ) } ,
2022-12-06 07:37:05 +01:00
// FIXME: Should return an error, but doesn't because jsonpath isn't evaluated due to body being empty in Condition.Validate()
//{condition: "len([BODY].users == 100", expectedErr: nil},
}
for _ , scenario := range scenarios {
t . Run ( string ( scenario . condition ) , func ( t * testing . T ) {
if err := scenario . condition . Validate ( ) ; fmt . Sprint ( err ) != fmt . Sprint ( scenario . expectedErr ) {
t . Errorf ( "expected err %v, got %v" , scenario . expectedErr , err )
}
} )
}
}
2021-03-11 03:49:13 +01:00
func TestCondition_evaluate ( t * testing . T ) {
2021-10-01 06:00:11 +02:00
scenarios := [ ] struct {
2021-09-15 01:34:46 +02:00
Name string
Condition Condition
Result * Result
DontResolveFailedConditions bool
ExpectedSuccess bool
ExpectedOutput string
2021-10-01 06:00:11 +02:00
} {
2021-03-11 03:49:13 +01:00
{
Name : "ip" ,
Condition : Condition ( "[IP] == 127.0.0.1" ) ,
Result : & Result { IP : "127.0.0.1" } ,
ExpectedSuccess : true ,
ExpectedOutput : "[IP] == 127.0.0.1" ,
} ,
{
Name : "status" ,
Condition : Condition ( "[STATUS] == 200" ) ,
Result : & Result { HTTPStatus : 200 } ,
ExpectedSuccess : true ,
ExpectedOutput : "[STATUS] == 200" ,
} ,
{
Name : "status-failure" ,
Condition : Condition ( "[STATUS] == 200" ) ,
Result : & Result { HTTPStatus : 500 } ,
ExpectedSuccess : false ,
ExpectedOutput : "[STATUS] (500) == 200" ,
} ,
{
Name : "status-using-less-than" ,
Condition : Condition ( "[STATUS] < 300" ) ,
Result : & Result { HTTPStatus : 201 } ,
ExpectedSuccess : true ,
ExpectedOutput : "[STATUS] < 300" ,
} ,
{
Name : "status-using-less-than-failure" ,
Condition : Condition ( "[STATUS] < 300" ) ,
Result : & Result { HTTPStatus : 404 } ,
ExpectedSuccess : false ,
ExpectedOutput : "[STATUS] (404) < 300" ,
} ,
{
Name : "response-time-using-less-than" ,
Condition : Condition ( "[RESPONSE_TIME] < 500" ) ,
Result : & Result { Duration : 50 * time . Millisecond } ,
ExpectedSuccess : true ,
ExpectedOutput : "[RESPONSE_TIME] < 500" ,
} ,
{
Name : "response-time-using-less-than-with-duration" ,
Condition : Condition ( "[RESPONSE_TIME] < 1s" ) ,
Result : & Result { Duration : 50 * time . Millisecond } ,
ExpectedSuccess : true ,
ExpectedOutput : "[RESPONSE_TIME] < 1s" ,
} ,
{
Name : "response-time-using-less-than-invalid" ,
Condition : Condition ( "[RESPONSE_TIME] < potato" ) ,
Result : & Result { Duration : 50 * time . Millisecond } ,
ExpectedSuccess : false ,
ExpectedOutput : "[RESPONSE_TIME] (50) < potato (0)" , // Non-numerical values automatically resolve to 0
} ,
{
Name : "response-time-using-greater-than" ,
Condition : Condition ( "[RESPONSE_TIME] > 500" ) ,
Result : & Result { Duration : 750 * time . Millisecond } ,
ExpectedSuccess : true ,
ExpectedOutput : "[RESPONSE_TIME] > 500" ,
} ,
{
Name : "response-time-using-greater-than-with-duration" ,
Condition : Condition ( "[RESPONSE_TIME] > 1s" ) ,
Result : & Result { Duration : 2 * time . Second } ,
ExpectedSuccess : true ,
ExpectedOutput : "[RESPONSE_TIME] > 1s" ,
} ,
{
Name : "response-time-using-greater-than-or-equal-to-equal" ,
Condition : Condition ( "[RESPONSE_TIME] >= 500" ) ,
Result : & Result { Duration : 500 * time . Millisecond } ,
ExpectedSuccess : true ,
ExpectedOutput : "[RESPONSE_TIME] >= 500" ,
} ,
{
Name : "response-time-using-greater-than-or-equal-to-greater" ,
Condition : Condition ( "[RESPONSE_TIME] >= 500" ) ,
Result : & Result { Duration : 499 * time . Millisecond } ,
ExpectedSuccess : false ,
ExpectedOutput : "[RESPONSE_TIME] (499) >= 500" ,
} ,
{
Name : "response-time-using-greater-than-or-equal-to-failure" ,
Condition : Condition ( "[RESPONSE_TIME] >= 500" ) ,
Result : & Result { Duration : 499 * time . Millisecond } ,
ExpectedSuccess : false ,
ExpectedOutput : "[RESPONSE_TIME] (499) >= 500" ,
} ,
{
Name : "response-time-using-less-than-or-equal-to-equal" ,
Condition : Condition ( "[RESPONSE_TIME] <= 500" ) ,
Result : & Result { Duration : 500 * time . Millisecond } ,
ExpectedSuccess : true ,
ExpectedOutput : "[RESPONSE_TIME] <= 500" ,
} ,
{
Name : "response-time-using-less-than-or-equal-to-less" ,
Condition : Condition ( "[RESPONSE_TIME] <= 500" ) ,
Result : & Result { Duration : 25 * time . Millisecond } ,
ExpectedSuccess : true ,
ExpectedOutput : "[RESPONSE_TIME] <= 500" ,
} ,
{
Name : "response-time-using-less-than-or-equal-to-failure" ,
Condition : Condition ( "[RESPONSE_TIME] <= 500" ) ,
Result : & Result { Duration : 750 * time . Millisecond } ,
ExpectedSuccess : false ,
ExpectedOutput : "[RESPONSE_TIME] (750) <= 500" ,
} ,
{
Name : "body" ,
Condition : Condition ( "[BODY] == test" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "test" ) } ,
2021-03-11 03:49:13 +01:00
ExpectedSuccess : true ,
ExpectedOutput : "[BODY] == test" ,
} ,
2023-01-19 07:37:21 +01:00
{
Name : "body-numerical-equal" ,
Condition : Condition ( "[BODY] == 123" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "123" ) } ,
2023-01-19 07:37:21 +01:00
ExpectedSuccess : true ,
ExpectedOutput : "[BODY] == 123" ,
} ,
{
Name : "body-numerical-less-than" ,
Condition : Condition ( "[BODY] < 124" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "123" ) } ,
2023-01-19 07:37:21 +01:00
ExpectedSuccess : true ,
ExpectedOutput : "[BODY] < 124" ,
} ,
{
Name : "body-numerical-greater-than" ,
Condition : Condition ( "[BODY] > 122" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "123" ) } ,
2023-01-19 07:37:21 +01:00
ExpectedSuccess : true ,
ExpectedOutput : "[BODY] > 122" ,
} ,
{
Name : "body-numerical-greater-than-failure" ,
Condition : Condition ( "[BODY] > 123" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "100" ) } ,
2023-01-19 07:37:21 +01:00
ExpectedSuccess : false ,
ExpectedOutput : "[BODY] (100) > 123" ,
} ,
2021-03-11 03:49:13 +01:00
{
Name : "body-jsonpath" ,
Condition : Condition ( "[BODY].status == UP" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "{\"status\":\"UP\"}" ) } ,
2021-03-11 03:49:13 +01:00
ExpectedSuccess : true ,
ExpectedOutput : "[BODY].status == UP" ,
} ,
{
Name : "body-jsonpath-complex" ,
Condition : Condition ( "[BODY].data.name == john" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "{\"data\": {\"id\": 1, \"name\": \"john\"}}" ) } ,
2021-03-11 03:49:13 +01:00
ExpectedSuccess : true ,
ExpectedOutput : "[BODY].data.name == john" ,
} ,
{
Name : "body-jsonpath-complex-invalid" ,
Condition : Condition ( "[BODY].data.name == john" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "{\"data\": {\"id\": 1}}" ) } ,
2021-03-11 03:49:13 +01:00
ExpectedSuccess : false ,
ExpectedOutput : "[BODY].data.name (INVALID) == john" ,
} ,
{
Name : "body-jsonpath-complex-len-invalid" ,
Condition : Condition ( "len([BODY].data.name) == john" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "{\"data\": {\"id\": 1}}" ) } ,
2021-03-11 03:49:13 +01:00
ExpectedSuccess : false ,
ExpectedOutput : "len([BODY].data.name) (INVALID) == john" ,
} ,
{
Name : "body-jsonpath-double-placeholder" ,
Condition : Condition ( "[BODY].user.firstName != [BODY].user.lastName" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "{\"user\": {\"firstName\": \"john\", \"lastName\": \"doe\"}}" ) } ,
2021-03-11 03:49:13 +01:00
ExpectedSuccess : true ,
ExpectedOutput : "[BODY].user.firstName != [BODY].user.lastName" ,
} ,
{
Name : "body-jsonpath-double-placeholder-failure" ,
Condition : Condition ( "[BODY].user.firstName == [BODY].user.lastName" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "{\"user\": {\"firstName\": \"john\", \"lastName\": \"doe\"}}" ) } ,
2021-03-11 03:49:13 +01:00
ExpectedSuccess : false ,
ExpectedOutput : "[BODY].user.firstName (john) == [BODY].user.lastName (doe)" ,
} ,
2021-05-09 19:28:22 +02:00
{
Name : "body-jsonpath-when-body-is-array" ,
Condition : Condition ( "[BODY][0].id == 1" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "[{\"id\": 1}, {\"id\": 2}]" ) } ,
2021-05-09 19:28:22 +02:00
ExpectedSuccess : true ,
ExpectedOutput : "[BODY][0].id == 1" ,
} ,
2022-12-23 15:55:17 +01:00
{
Name : "body-jsonpath-when-body-is-array-but-actual-body-is-not" ,
Condition : Condition ( "[BODY][0].name == test" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "{\"statusCode\": 500, \"message\": \"Internal Server Error\"}" ) } ,
2022-12-23 15:55:17 +01:00
ExpectedSuccess : false ,
ExpectedOutput : "[BODY][0].name (INVALID) == test" ,
} ,
2021-03-11 03:49:13 +01:00
{
Name : "body-jsonpath-complex-int" ,
Condition : Condition ( "[BODY].data.id == 1" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "{\"data\": {\"id\": 1}}" ) } ,
2021-03-11 03:49:13 +01:00
ExpectedSuccess : true ,
ExpectedOutput : "[BODY].data.id == 1" ,
} ,
{
Name : "body-jsonpath-complex-array-int" ,
Condition : Condition ( "[BODY].data[1].id == 2" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "{\"data\": [{\"id\": 1}, {\"id\": 2}, {\"id\": 3}]}" ) } ,
2021-03-11 03:49:13 +01:00
ExpectedSuccess : true ,
ExpectedOutput : "[BODY].data[1].id == 2" ,
} ,
{
Name : "body-jsonpath-complex-int-using-greater-than" ,
Condition : Condition ( "[BODY].data.id > 0" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "{\"data\": {\"id\": 1}}" ) } ,
2021-03-11 03:49:13 +01:00
ExpectedSuccess : true ,
ExpectedOutput : "[BODY].data.id > 0" ,
} ,
{
Name : "body-jsonpath-complex-int-using-greater-than-failure" ,
Condition : Condition ( "[BODY].data.id > 5" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "{\"data\": {\"id\": 1}}" ) } ,
2021-03-11 03:49:13 +01:00
ExpectedSuccess : false ,
ExpectedOutput : "[BODY].data.id (1) > 5" ,
} ,
2023-02-16 01:30:29 +01:00
{
Name : "body-jsonpath-float-using-greater-than-issue433" , // As of v5.3.1, Gatus will convert a float to an int. We're losing precision, but it's better than just returning 0
Condition : Condition ( "[BODY].balance > 100" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( ` { "balance": "123.40000000000005"} ` ) } ,
2023-02-16 01:30:29 +01:00
ExpectedSuccess : true ,
ExpectedOutput : "[BODY].balance > 100" ,
} ,
2021-03-11 03:49:13 +01:00
{
Name : "body-jsonpath-complex-int-using-less-than" ,
Condition : Condition ( "[BODY].data.id < 5" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "{\"data\": {\"id\": 2}}" ) } ,
2021-03-11 03:49:13 +01:00
ExpectedSuccess : true ,
ExpectedOutput : "[BODY].data.id < 5" ,
} ,
{
Name : "body-jsonpath-complex-int-using-less-than-failure" ,
Condition : Condition ( "[BODY].data.id < 5" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "{\"data\": {\"id\": 10}}" ) } ,
2021-03-11 03:49:13 +01:00
ExpectedSuccess : false ,
ExpectedOutput : "[BODY].data.id (10) < 5" ,
} ,
{
2022-11-19 23:25:40 +01:00
Name : "connected" ,
Condition : Condition ( "[CONNECTED] == true" ) ,
Result : & Result { Connected : true } ,
ExpectedSuccess : true ,
ExpectedOutput : "[CONNECTED] == true" ,
} ,
{
Name : "connected-failure" ,
Condition : Condition ( "[CONNECTED] == true" ) ,
Result : & Result { Connected : false } ,
ExpectedSuccess : false ,
ExpectedOutput : "[CONNECTED] (false) == true" ,
} ,
{
Name : "certificate-expiration-not-set" ,
Condition : Condition ( "[CERTIFICATE_EXPIRATION] == 0" ) ,
Result : & Result { } ,
ExpectedSuccess : true ,
ExpectedOutput : "[CERTIFICATE_EXPIRATION] == 0" ,
} ,
{
Name : "certificate-expiration-greater-than-numerical" ,
Condition : Condition ( "[CERTIFICATE_EXPIRATION] > " + strconv . FormatInt ( ( time . Hour * 24 * 28 ) . Milliseconds ( ) , 10 ) ) ,
Result : & Result { CertificateExpiration : time . Hour * 24 * 60 } ,
ExpectedSuccess : true ,
ExpectedOutput : "[CERTIFICATE_EXPIRATION] > 2419200000" ,
} ,
{
Name : "certificate-expiration-greater-than-numerical-failure" ,
Condition : Condition ( "[CERTIFICATE_EXPIRATION] > " + strconv . FormatInt ( ( time . Hour * 24 * 28 ) . Milliseconds ( ) , 10 ) ) ,
Result : & Result { CertificateExpiration : time . Hour * 24 * 14 } ,
ExpectedSuccess : false ,
ExpectedOutput : "[CERTIFICATE_EXPIRATION] (1209600000) > 2419200000" ,
} ,
{
Name : "certificate-expiration-greater-than-duration" ,
Condition : Condition ( "[CERTIFICATE_EXPIRATION] > 12h" ) ,
Result : & Result { CertificateExpiration : 24 * time . Hour } ,
ExpectedSuccess : true ,
ExpectedOutput : "[CERTIFICATE_EXPIRATION] > 12h" ,
} ,
{
Name : "certificate-expiration-greater-than-duration" ,
Condition : Condition ( "[CERTIFICATE_EXPIRATION] > 48h" ) ,
Result : & Result { CertificateExpiration : 24 * time . Hour } ,
ExpectedSuccess : false ,
ExpectedOutput : "[CERTIFICATE_EXPIRATION] (86400000) > 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" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "{\"data\": {\"name\": \"john\"}}" ) } ,
2022-11-19 23:25:40 +01:00
ExpectedSuccess : true ,
ExpectedOutput : "len([BODY].data.name) == 4" ,
} ,
{
Name : "len-body-array" ,
2022-11-04 01:46:35 +01:00
Condition : Condition ( "len([BODY]) == 3" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "[{\"id\": 1}, {\"id\": 2}, {\"id\": 3}]" ) } ,
2022-11-04 01:46:35 +01:00
ExpectedSuccess : true ,
ExpectedOutput : "len([BODY]) == 3" ,
} ,
{
2022-11-19 23:25:40 +01:00
Name : "len-body-keyed-array" ,
2021-03-11 03:49:13 +01:00
Condition : Condition ( "len([BODY].data) == 3" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "{\"data\": [{\"id\": 1}, {\"id\": 2}, {\"id\": 3}]}" ) } ,
2021-03-11 03:49:13 +01:00
ExpectedSuccess : true ,
ExpectedOutput : "len([BODY].data) == 3" ,
} ,
{
2022-11-19 23:25:40 +01:00
Name : "len-body-array-invalid" ,
2021-03-11 03:49:13 +01:00
Condition : Condition ( "len([BODY].data) == 8" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "{\"name\": \"john.doe\"}" ) } ,
2021-03-11 03:49:13 +01:00
ExpectedSuccess : false ,
ExpectedOutput : "len([BODY].data) (INVALID) == 8" ,
} ,
{
2022-11-19 23:25:40 +01:00
Name : "len-body-string" ,
2022-11-04 01:46:35 +01:00
Condition : Condition ( "len([BODY]) == 8" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "john.doe" ) } ,
2022-11-04 01:46:35 +01:00
ExpectedSuccess : true ,
ExpectedOutput : "len([BODY]) == 8" ,
} ,
{
2022-11-19 23:25:40 +01:00
Name : "len-body-keyed-string" ,
2021-03-11 03:49:13 +01:00
Condition : Condition ( "len([BODY].name) == 8" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "{\"name\": \"john.doe\"}" ) } ,
2021-03-11 03:49:13 +01:00
ExpectedSuccess : true ,
ExpectedOutput : "len([BODY].name) == 8" ,
} ,
{
2022-11-19 23:25:40 +01:00
Name : "len-body-keyed-int" ,
Condition : Condition ( "len([BODY].age) == 2" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( ` { "age":18} ` ) } ,
2022-11-19 23:25:40 +01:00
ExpectedSuccess : true ,
ExpectedOutput : "len([BODY].age) == 2" ,
} ,
{
Name : "len-body-keyed-bool" ,
Condition : Condition ( "len([BODY].adult) == 4" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( ` { "adult":true} ` ) } ,
2022-11-19 23:25:40 +01:00
ExpectedSuccess : true ,
ExpectedOutput : "len([BODY].adult) == 4" ,
} ,
{
Name : "len-body-object-inside-array" ,
Condition : Condition ( "len([BODY][0]) == 23" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( ` [ { "age":18,"adult":true}] ` ) } ,
2022-11-19 23:25:40 +01:00
ExpectedSuccess : true ,
ExpectedOutput : "len([BODY][0]) == 23" ,
} ,
{
Name : "len-body-object-keyed-int-inside-array" ,
Condition : Condition ( "len([BODY][0].age) == 2" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( ` [ { "age":18,"adult":true}] ` ) } ,
2022-11-19 23:25:40 +01:00
ExpectedSuccess : true ,
ExpectedOutput : "len([BODY][0].age) == 2" ,
} ,
{
Name : "len-body-keyed-bool-inside-array" ,
Condition : Condition ( "len([BODY][0].adult) == 4" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( ` [ { "age":18,"adult":true}] ` ) } ,
2022-11-19 23:25:40 +01:00
ExpectedSuccess : true ,
ExpectedOutput : "len([BODY][0].adult) == 4" ,
} ,
{
Name : "len-body-object" ,
Condition : Condition ( "len([BODY]) == 20" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "{\"name\": \"john.doe\"}" ) } ,
2022-11-19 23:25:40 +01:00
ExpectedSuccess : true ,
ExpectedOutput : "len([BODY]) == 20" ,
} ,
// pat
{
Name : "pat-body-1" ,
2021-03-11 03:49:13 +01:00
Condition : Condition ( "[BODY] == pat(*john*)" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "{\"name\": \"john.doe\"}" ) } ,
2021-03-11 03:49:13 +01:00
ExpectedSuccess : true ,
ExpectedOutput : "[BODY] == pat(*john*)" ,
} ,
{
2022-11-19 23:25:40 +01:00
Name : "pat-body-2" ,
2021-03-11 03:49:13 +01:00
Condition : Condition ( "[BODY].name == pat(john*)" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "{\"name\": \"john.doe\"}" ) } ,
2021-03-11 03:49:13 +01:00
ExpectedSuccess : true ,
ExpectedOutput : "[BODY].name == pat(john*)" ,
} ,
{
2022-11-19 23:25:40 +01:00
Name : "pat-body-failure" ,
2021-03-11 03:49:13 +01:00
Condition : Condition ( "[BODY].name == pat(bob*)" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "{\"name\": \"john.doe\"}" ) } ,
2021-03-11 03:49:13 +01:00
ExpectedSuccess : false ,
ExpectedOutput : "[BODY].name (john.doe) == pat(bob*)" ,
} ,
{
2022-11-19 23:25:40 +01:00
Name : "pat-body-html" ,
2021-03-11 03:49:13 +01:00
Condition : Condition ( "[BODY] == pat(*<div id=\"user\">john.doe</div>*)" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( ` <!DOCTYPE html><html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body><div id="user">john.doe</div></body></html> ` ) } ,
2021-03-11 03:49:13 +01:00
ExpectedSuccess : true ,
ExpectedOutput : "[BODY] == pat(*<div id=\"user\">john.doe</div>*)" ,
} ,
2021-03-14 18:05:16 +01:00
{
2022-11-19 23:25:40 +01:00
Name : "pat-body-html-failure" ,
2021-03-14 18:05:16 +01:00
Condition : Condition ( "[BODY] == pat(*<div id=\"user\">john.doe</div>*)" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( ` <!DOCTYPE html><html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body><div id="user">jane.doe</div></body></html> ` ) } ,
2021-03-14 18:05:16 +01:00
ExpectedSuccess : false ,
ExpectedOutput : "[BODY] (<!DOCTYPE html><html lang...(truncated)) == pat(*<div id=\"user\">john.doe</div>*)" ,
} ,
{
2022-11-19 23:25:40 +01:00
Name : "pat-body-html-failure-alt" ,
2021-03-14 18:05:16 +01:00
Condition : Condition ( "pat(*<div id=\"user\">john.doe</div>*) == [BODY]" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( ` <!DOCTYPE html><html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body><div id="user">jane.doe</div></body></html> ` ) } ,
2021-03-14 18:05:16 +01:00
ExpectedSuccess : false ,
ExpectedOutput : "pat(*<div id=\"user\">john.doe</div>*) == [BODY] (<!DOCTYPE html><html lang...(truncated))" ,
} ,
2022-11-17 00:27:28 +01:00
{
2022-11-19 23:25:40 +01:00
Name : "pat-body-in-array" ,
2022-11-17 00:27:28 +01:00
Condition : Condition ( "[BODY].data == pat(*Whatever*)" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "{\"data\": [\"hello\", \"world\", \"Whatever\"]}" ) } ,
2022-11-17 00:27:28 +01:00
ExpectedSuccess : true ,
ExpectedOutput : "[BODY].data == pat(*Whatever*)" ,
} ,
2021-03-11 03:49:13 +01:00
{
2022-11-19 23:25:40 +01:00
Name : "pat-ip" ,
2021-03-11 03:49:13 +01:00
Condition : Condition ( "[IP] == pat(10.*)" ) ,
Result : & Result { IP : "10.0.0.0" } ,
ExpectedSuccess : true ,
ExpectedOutput : "[IP] == pat(10.*)" ,
} ,
{
2022-11-19 23:25:40 +01:00
Name : "pat-ip-failure" ,
2021-03-11 03:49:13 +01:00
Condition : Condition ( "[IP] == pat(10.*)" ) ,
Result : & Result { IP : "255.255.255.255" } ,
ExpectedSuccess : false ,
ExpectedOutput : "[IP] (255.255.255.255) == pat(10.*)" ,
} ,
{
2022-11-19 23:25:40 +01:00
Name : "pat-status" ,
2021-03-11 03:49:13 +01:00
Condition : Condition ( "[STATUS] == pat(4*)" ) ,
Result : & Result { HTTPStatus : 404 } ,
ExpectedSuccess : true ,
ExpectedOutput : "[STATUS] == pat(4*)" ,
} ,
{
2022-11-19 23:25:40 +01:00
Name : "pat-status-failure" ,
2021-03-11 03:49:13 +01:00
Condition : Condition ( "[STATUS] == pat(4*)" ) ,
Result : & Result { HTTPStatus : 200 } ,
ExpectedSuccess : false ,
ExpectedOutput : "[STATUS] (200) == pat(4*)" ,
} ,
2022-11-19 23:25:40 +01:00
// any
2021-03-11 03:49:13 +01:00
{
2022-11-19 23:25:40 +01:00
Name : "any-body-1" ,
2021-03-11 03:49:13 +01:00
Condition : Condition ( "[BODY].name == any(john.doe, jane.doe)" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "{\"name\": \"john.doe\"}" ) } ,
2021-03-11 03:49:13 +01:00
ExpectedSuccess : true ,
ExpectedOutput : "[BODY].name == any(john.doe, jane.doe)" ,
} ,
{
2022-11-19 23:25:40 +01:00
Name : "any-body-2" ,
2021-03-11 03:49:13 +01:00
Condition : Condition ( "[BODY].name == any(john.doe, jane.doe)" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "{\"name\": \"jane.doe\"}" ) } ,
2021-03-11 03:49:13 +01:00
ExpectedSuccess : true ,
ExpectedOutput : "[BODY].name == any(john.doe, jane.doe)" ,
} ,
{
2022-11-19 23:25:40 +01:00
Name : "any-body-failure" ,
2021-03-11 03:49:13 +01:00
Condition : Condition ( "[BODY].name == any(john.doe, jane.doe)" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "{\"name\": \"bob\"}" ) } ,
2021-03-11 03:49:13 +01:00
ExpectedSuccess : false ,
ExpectedOutput : "[BODY].name (bob) == any(john.doe, jane.doe)" ,
} ,
{
2022-11-19 23:25:40 +01:00
Name : "any-status-1" ,
2021-03-11 03:49:13 +01:00
Condition : Condition ( "[STATUS] == any(200, 429)" ) ,
Result : & Result { HTTPStatus : 200 } ,
ExpectedSuccess : true ,
ExpectedOutput : "[STATUS] == any(200, 429)" ,
} ,
{
2022-11-19 23:25:40 +01:00
Name : "any-status-2" ,
2021-03-11 03:49:13 +01:00
Condition : Condition ( "[STATUS] == any(200, 429)" ) ,
Result : & Result { HTTPStatus : 429 } ,
ExpectedSuccess : true ,
ExpectedOutput : "[STATUS] == any(200, 429)" ,
} ,
{
2022-11-19 23:25:40 +01:00
Name : "any-status-reverse" ,
2021-03-11 03:49:13 +01:00
Condition : Condition ( "any(200, 429) == [STATUS]" ) ,
Result : & Result { HTTPStatus : 429 } ,
ExpectedSuccess : true ,
ExpectedOutput : "any(200, 429) == [STATUS]" ,
} ,
{
2022-11-19 23:25:40 +01:00
Name : "any-status-failure" ,
2021-03-11 03:49:13 +01:00
Condition : Condition ( "[STATUS] == any(200, 429)" ) ,
Result : & Result { HTTPStatus : 404 } ,
ExpectedSuccess : false ,
ExpectedOutput : "[STATUS] (404) == any(200, 429)" ,
} ,
2021-09-15 01:34:46 +02:00
{
2022-11-19 23:25:40 +01:00
Name : "any-status-failure-but-dont-resolve" ,
2021-09-15 01:34:46 +02:00
Condition : Condition ( "[STATUS] == any(200, 429)" ) ,
Result : & Result { HTTPStatus : 404 } ,
DontResolveFailedConditions : true ,
ExpectedSuccess : false ,
ExpectedOutput : "[STATUS] == any(200, 429)" ,
} ,
2022-11-19 23:25:40 +01:00
// has
2021-03-11 03:49:13 +01:00
{
Name : "has" ,
Condition : Condition ( "has([BODY].errors) == false" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "{}" ) } ,
2021-03-11 03:49:13 +01:00
ExpectedSuccess : true ,
ExpectedOutput : "has([BODY].errors) == false" ,
} ,
2022-12-18 05:58:01 +01:00
{
Name : "has-key-of-map" ,
Condition : Condition ( "has([BODY].article) == true" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "{\n \"article\": {\n \"id\": 123,\n \"title\": \"Hello, world!\",\n \"author\": \"John Doe\",\n \"tags\": [\"hello\", \"world\"],\n \"content\": \"I really like Gatus!\"\n }\n}" ) } ,
2022-12-18 05:58:01 +01:00
DontResolveFailedConditions : false ,
ExpectedSuccess : true ,
ExpectedOutput : "has([BODY].article) == true" ,
} ,
2021-03-11 03:49:13 +01:00
{
Name : "has-failure" ,
Condition : Condition ( "has([BODY].errors) == false" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "{\"errors\": [\"1\"]}" ) } ,
2021-03-11 03:49:13 +01:00
ExpectedSuccess : false ,
ExpectedOutput : "has([BODY].errors) (true) == false" ,
} ,
2021-09-15 01:34:46 +02:00
{
Name : "has-failure-but-dont-resolve" ,
Condition : Condition ( "has([BODY].errors) == false" ) ,
2023-03-15 01:02:31 +01:00
Result : & Result { Body : [ ] byte ( "{\"errors\": [\"1\"]}" ) } ,
2021-09-15 01:34:46 +02:00
DontResolveFailedConditions : true ,
ExpectedSuccess : false ,
ExpectedOutput : "has([BODY].errors) == false" ,
} ,
2021-03-11 03:49:13 +01:00
}
for _ , scenario := range scenarios {
t . Run ( scenario . Name , func ( t * testing . T ) {
2021-09-15 01:34:46 +02:00
scenario . Condition . evaluate ( scenario . Result , scenario . DontResolveFailedConditions )
2021-03-11 03:49:13 +01:00
if scenario . Result . ConditionResults [ 0 ] . Success != scenario . ExpectedSuccess {
t . Errorf ( "Condition '%s' should have been success=%v" , scenario . Condition , scenario . ExpectedSuccess )
}
if scenario . Result . ConditionResults [ 0 ] . Condition != scenario . ExpectedOutput {
t . Errorf ( "Condition '%s' should have resolved to '%s', got '%s'" , scenario . Condition , scenario . ExpectedOutput , scenario . Result . ConditionResults [ 0 ] . Condition )
}
} )
2021-01-15 04:49:19 +01:00
}
2020-11-16 16:10:02 +01:00
}
2021-03-09 03:30:11 +01:00
func TestCondition_evaluateWithInvalidOperator ( t * testing . T ) {
condition := Condition ( "[STATUS] ? 201" )
result := & Result { HTTPStatus : 201 }
2021-09-15 01:34:46 +02:00
condition . evaluate ( result , false )
2021-03-09 03:30:11 +01:00
if result . Success {
t . Error ( "condition was invalid, result should've been a failure" )
}
if len ( result . Errors ) != 1 {
t . Error ( "condition was invalid, result should've had an error" )
}
}