2020-09-26 20:23:43 +02:00
|
|
|
package client
|
|
|
|
|
2020-10-05 01:49:02 +02:00
|
|
|
import (
|
|
|
|
"testing"
|
2021-01-13 03:26:28 +01:00
|
|
|
"time"
|
2020-10-05 01:49:02 +02:00
|
|
|
)
|
2020-09-26 20:23:43 +02:00
|
|
|
|
2020-12-28 23:19:41 +01:00
|
|
|
func TestGetHTTPClient(t *testing.T) {
|
2021-07-30 00:13:37 +02:00
|
|
|
cfg := &Config{
|
2021-07-29 03:41:26 +02:00
|
|
|
Insecure: false,
|
|
|
|
IgnoreRedirect: false,
|
|
|
|
Timeout: 0,
|
2021-07-30 00:13:37 +02:00
|
|
|
}
|
|
|
|
cfg.ValidateAndSetDefaults()
|
|
|
|
if GetHTTPClient(cfg) == nil {
|
|
|
|
t.Error("expected client to not be nil")
|
|
|
|
}
|
|
|
|
if GetHTTPClient(nil) == nil {
|
|
|
|
t.Error("expected client to not be nil")
|
|
|
|
}
|
2020-09-26 20:23:43 +02:00
|
|
|
}
|
2020-12-25 06:07:18 +01:00
|
|
|
|
2020-12-27 23:07:50 +01:00
|
|
|
func TestPing(t *testing.T) {
|
2021-07-29 03:41:26 +02:00
|
|
|
if success, rtt := Ping("127.0.0.1", &Config{Timeout: 500 * time.Millisecond}); !success {
|
2020-12-25 06:07:18 +01:00
|
|
|
t.Error("expected true")
|
2020-12-27 23:07:50 +01:00
|
|
|
if rtt == 0 {
|
|
|
|
t.Error("Round-trip time returned on success should've higher than 0")
|
|
|
|
}
|
2020-12-25 06:07:18 +01:00
|
|
|
}
|
2021-07-29 03:41:26 +02:00
|
|
|
if success, rtt := Ping("256.256.256.256", &Config{Timeout: 500 * time.Millisecond}); success {
|
2021-01-13 03:26:28 +01: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")
|
|
|
|
}
|
|
|
|
}
|
2021-07-29 03:41:26 +02:00
|
|
|
if success, rtt := Ping("192.168.152.153", &Config{Timeout: 500 * time.Millisecond}); success {
|
2021-01-13 03:26:28 +01:00
|
|
|
t.Error("expected false, because the IP is valid but the host should be unreachable")
|
2020-12-27 23:07:50 +01:00
|
|
|
if rtt != 0 {
|
|
|
|
t.Error("Round-trip time returned on failure should've been 0")
|
|
|
|
}
|
2020-12-25 06:07:18 +01:00
|
|
|
}
|
|
|
|
}
|
2021-06-05 21:47:11 +02:00
|
|
|
|
2021-06-05 22:35:52 +02: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 22:35:52 +02: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 22:35:52 +02:00
|
|
|
wantConnected: false,
|
|
|
|
wantErr: true,
|
2021-06-05 21:47:11 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "error dial",
|
|
|
|
args: args{
|
|
|
|
address: "test:1234",
|
|
|
|
},
|
2021-06-05 22:35:52 +02: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-07-29 03:41:26 +02:00
|
|
|
connected, _, err := CanPerformStartTLS(tt.args.address, &Config{Insecure: tt.args.insecure, Timeout: 5 * time.Second})
|
2021-06-05 21:47:11 +02:00
|
|
|
if (err != nil) != tt.wantErr {
|
2021-06-05 22:35:52 +02:00
|
|
|
t.Errorf("CanPerformStartTLS() err=%v, wantErr=%v", err, tt.wantErr)
|
2021-06-05 21:47:11 +02:00
|
|
|
return
|
|
|
|
}
|
2021-06-05 22:35:52 +02:00
|
|
|
if connected != tt.wantConnected {
|
|
|
|
t.Errorf("CanPerformStartTLS() connected=%v, wantConnected=%v", connected, tt.wantConnected)
|
2021-06-05 21:47:11 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-07-17 03:12:18 +02:00
|
|
|
|
2021-09-30 22:15:17 +02:00
|
|
|
func TestCanPerformTLS(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
address string
|
|
|
|
insecure bool
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantConnected bool
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "invalid address",
|
|
|
|
args: args{
|
|
|
|
address: "test",
|
|
|
|
},
|
|
|
|
wantConnected: false,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "error dial",
|
|
|
|
args: args{
|
|
|
|
address: "test:1234",
|
|
|
|
},
|
|
|
|
wantConnected: false,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid tls",
|
|
|
|
args: args{
|
|
|
|
address: "smtp.gmail.com:465",
|
|
|
|
},
|
|
|
|
wantConnected: true,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
connected, _, err := CanPerformTLS(tt.args.address, &Config{Insecure: tt.args.insecure, Timeout: 5 * time.Second})
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("CanPerformTLS() err=%v, wantErr=%v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if connected != tt.wantConnected {
|
|
|
|
t.Errorf("CanPerformTLS() connected=%v, wantConnected=%v", connected, tt.wantConnected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-17 03:12:18 +02:00
|
|
|
func TestCanCreateTCPConnection(t *testing.T) {
|
2021-07-29 03:41:26 +02:00
|
|
|
if CanCreateTCPConnection("127.0.0.1", &Config{Timeout: 5 * time.Second}) {
|
2021-07-17 03:12:18 +02:00
|
|
|
t.Error("should've failed, because there's no port in the address")
|
|
|
|
}
|
|
|
|
}
|