2020-04-15 01:20:00 +02:00
package core
import (
2021-12-03 07:44:17 +01:00
"io"
2020-12-19 00:40:11 +01:00
"strings"
2020-04-15 01:20:00 +02:00
"testing"
2020-10-22 03:56:07 +02:00
"time"
2021-05-19 04:29:15 +02:00
2021-10-08 03:28:04 +02:00
"github.com/TwiN/gatus/v3/alerting/alert"
"github.com/TwiN/gatus/v3/client"
2022-03-16 01:52:46 +01:00
"github.com/TwiN/gatus/v3/core/ui"
2020-04-15 01:20:00 +02:00
)
2021-10-23 22:47:12 +02:00
func TestEndpoint_IsEnabled ( t * testing . T ) {
if ! ( Endpoint { Enabled : nil } ) . IsEnabled ( ) {
t . Error ( "endpoint.IsEnabled() should've returned true, because Enabled was set to nil" )
2021-09-18 17:52:11 +02:00
}
2021-10-23 22:47:12 +02:00
if value := false ; ( Endpoint { Enabled : & value } ) . IsEnabled ( ) {
t . Error ( "endpoint.IsEnabled() should've returned false, because Enabled was set to false" )
2021-09-18 17:52:11 +02:00
}
2021-10-23 22:47:12 +02:00
if value := true ; ! ( Endpoint { Enabled : & value } ) . IsEnabled ( ) {
t . Error ( "Endpoint.IsEnabled() should've returned true, because Enabled was set to true" )
2021-09-18 17:52:11 +02:00
}
}
2022-05-17 03:10:45 +02:00
func TestEndpoint_Type ( t * testing . T ) {
type fields struct {
URL string
DNS * DNS
}
tests := [ ] struct {
fields fields
want EndpointType
} { {
fields : fields {
URL : "8.8.8.8" ,
DNS : & DNS {
QueryType : "A" ,
QueryName : "example.com" ,
} ,
} ,
want : EndpointTypeDNS ,
} , {
fields : fields {
URL : "tcp://127.0.0.1:6379" ,
} ,
want : EndpointTypeTCP ,
} , {
fields : fields {
URL : "icmp://example.com" ,
} ,
want : EndpointTypeICMP ,
} , {
fields : fields {
URL : "starttls://smtp.gmail.com:587" ,
} ,
want : EndpointTypeSTARTTLS ,
} , {
fields : fields {
URL : "tls://example.com:443" ,
} ,
want : EndpointTypeTLS ,
} , {
fields : fields {
URL : "https://twin.sh/health" ,
} ,
want : EndpointTypeHTTP ,
} }
for _ , tt := range tests {
t . Run ( string ( tt . want ) , func ( t * testing . T ) {
endpoint := Endpoint {
URL : tt . fields . URL ,
DNS : tt . fields . DNS ,
}
if got := endpoint . Type ( ) ; got != tt . want {
t . Errorf ( "Endpoint.Type() = %v, want %v" , got , tt . want )
}
} )
}
}
2021-10-23 22:47:12 +02:00
func TestEndpoint_ValidateAndSetDefaults ( t * testing . T ) {
endpoint := Endpoint {
2021-09-18 18:42:11 +02:00
Name : "website-health" ,
URL : "https://twin.sh/health" ,
2022-06-14 01:15:30 +02:00
Conditions : [ ] Condition { Condition ( "[STATUS] == 200" ) } ,
2021-05-19 04:29:15 +02:00
Alerts : [ ] * alert . Alert { { Type : alert . TypePagerDuty } } ,
2020-10-22 04:21:51 +02:00
}
2021-10-23 22:47:12 +02:00
endpoint . ValidateAndSetDefaults ( )
if endpoint . ClientConfig == nil {
2021-07-29 03:41:26 +02:00
t . Error ( "client configuration should've been set to the default configuration" )
} else {
2021-10-23 22:47:12 +02:00
if endpoint . ClientConfig . Insecure != client . GetDefaultConfig ( ) . Insecure {
t . Errorf ( "Default client configuration should've set Insecure to %v, got %v" , client . GetDefaultConfig ( ) . Insecure , endpoint . ClientConfig . Insecure )
2021-07-29 03:41:26 +02:00
}
2021-10-23 22:47:12 +02:00
if endpoint . ClientConfig . IgnoreRedirect != client . GetDefaultConfig ( ) . IgnoreRedirect {
t . Errorf ( "Default client configuration should've set IgnoreRedirect to %v, got %v" , client . GetDefaultConfig ( ) . IgnoreRedirect , endpoint . ClientConfig . IgnoreRedirect )
2021-07-29 03:41:26 +02:00
}
2021-10-23 22:47:12 +02:00
if endpoint . ClientConfig . Timeout != client . GetDefaultConfig ( ) . Timeout {
t . Errorf ( "Default client configuration should've set Timeout to %v, got %v" , client . GetDefaultConfig ( ) . Timeout , endpoint . ClientConfig . Timeout )
2021-07-29 03:41:26 +02:00
}
}
2021-10-23 22:47:12 +02:00
if endpoint . Method != "GET" {
t . Error ( "Endpoint method should've defaulted to GET" )
2020-10-22 04:21:51 +02:00
}
2021-10-23 22:47:12 +02:00
if endpoint . Interval != time . Minute {
t . Error ( "Endpoint interval should've defaulted to 1 minute" )
2020-10-22 04:21:51 +02:00
}
2021-10-23 22:47:12 +02:00
if endpoint . Headers == nil {
t . Error ( "Endpoint headers should've defaulted to an empty map" )
2020-10-22 04:21:51 +02:00
}
2021-10-23 22:47:12 +02:00
if len ( endpoint . Alerts ) != 1 {
t . Error ( "Endpoint should've had 1 alert" )
2020-10-22 04:21:51 +02:00
}
2021-10-23 22:47:12 +02:00
if endpoint . Alerts [ 0 ] . IsEnabled ( ) {
t . Error ( "Endpoint alert should've defaulted to disabled" )
2020-10-22 04:21:51 +02:00
}
2021-10-23 22:47:12 +02:00
if endpoint . Alerts [ 0 ] . SuccessThreshold != 2 {
t . Error ( "Endpoint alert should've defaulted to a success threshold of 2" )
2020-10-22 04:21:51 +02:00
}
2021-10-23 22:47:12 +02:00
if endpoint . Alerts [ 0 ] . FailureThreshold != 3 {
t . Error ( "Endpoint alert should've defaulted to a failure threshold of 3" )
2020-10-22 04:21:51 +02:00
}
}
2021-10-23 22:47:12 +02:00
func TestEndpoint_ValidateAndSetDefaultsWithClientConfig ( t * testing . T ) {
2021-07-29 03:41:26 +02:00
condition := Condition ( "[STATUS] == 200" )
2021-10-23 22:47:12 +02:00
endpoint := Endpoint {
2021-09-18 18:42:11 +02:00
Name : "website-health" ,
URL : "https://twin.sh/health" ,
2022-06-14 01:15:30 +02:00
Conditions : [ ] Condition { condition } ,
2021-07-29 03:41:26 +02:00
ClientConfig : & client . Config {
Insecure : true ,
IgnoreRedirect : true ,
Timeout : 0 ,
} ,
}
2021-10-23 22:47:12 +02:00
endpoint . ValidateAndSetDefaults ( )
if endpoint . ClientConfig == nil {
2021-07-29 03:41:26 +02:00
t . Error ( "client configuration should've been set to the default configuration" )
} else {
2021-10-23 22:47:12 +02:00
if ! endpoint . ClientConfig . Insecure {
t . Error ( "endpoint.ClientConfig.Insecure should've been set to true" )
2021-07-29 03:41:26 +02:00
}
2021-10-23 22:47:12 +02:00
if ! endpoint . ClientConfig . IgnoreRedirect {
t . Error ( "endpoint.ClientConfig.IgnoreRedirect should've been set to true" )
2021-07-29 03:41:26 +02:00
}
2021-10-23 22:47:12 +02:00
if endpoint . ClientConfig . Timeout != client . GetDefaultConfig ( ) . Timeout {
t . Error ( "endpoint.ClientConfig.Timeout should've been set to 10s, because the timeout value entered is not set or invalid" )
2021-07-29 03:41:26 +02:00
}
}
}
2021-10-23 22:47:12 +02:00
func TestEndpoint_ValidateAndSetDefaultsWithNoName ( t * testing . T ) {
2020-10-22 04:56:35 +02:00
defer func ( ) { recover ( ) } ( )
condition := Condition ( "[STATUS] == 200" )
2021-10-23 22:47:12 +02:00
endpoint := & Endpoint {
2020-10-22 04:56:35 +02:00
Name : "" ,
2020-10-23 22:29:20 +02:00
URL : "http://example.com" ,
2022-06-14 01:15:30 +02:00
Conditions : [ ] Condition { condition } ,
2020-10-22 04:56:35 +02:00
}
2021-10-23 22:47:12 +02:00
err := endpoint . ValidateAndSetDefaults ( )
2021-05-19 05:27:43 +02:00
if err == nil {
2021-10-23 22:47:12 +02:00
t . Fatal ( "Should've returned an error because endpoint didn't have a name, which is a mandatory field" )
2021-05-19 05:27:43 +02:00
}
2020-10-22 04:56:35 +02:00
}
2021-10-23 22:47:12 +02:00
func TestEndpoint_ValidateAndSetDefaultsWithNoUrl ( t * testing . T ) {
2020-10-22 04:56:35 +02:00
defer func ( ) { recover ( ) } ( )
condition := Condition ( "[STATUS] == 200" )
2021-10-23 22:47:12 +02:00
endpoint := & Endpoint {
2020-10-22 04:56:35 +02:00
Name : "example" ,
2020-10-23 22:29:20 +02:00
URL : "" ,
2022-06-14 01:15:30 +02:00
Conditions : [ ] Condition { condition } ,
2020-10-22 04:56:35 +02:00
}
2021-10-23 22:47:12 +02:00
err := endpoint . ValidateAndSetDefaults ( )
2021-05-19 05:27:43 +02:00
if err == nil {
2021-10-23 22:47:12 +02:00
t . Fatal ( "Should've returned an error because endpoint didn't have an url, which is a mandatory field" )
2021-05-19 05:27:43 +02:00
}
2020-10-22 04:56:35 +02:00
}
2021-10-23 22:47:12 +02:00
func TestEndpoint_ValidateAndSetDefaultsWithNoConditions ( t * testing . T ) {
2020-10-22 04:56:35 +02:00
defer func ( ) { recover ( ) } ( )
2021-10-23 22:47:12 +02:00
endpoint := & Endpoint {
2020-10-22 04:56:35 +02:00
Name : "example" ,
2020-10-23 22:29:20 +02:00
URL : "http://example.com" ,
2020-10-22 04:56:35 +02:00
Conditions : nil ,
}
2021-10-23 22:47:12 +02:00
err := endpoint . ValidateAndSetDefaults ( )
2021-05-19 05:27:43 +02:00
if err == nil {
2021-10-23 22:47:12 +02:00
t . Fatal ( "Should've returned an error because endpoint didn't have at least 1 condition" )
2021-05-19 05:27:43 +02:00
}
2020-10-22 04:56:35 +02:00
}
2021-10-23 22:47:12 +02:00
func TestEndpoint_ValidateAndSetDefaultsWithDNS ( t * testing . T ) {
endpoint := & Endpoint {
2020-11-18 01:00:16 +01:00
Name : "dns-test" ,
2020-11-18 00:55:31 +01:00
URL : "http://example.com" ,
DNS : & DNS {
QueryType : "A" ,
QueryName : "example.com" ,
} ,
2022-06-14 01:15:30 +02:00
Conditions : [ ] Condition { Condition ( "[DNS_RCODE] == NOERROR" ) } ,
2020-11-18 00:55:31 +01:00
}
2021-10-23 22:47:12 +02:00
err := endpoint . ValidateAndSetDefaults ( )
2021-05-19 05:27:43 +02:00
if err != nil {
}
2021-10-23 22:47:12 +02:00
if endpoint . DNS . QueryName != "example.com." {
t . Error ( "Endpoint.dns.query-name should be formatted with . suffix" )
2020-11-18 00:55:31 +01:00
}
}
2021-10-23 22:47:12 +02:00
func TestEndpoint_buildHTTPRequest ( t * testing . T ) {
2020-12-19 00:06:57 +01:00
condition := Condition ( "[STATUS] == 200" )
2021-10-23 22:47:12 +02:00
endpoint := Endpoint {
2021-09-18 18:42:11 +02:00
Name : "website-health" ,
URL : "https://twin.sh/health" ,
2022-06-14 01:15:30 +02:00
Conditions : [ ] Condition { condition } ,
2020-12-19 00:06:57 +01:00
}
2021-10-23 22:47:12 +02:00
endpoint . ValidateAndSetDefaults ( )
request := endpoint . buildHTTPRequest ( )
2020-12-19 00:40:11 +01:00
if request . Method != "GET" {
t . Error ( "request.Method should've been GET, but was" , request . Method )
}
2021-09-18 18:42:11 +02:00
if request . Host != "twin.sh" {
t . Error ( "request.Host should've been twin.sh, but was" , request . Host )
2020-12-19 00:06:57 +01:00
}
2020-12-31 01:56:12 +01:00
if userAgent := request . Header . Get ( "User-Agent" ) ; userAgent != GatusUserAgent {
t . Errorf ( "request.Header.Get(User-Agent) should've been %s, but was %s" , GatusUserAgent , userAgent )
}
}
2021-10-23 22:47:12 +02:00
func TestEndpoint_buildHTTPRequestWithCustomUserAgent ( t * testing . T ) {
2020-12-31 01:56:12 +01:00
condition := Condition ( "[STATUS] == 200" )
2021-10-23 22:47:12 +02:00
endpoint := Endpoint {
2021-09-18 18:42:11 +02:00
Name : "website-health" ,
URL : "https://twin.sh/health" ,
2022-06-14 01:15:30 +02:00
Conditions : [ ] Condition { condition } ,
2020-12-31 01:56:12 +01:00
Headers : map [ string ] string {
"User-Agent" : "Test/2.0" ,
} ,
}
2021-10-23 22:47:12 +02:00
endpoint . ValidateAndSetDefaults ( )
request := endpoint . buildHTTPRequest ( )
2020-12-31 01:56:12 +01:00
if request . Method != "GET" {
t . Error ( "request.Method should've been GET, but was" , request . Method )
}
2021-09-18 18:42:11 +02:00
if request . Host != "twin.sh" {
t . Error ( "request.Host should've been twin.sh, but was" , request . Host )
2020-12-31 01:56:12 +01:00
}
if userAgent := request . Header . Get ( "User-Agent" ) ; userAgent != "Test/2.0" {
t . Errorf ( "request.Header.Get(User-Agent) should've been %s, but was %s" , "Test/2.0" , userAgent )
}
2020-12-19 00:06:57 +01:00
}
2021-10-23 22:47:12 +02:00
func TestEndpoint_buildHTTPRequestWithHostHeader ( t * testing . T ) {
2020-12-19 00:06:57 +01:00
condition := Condition ( "[STATUS] == 200" )
2021-10-23 22:47:12 +02:00
endpoint := Endpoint {
2021-09-18 18:42:11 +02:00
Name : "website-health" ,
URL : "https://twin.sh/health" ,
2020-12-19 00:40:11 +01:00
Method : "POST" ,
2022-06-14 01:15:30 +02:00
Conditions : [ ] Condition { condition } ,
2020-12-19 00:06:57 +01:00
Headers : map [ string ] string {
"Host" : "example.com" ,
} ,
}
2021-10-23 22:47:12 +02:00
endpoint . ValidateAndSetDefaults ( )
request := endpoint . buildHTTPRequest ( )
2020-12-19 00:40:11 +01:00
if request . Method != "POST" {
t . Error ( "request.Method should've been POST, but was" , request . Method )
}
2020-12-19 00:06:57 +01:00
if request . Host != "example.com" {
2020-12-19 00:40:11 +01:00
t . Error ( "request.Host should've been example.com, but was" , request . Host )
}
}
2021-10-23 22:47:12 +02:00
func TestEndpoint_buildHTTPRequestWithGraphQLEnabled ( t * testing . T ) {
2020-12-19 00:40:11 +01:00
condition := Condition ( "[STATUS] == 200" )
2021-10-23 22:47:12 +02:00
endpoint := Endpoint {
2021-09-18 18:42:11 +02:00
Name : "website-graphql" ,
URL : "https://twin.sh/graphql" ,
2020-12-19 00:40:11 +01:00
Method : "POST" ,
2022-06-14 01:15:30 +02:00
Conditions : [ ] Condition { condition } ,
2020-12-19 00:40:11 +01:00
GraphQL : true ,
Body : ` {
2021-03-06 02:33:06 +01:00
users ( gender : "female" ) {
2020-12-19 00:40:11 +01:00
id
name
gender
avatar
}
} ` ,
}
2021-10-23 22:47:12 +02:00
endpoint . ValidateAndSetDefaults ( )
request := endpoint . buildHTTPRequest ( )
2020-12-19 00:40:11 +01:00
if request . Method != "POST" {
t . Error ( "request.Method should've been POST, but was" , request . Method )
}
if contentType := request . Header . Get ( ContentTypeHeader ) ; contentType != "application/json" {
t . Error ( "request.Header.Content-Type should've been application/json, but was" , contentType )
}
2021-12-03 07:44:17 +01:00
body , _ := io . ReadAll ( request . Body )
2020-12-19 00:40:11 +01:00
if ! strings . HasPrefix ( string ( body ) , "{\"query\":" ) {
2021-03-09 03:30:11 +01:00
t . Error ( "request.body should've started with '{\"query\":', but it didn't:" , string ( body ) )
2020-12-19 00:06:57 +01:00
}
}
2020-09-25 01:49:32 +02:00
func TestIntegrationEvaluateHealth ( t * testing . T ) {
2020-04-15 01:20:00 +02:00
condition := Condition ( "[STATUS] == 200" )
2021-03-09 03:30:11 +01:00
bodyCondition := Condition ( "[BODY].status == UP" )
2021-10-23 22:47:12 +02:00
endpoint := Endpoint {
2021-09-18 18:42:11 +02:00
Name : "website-health" ,
URL : "https://twin.sh/health" ,
2022-06-14 01:15:30 +02:00
Conditions : [ ] Condition { condition , bodyCondition } ,
2020-04-15 01:20:00 +02:00
}
2021-10-23 22:47:12 +02:00
endpoint . ValidateAndSetDefaults ( )
result := endpoint . EvaluateHealth ( )
2020-04-15 01:20:00 +02:00
if ! result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a success" , condition )
}
2020-10-05 01:55:19 +02:00
if ! result . Connected {
t . Error ( "Because the connection has been established, result.Connected should've been true" )
}
2020-04-15 01:20:00 +02:00
if ! result . Success {
t . Error ( "Because all conditions passed, this should have been a success" )
}
2022-03-16 01:52:46 +01:00
if result . Hostname != "twin.sh" {
t . Error ( "result.Hostname should've been twin.sh, but was" , result . Hostname )
}
2020-04-15 01:20:00 +02:00
}
2020-12-25 06:07:18 +01:00
func TestIntegrationEvaluateHealthWithFailure ( t * testing . T ) {
condition := Condition ( "[STATUS] == 500" )
2021-10-23 22:47:12 +02:00
endpoint := Endpoint {
2021-09-18 18:42:11 +02:00
Name : "website-health" ,
URL : "https://twin.sh/health" ,
2022-06-14 01:15:30 +02:00
Conditions : [ ] Condition { condition } ,
2020-12-25 06:07:18 +01:00
}
2021-10-23 22:47:12 +02:00
endpoint . ValidateAndSetDefaults ( )
result := endpoint . EvaluateHealth ( )
2020-12-25 06:07:18 +01:00
if result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Condition '%s' should have been a failure" , condition )
}
if ! result . Connected {
t . Error ( "Because the connection has been established, result.Connected should've been true" )
}
if result . Success {
2022-03-16 01:52:46 +01:00
t . Error ( "Because one of the conditions failed, result.Success should have been false" )
}
}
func TestIntegrationEvaluateHealthWithInvalidCondition ( t * testing . T ) {
condition := Condition ( "[STATUS] invalid 200" )
endpoint := Endpoint {
Name : "invalid-condition" ,
URL : "https://twin.sh/health" ,
2022-06-14 01:15:30 +02:00
Conditions : [ ] Condition { condition } ,
2022-03-16 01:52:46 +01:00
}
if err := endpoint . ValidateAndSetDefaults ( ) ; err != nil {
// XXX: Should this really not return an error? After all, the condition is not valid and conditions are part of the endpoint...
t . Error ( "endpoint validation should've been successful, but wasn't" )
}
result := endpoint . EvaluateHealth ( )
if result . Success {
t . Error ( "Because one of the conditions was invalid, result.Success should have been false" )
}
if len ( result . Errors ) == 0 {
t . Error ( "There should've been an error" )
}
}
func TestIntegrationEvaluateHealthWithError ( t * testing . T ) {
condition := Condition ( "[STATUS] == 200" )
endpoint := Endpoint {
Name : "invalid-host" ,
URL : "http://invalid/health" ,
2022-06-14 01:15:30 +02:00
Conditions : [ ] Condition { condition } ,
2022-03-16 01:52:46 +01:00
UIConfig : & ui . Config {
HideHostname : true ,
} ,
}
endpoint . ValidateAndSetDefaults ( )
result := endpoint . EvaluateHealth ( )
if result . Success {
t . Error ( "Because one of the conditions was invalid, result.Success should have been false" )
}
if len ( result . Errors ) == 0 {
t . Error ( "There should've been an error" )
}
if ! strings . Contains ( result . Errors [ 0 ] , "<redacted>" ) {
t . Error ( "result.Errors[0] should've had the hostname redacted because ui.hide-hostname is set to true" )
}
if result . Hostname != "" {
t . Error ( "result.Hostname should've been empty because ui.hide-hostname is set to true" )
2020-12-25 06:07:18 +01:00
}
}
2022-06-16 23:53:03 +02:00
func TestIntegrationEvaluateHealthWithErrorAndHideURL ( t * testing . T ) {
endpoint := Endpoint {
Name : "invalid-url" ,
URL : "https://httpstat.us/200?sleep=100" ,
Conditions : [ ] Condition { Condition ( "[STATUS] == 200" ) } ,
ClientConfig : & client . Config {
Timeout : 1 * time . Millisecond ,
} ,
UIConfig : & ui . Config {
HideURL : true ,
} ,
}
endpoint . ValidateAndSetDefaults ( )
result := endpoint . EvaluateHealth ( )
if result . Success {
t . Error ( "Because one of the conditions was invalid, result.Success should have been false" )
}
if len ( result . Errors ) == 0 {
t . Error ( "There should've been an error" )
}
if ! strings . Contains ( result . Errors [ 0 ] , "<redacted>" ) || strings . Contains ( result . Errors [ 0 ] , endpoint . URL ) {
t . Error ( "result.Errors[0] should've had the URL redacted because ui.hide-url is set to true" )
}
}
2020-11-18 00:55:31 +01:00
func TestIntegrationEvaluateHealthForDNS ( t * testing . T ) {
conditionSuccess := Condition ( "[DNS_RCODE] == NOERROR" )
conditionBody := Condition ( "[BODY] == 93.184.216.34" )
2021-10-23 22:47:12 +02:00
endpoint := Endpoint {
2021-03-09 03:30:11 +01:00
Name : "example" ,
2020-11-18 00:55:31 +01:00
URL : "8.8.8.8" ,
DNS : & DNS {
QueryType : "A" ,
QueryName : "example.com." ,
} ,
2022-06-14 01:15:30 +02:00
Conditions : [ ] Condition { conditionSuccess , conditionBody } ,
2020-11-18 00:55:31 +01:00
}
2021-10-23 22:47:12 +02:00
endpoint . ValidateAndSetDefaults ( )
result := endpoint . EvaluateHealth ( )
2020-11-18 00:55:31 +01:00
if ! result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Conditions '%s' and %s should have been a success" , conditionSuccess , conditionBody )
}
if ! result . Connected {
t . Error ( "Because the connection has been established, result.Connected should've been true" )
}
if ! result . Success {
t . Error ( "Because all conditions passed, this should have been a success" )
}
}
2020-12-25 06:07:18 +01:00
func TestIntegrationEvaluateHealthForICMP ( t * testing . T ) {
conditionSuccess := Condition ( "[CONNECTED] == true" )
2021-10-23 22:47:12 +02:00
endpoint := Endpoint {
2021-03-09 03:30:11 +01:00
Name : "icmp-test" ,
2020-12-25 09:05:47 +01:00
URL : "icmp://127.0.0.1" ,
2022-06-14 01:15:30 +02:00
Conditions : [ ] Condition { conditionSuccess } ,
2020-04-15 01:20:00 +02:00
}
2021-10-23 22:47:12 +02:00
endpoint . ValidateAndSetDefaults ( )
result := endpoint . EvaluateHealth ( )
2020-12-25 06:07:18 +01:00
if ! result . ConditionResults [ 0 ] . Success {
t . Errorf ( "Conditions '%s' should have been a success" , conditionSuccess )
2020-04-15 01:20:00 +02:00
}
2020-10-05 01:55:19 +02:00
if ! result . Connected {
t . Error ( "Because the connection has been established, result.Connected should've been true" )
}
2020-12-25 06:07:18 +01:00
if ! result . Success {
t . Error ( "Because all conditions passed, this should have been a success" )
2020-04-15 01:20:00 +02:00
}
}
2020-12-29 23:27:58 +01:00
2021-10-23 22:47:12 +02:00
func TestEndpoint_getIP ( t * testing . T ) {
2020-12-29 23:27:58 +01:00
conditionSuccess := Condition ( "[CONNECTED] == true" )
2021-10-23 22:47:12 +02:00
endpoint := Endpoint {
2021-03-09 03:30:11 +01:00
Name : "invalid-url-test" ,
2020-12-29 23:27:58 +01:00
URL : "" ,
2022-06-14 01:15:30 +02:00
Conditions : [ ] Condition { conditionSuccess } ,
2020-12-29 23:27:58 +01:00
}
result := & Result { }
2021-10-23 22:47:12 +02:00
endpoint . getIP ( result )
2020-12-29 23:27:58 +01:00
if len ( result . Errors ) == 0 {
2021-10-23 22:47:12 +02:00
t . Error ( "endpoint.getIP(result) should've thrown an error because the URL is invalid, thus cannot be parsed" )
2020-12-29 23:27:58 +01:00
}
}
2021-03-09 03:30:11 +01:00
2021-10-23 22:47:12 +02:00
func TestEndpoint_NeedsToReadBody ( t * testing . T ) {
2021-03-09 03:30:11 +01:00
statusCondition := Condition ( "[STATUS] == 200" )
bodyCondition := Condition ( "[BODY].status == UP" )
bodyConditionWithLength := Condition ( "len([BODY].tags) > 0" )
2022-06-14 01:15:30 +02:00
if ( & Endpoint { Conditions : [ ] Condition { statusCondition } } ) . needsToReadBody ( ) {
2021-03-09 03:30:11 +01:00
t . Error ( "expected false, got true" )
}
2022-06-14 01:15:30 +02:00
if ! ( & Endpoint { Conditions : [ ] Condition { bodyCondition } } ) . needsToReadBody ( ) {
2021-03-09 03:30:11 +01:00
t . Error ( "expected true, got false" )
}
2022-06-14 01:15:30 +02:00
if ! ( & Endpoint { Conditions : [ ] Condition { bodyConditionWithLength } } ) . needsToReadBody ( ) {
2021-03-09 03:30:11 +01:00
t . Error ( "expected true, got false" )
}
2022-06-14 01:15:30 +02:00
if ! ( & Endpoint { Conditions : [ ] Condition { statusCondition , bodyCondition } } ) . needsToReadBody ( ) {
2021-03-09 03:30:11 +01:00
t . Error ( "expected true, got false" )
}
2022-06-14 01:15:30 +02:00
if ! ( & Endpoint { Conditions : [ ] Condition { bodyCondition , statusCondition } } ) . needsToReadBody ( ) {
2021-03-09 03:30:11 +01:00
t . Error ( "expected true, got false" )
}
2022-06-14 01:15:30 +02:00
if ! ( & Endpoint { Conditions : [ ] Condition { bodyConditionWithLength , statusCondition } } ) . needsToReadBody ( ) {
2021-03-09 03:30:11 +01:00
t . Error ( "expected true, got false" )
}
}