Files
gatus/client/client_test.go
T

101 lines
2.5 KiB
Go
Raw Normal View History

package client
2020-10-04 19:49:02 -04:00
import (
"testing"
2021-01-12 21:26:28 -05:00
"time"
2020-10-04 19:49:02 -04:00
)
func TestGetHTTPClient(t *testing.T) {
2020-10-23 16:29:20 -04:00
if secureHTTPClient != nil {
t.Error("secureHTTPClient should've been nil since it hasn't been called a single time yet")
}
2020-10-23 16:29:20 -04:00
if insecureHTTPClient != nil {
t.Error("insecureHTTPClient should've been nil since it hasn't been called a single time yet")
}
2020-10-23 16:29:20 -04:00
_ = GetHTTPClient(false)
if secureHTTPClient == nil {
t.Error("secureHTTPClient shouldn't have been nil, since it has been called once")
}
2020-10-23 16:29:20 -04:00
if insecureHTTPClient != nil {
t.Error("insecureHTTPClient should've been nil since it hasn't been called a single time yet")
}
2020-10-23 16:29:20 -04:00
_ = GetHTTPClient(true)
if secureHTTPClient == nil {
t.Error("secureHTTPClient shouldn't have been nil, since it has been called once")
}
2020-10-23 16:29:20 -04:00
if insecureHTTPClient == nil {
t.Error("insecureHTTPClient shouldn't have been nil, since it has been called once")
}
}
2020-12-25 00:07:18 -05:00
func TestPing(t *testing.T) {
2021-01-12 22:19:19 -05:00
pingTimeout = 500 * time.Millisecond
if success, rtt := Ping("127.0.0.1"); !success {
2020-12-25 00:07:18 -05:00
t.Error("expected true")
if rtt == 0 {
t.Error("Round-trip time returned on success should've higher than 0")
}
2020-12-25 00:07:18 -05:00
}
if success, rtt := Ping("256.256.256.256"); success {
2021-01-12 21:26:28 -05:00
t.Error("expected false, because the IP is invalid")
if rtt != 0 {
t.Error("Round-trip time returned on failure should've been 0")
}
}
if success, rtt := Ping("192.168.152.153"); success {
t.Error("expected false, because the IP is valid but the host should be unreachable")
if rtt != 0 {
t.Error("Round-trip time returned on failure should've been 0")
}
2020-12-25 00:07:18 -05:00
}
}
2021-06-05 21:47:11 +02:00
2021-06-05 16:35:52 -04:00
func TestCanPerformStartTLS(t *testing.T) {
2021-06-05 21:47:11 +02:00
type args struct {
address string
insecure bool
}
tests := []struct {
2021-06-05 16:35:52 -04:00
name string
args args
wantConnected bool
wantErr bool
2021-06-05 21:47:11 +02:00
}{
{
name: "invalid address",
args: args{
address: "test",
},
2021-06-05 16:35:52 -04:00
wantConnected: false,
wantErr: true,
2021-06-05 21:47:11 +02:00
},
{
name: "error dial",
args: args{
address: "test:1234",
},
2021-06-05 16:35:52 -04:00
wantConnected: false,
wantErr: true,
2021-06-05 21:47:11 +02:00
},
{
name: "valid starttls",
args: args{
address: "smtp.gmail.com:587",
},
wantConnected: true,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2021-06-05 16:35:52 -04:00
connected, _, err := CanPerformStartTLS(tt.args.address, tt.args.insecure)
2021-06-05 21:47:11 +02:00
if (err != nil) != tt.wantErr {
2021-06-05 16:35:52 -04:00
t.Errorf("CanPerformStartTLS() err=%v, wantErr=%v", err, tt.wantErr)
2021-06-05 21:47:11 +02:00
return
}
2021-06-05 16:35:52 -04:00
if connected != tt.wantConnected {
t.Errorf("CanPerformStartTLS() connected=%v, wantConnected=%v", connected, tt.wantConnected)
2021-06-05 21:47:11 +02:00
}
})
}
}