2019-09-07 02:34:10 +02:00
package core
import (
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
)
2020-10-23 22:29:20 +02:00
func TestCondition_evaluateWithIP ( t * testing . T ) {
2019-12-04 23:27:27 +01:00
condition := Condition ( "[IP] == 127.0.0.1" )
2020-10-23 22:29:20 +02:00
result := & Result { IP : "127.0.0.1" }
2020-04-07 00:58:13 +02:00
condition . evaluate ( result )
2019-09-12 22:15:42 +02:00
if ! result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a success" , condition )
2019-09-07 02:34:10 +02:00
}
}
2020-04-15 01:20:00 +02:00
func TestCondition_evaluateWithStatus ( t * testing . T ) {
2019-12-04 23:27:27 +01:00
condition := Condition ( "[STATUS] == 201" )
2020-10-23 22:29:20 +02:00
result := & Result { HTTPStatus : 201 }
2020-04-07 00:58:13 +02:00
condition . evaluate ( result )
2019-09-12 22:15:42 +02:00
if ! result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a success" , condition )
}
}
2020-04-15 01:20:00 +02:00
func TestCondition_evaluateWithStatusFailure ( t * testing . T ) {
2019-12-04 23:27:27 +01:00
condition := Condition ( "[STATUS] == 200" )
2020-10-23 22:29:20 +02:00
result := & Result { HTTPStatus : 500 }
2020-04-07 00:58:13 +02:00
condition . evaluate ( result )
2019-09-12 22:15:42 +02:00
if result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a failure" , condition )
}
}
2020-04-15 01:20:00 +02:00
func TestCondition_evaluateWithStatusUsingLessThan ( t * testing . T ) {
2020-04-10 22:34:20 +02:00
condition := Condition ( "[STATUS] < 300" )
2020-10-23 22:29:20 +02:00
result := & Result { HTTPStatus : 201 }
2020-04-10 22:34:20 +02:00
condition . evaluate ( result )
if ! result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a success" , condition )
}
}
2020-04-15 01:20:00 +02:00
func TestCondition_evaluateWithStatusFailureUsingLessThan ( t * testing . T ) {
2020-04-10 22:34:20 +02:00
condition := Condition ( "[STATUS] < 300" )
2020-10-23 22:29:20 +02:00
result := & Result { HTTPStatus : 404 }
2020-04-10 22:34:20 +02:00
condition . evaluate ( result )
if result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a failure" , condition )
}
}
2020-04-15 01:20:00 +02:00
func TestCondition_evaluateWithResponseTimeUsingLessThan ( t * testing . T ) {
2020-04-10 22:34:20 +02:00
condition := Condition ( "[RESPONSE_TIME] < 500" )
result := & Result { Duration : time . Millisecond * 50 }
condition . evaluate ( result )
if ! result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a success" , condition )
}
}
2020-11-16 16:27:46 +01:00
func TestCondition_evaluateWithResponseTimeUsingLessThanDuration ( t * testing . T ) {
condition := Condition ( "[RESPONSE_TIME] < 1s" )
result := & Result { Duration : time . Millisecond * 50 }
condition . evaluate ( result )
if ! result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a success" , condition )
}
}
func TestCondition_evaluateWithResponseTimeUsingLessThanInvalid ( t * testing . T ) {
condition := Condition ( "[RESPONSE_TIME] < potato" )
result := & Result { Duration : time . Millisecond * 50 }
condition . evaluate ( result )
if result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have failed because the condition has an invalid numerical value that should've automatically resolved to 0" , condition )
}
}
2020-04-15 01:20:00 +02:00
func TestCondition_evaluateWithResponseTimeUsingGreaterThan ( t * testing . T ) {
2020-11-16 16:27:46 +01:00
// Not exactly sure why you'd want to have a condition that checks if the response time is too fast,
// but hey, who am I to judge?
2020-04-10 22:34:20 +02:00
condition := Condition ( "[RESPONSE_TIME] > 500" )
result := & Result { Duration : time . Millisecond * 750 }
condition . evaluate ( result )
if ! result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a success" , condition )
}
}
2020-11-16 16:27:46 +01:00
func TestCondition_evaluateWithResponseTimeUsingGreaterThanDuration ( t * testing . T ) {
condition := Condition ( "[RESPONSE_TIME] > 1s" )
result := & Result { Duration : time . Second * 2 }
condition . evaluate ( result )
if ! result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a success" , condition )
}
}
2020-04-15 01:20:00 +02:00
func TestCondition_evaluateWithResponseTimeUsingGreaterThanOrEqualTo ( t * testing . T ) {
2020-04-10 22:34:20 +02:00
condition := Condition ( "[RESPONSE_TIME] >= 500" )
result := & Result { Duration : time . Millisecond * 500 }
condition . evaluate ( result )
if ! result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a success" , condition )
}
}
2020-04-15 01:20:00 +02:00
func TestCondition_evaluateWithResponseTimeUsingLessThanOrEqualTo ( t * testing . T ) {
2020-04-10 22:34:20 +02:00
condition := Condition ( "[RESPONSE_TIME] <= 500" )
result := & Result { Duration : time . Millisecond * 500 }
condition . evaluate ( result )
if ! result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a success" , condition )
}
}
2020-04-15 01:20:00 +02:00
func TestCondition_evaluateWithBody ( t * testing . T ) {
2020-04-11 04:56:38 +02:00
condition := Condition ( "[BODY] == test" )
result := & Result { Body : [ ] byte ( "test" ) }
condition . evaluate ( result )
if ! result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a success" , condition )
}
}
2020-10-23 22:29:20 +02:00
func TestCondition_evaluateWithBodyJSONPath ( t * testing . T ) {
2020-04-11 04:56:38 +02:00
condition := Condition ( "[BODY].status == UP" )
result := & Result { Body : [ ] byte ( "{\"status\":\"UP\"}" ) }
condition . evaluate ( result )
if ! result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a success" , condition )
}
}
2020-10-23 22:29:20 +02:00
func TestCondition_evaluateWithBodyJSONPathComplex ( t * testing . T ) {
2020-04-11 04:56:38 +02:00
condition := Condition ( "[BODY].data.name == john" )
result := & Result { Body : [ ] byte ( "{\"data\": {\"id\": 1, \"name\": \"john\"}}" ) }
condition . evaluate ( result )
if ! result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a success" , condition )
}
}
2020-10-23 22:29:20 +02:00
func TestCondition_evaluateWithInvalidBodyJSONPathComplex ( t * testing . T ) {
2020-10-16 04:44:34 +02:00
expectedResolvedCondition := "[BODY].data.name (INVALID) == john"
condition := Condition ( "[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 )
}
}
2020-10-29 22:18:43 +01:00
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 )
}
}
2020-10-23 22:29:20 +02:00
func TestCondition_evaluateWithBodyJSONPathDoublePlaceholders ( t * testing . T ) {
2020-09-30 01:32:59 +02:00
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 )
}
}
2020-10-23 22:29:20 +02:00
func TestCondition_evaluateWithBodyJSONPathDoublePlaceholdersFailure ( t * testing . T ) {
2020-09-30 01:32:59 +02:00
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 )
}
}
2020-10-23 22:29:20 +02:00
func TestCondition_evaluateWithBodyJSONPathLongInt ( t * testing . T ) {
2020-04-11 04:56:38 +02:00
condition := Condition ( "[BODY].data.id == 1" )
result := & Result { Body : [ ] byte ( "{\"data\": {\"id\": 1}}" ) }
condition . evaluate ( result )
if ! result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a success" , condition )
}
}
2020-10-23 22:29:20 +02:00
func TestCondition_evaluateWithBodyJSONPathComplexInt ( t * testing . T ) {
2020-04-15 01:20:00 +02:00
condition := Condition ( "[BODY].data[1].id == 2" )
result := & Result { Body : [ ] byte ( "{\"data\": [{\"id\": 1}, {\"id\": 2}, {\"id\": 3}]}" ) }
condition . evaluate ( result )
if ! result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a success" , condition )
}
}
2020-10-23 22:29:20 +02:00
func TestCondition_evaluateWithBodyJSONPathComplexIntUsingGreaterThan ( t * testing . T ) {
2020-04-11 04:56:38 +02:00
condition := Condition ( "[BODY].data.id > 0" )
result := & Result { Body : [ ] byte ( "{\"data\": {\"id\": 1}}" ) }
condition . evaluate ( result )
if ! result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a success" , condition )
}
}
2020-10-23 22:29:20 +02:00
func TestCondition_evaluateWithBodyJSONPathComplexIntFailureUsingGreaterThan ( t * testing . T ) {
2020-04-11 04:56:38 +02:00
condition := Condition ( "[BODY].data.id > 5" )
result := & Result { Body : [ ] byte ( "{\"data\": {\"id\": 1}}" ) }
condition . evaluate ( result )
if result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a failure" , condition )
}
}
2020-10-23 22:29:20 +02:00
func TestCondition_evaluateWithBodyJSONPathComplexIntUsingLessThan ( t * testing . T ) {
2020-04-11 04:56:38 +02:00
condition := Condition ( "[BODY].data.id < 5" )
result := & Result { Body : [ ] byte ( "{\"data\": {\"id\": 2}}" ) }
condition . evaluate ( result )
if ! result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a success" , condition )
}
}
2020-10-23 22:29:20 +02:00
func TestCondition_evaluateWithBodyJSONPathComplexIntFailureUsingLessThan ( t * testing . T ) {
2020-04-11 04:56:38 +02:00
condition := Condition ( "[BODY].data.id < 5" )
result := & Result { Body : [ ] byte ( "{\"data\": {\"id\": 10}}" ) }
condition . evaluate ( result )
if result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a failure" , condition )
}
}
2020-08-13 03:42:13 +02:00
func TestCondition_evaluateWithBodySliceLength ( t * testing . T ) {
condition := Condition ( "len([BODY].data) == 3" )
result := & Result { Body : [ ] byte ( "{\"data\": [{\"id\": 1}, {\"id\": 2}, {\"id\": 3}]}" ) }
condition . evaluate ( result )
if ! result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a success" , condition )
}
}
func TestCondition_evaluateWithBodyStringLength ( t * testing . T ) {
condition := Condition ( "len([BODY].name) == 8" )
result := & Result { Body : [ ] byte ( "{\"name\": \"john.doe\"}" ) }
condition . evaluate ( result )
if ! result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a success" , condition )
}
}
2020-10-02 01:57:11 +02:00
func TestCondition_evaluateWithBodyStringPattern ( t * testing . T ) {
condition := Condition ( "[BODY].name == pat(*ohn*)" )
result := & Result { Body : [ ] byte ( "{\"name\": \"john.doe\"}" ) }
condition . evaluate ( result )
if ! result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a success" , condition )
}
}
func TestCondition_evaluateWithBodyStringPatternFailure ( t * testing . T ) {
condition := Condition ( "[BODY].name == pat(bob*)" )
result := & Result { Body : [ ] byte ( "{\"name\": \"john.doe\"}" ) }
condition . evaluate ( result )
if result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a failure" , condition )
}
}
func TestCondition_evaluateWithBodyPatternFailure ( t * testing . T ) {
condition := Condition ( "[BODY] == pat(*john*)" )
result := & Result { Body : [ ] byte ( "{\"name\": \"john.doe\"}" ) }
condition . evaluate ( result )
if ! result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a success" , condition )
}
}
func TestCondition_evaluateWithIPPattern ( t * testing . T ) {
condition := Condition ( "[IP] == pat(10.*)" )
2020-10-23 22:29:20 +02:00
result := & Result { IP : "10.0.0.0" }
2020-10-02 01:57:11 +02:00
condition . evaluate ( result )
if ! result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a success" , condition )
}
}
func TestCondition_evaluateWithIPPatternFailure ( t * testing . T ) {
condition := Condition ( "[IP] == pat(10.*)" )
2020-10-23 22:29:20 +02:00
result := & Result { IP : "255.255.255.255" }
2020-10-02 01:57:11 +02:00
condition . evaluate ( result )
if result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a failure" , condition )
}
}
func TestCondition_evaluateWithStatusPattern ( t * testing . T ) {
condition := Condition ( "[STATUS] == pat(4*)" )
2020-10-23 22:29:20 +02:00
result := & Result { HTTPStatus : 404 }
2020-10-02 01:57:11 +02:00
condition . evaluate ( result )
if ! result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a success" , condition )
}
}
func TestCondition_evaluateWithStatusPatternFailure ( t * testing . T ) {
condition := Condition ( "[STATUS] != pat(4*)" )
2020-10-23 22:29:20 +02:00
result := & Result { HTTPStatus : 404 }
2020-10-02 01:57:11 +02:00
condition . evaluate ( result )
if result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a failure" , condition )
}
}
2020-10-05 01:49:02 +02:00
func TestCondition_evaluateWithConnected ( t * testing . T ) {
condition := Condition ( "[CONNECTED] == true" )
result := & Result { Connected : true }
condition . evaluate ( result )
if ! result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a success" , condition )
}
}
func TestCondition_evaluateWithConnectedFailure ( t * testing . T ) {
condition := Condition ( "[CONNECTED] == true" )
result := & Result { Connected : false }
condition . evaluate ( result )
if result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a failure" , condition )
}
}
2020-11-15 18:47:28 +01:00
func TestCondition_evaluateWithUnsetCertificateExpiration ( t * testing . T ) {
condition := Condition ( "[CERTIFICATE_EXPIRATION] == 0" )
result := & Result { }
condition . evaluate ( result )
if ! result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a success" , condition )
}
}
2020-11-16 16:10:02 +01:00
func TestCondition_evaluateWithCertificateExpirationGreaterThanNumerical ( t * testing . T ) {
2020-11-15 18:47:28 +01:00
acceptable := ( time . Hour * 24 * 28 ) . Milliseconds ( )
condition := Condition ( "[CERTIFICATE_EXPIRATION] > " + strconv . FormatInt ( acceptable , 10 ) )
result := & Result { CertificateExpiration : time . Hour * 24 * 60 }
condition . evaluate ( result )
if ! result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a success" , condition )
}
}
2020-11-16 16:10:02 +01:00
func TestCondition_evaluateWithCertificateExpirationGreaterThanNumericalFailure ( t * testing . T ) {
2020-11-15 18:47:28 +01:00
acceptable := ( time . Hour * 24 * 28 ) . Milliseconds ( )
condition := Condition ( "[CERTIFICATE_EXPIRATION] > " + strconv . FormatInt ( acceptable , 10 ) )
result := & Result { CertificateExpiration : time . Hour * 24 * 14 }
condition . evaluate ( result )
if result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a failure" , condition )
}
}
2020-11-16 16:10:02 +01:00
func TestCondition_evaluateWithCertificateExpirationGreaterThanDuration ( t * testing . T ) {
condition := Condition ( "[CERTIFICATE_EXPIRATION] > 12h" )
result := & Result { CertificateExpiration : 24 * time . Hour }
condition . evaluate ( result )
if ! result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a success" , condition )
}
}
func TestCondition_evaluateWithCertificateExpirationGreaterThanDurationFailure ( t * testing . T ) {
condition := Condition ( "[CERTIFICATE_EXPIRATION] > 48h" )
result := & Result { CertificateExpiration : 24 * time . Hour }
condition . evaluate ( result )
if result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a failure" , condition )
}
}