2024-02-08 14:52:00 +00:00
|
|
|
package httputils
|
2020-06-28 11:26:22 +00:00
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
|
|
|
func TestTLSConfig(t *testing.T) {
|
|
|
|
var certFile, keyFile, CAFile, serverName string
|
|
|
|
var insecureSkipVerify bool
|
|
|
|
serverName = "test"
|
|
|
|
insecureSkipVerify = true
|
|
|
|
tlsCfg, err := TLSConfig(certFile, keyFile, CAFile, serverName, insecureSkipVerify)
|
|
|
|
if err != nil {
|
2024-07-09 20:32:54 +00:00
|
|
|
t.Fatalf("unexpected error %s", err)
|
2020-06-28 11:26:22 +00:00
|
|
|
}
|
|
|
|
if tlsCfg == nil {
|
2024-07-09 20:32:54 +00:00
|
|
|
t.Fatalf("expected tlsConfig to be set, got nil")
|
2020-06-28 11:26:22 +00:00
|
|
|
}
|
|
|
|
if tlsCfg.ServerName != serverName {
|
2024-07-09 20:32:54 +00:00
|
|
|
t.Fatalf("unexpected ServerName, want %s, got %s", serverName, tlsCfg.ServerName)
|
2020-06-28 11:26:22 +00:00
|
|
|
}
|
|
|
|
if tlsCfg.InsecureSkipVerify != insecureSkipVerify {
|
2024-07-09 20:32:54 +00:00
|
|
|
t.Fatalf("unexpected InsecureSkipVerify, want %v, got %v", insecureSkipVerify, tlsCfg.InsecureSkipVerify)
|
2020-06-28 11:26:22 +00:00
|
|
|
}
|
|
|
|
certFile = "/path/to/nonexisting/cert/file"
|
|
|
|
_, err = TLSConfig(certFile, keyFile, CAFile, serverName, insecureSkipVerify)
|
|
|
|
if err == nil {
|
2024-07-09 20:32:54 +00:00
|
|
|
t.Fatalf("expected keypair error, got nil")
|
2020-06-28 11:26:22 +00:00
|
|
|
}
|
|
|
|
certFile = ""
|
|
|
|
CAFile = "/path/to/nonexisting/cert/file"
|
|
|
|
_, err = TLSConfig(certFile, keyFile, CAFile, serverName, insecureSkipVerify)
|
|
|
|
if err == nil {
|
2024-07-09 20:32:54 +00:00
|
|
|
t.Fatalf("expected read error, got nil")
|
2020-06-28 11:26:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTransport(t *testing.T) {
|
|
|
|
var certFile, keyFile, CAFile, serverName string
|
|
|
|
var insecureSkipVerify bool
|
|
|
|
URL := "http://victoriametrics.com"
|
|
|
|
_, err := Transport(URL, certFile, keyFile, CAFile, serverName, insecureSkipVerify)
|
|
|
|
if err != nil {
|
2024-07-09 20:32:54 +00:00
|
|
|
t.Fatalf("unexpected error %s", err)
|
2020-06-28 11:26:22 +00:00
|
|
|
}
|
|
|
|
URL = "https://victoriametrics.com"
|
|
|
|
tr, err := Transport(URL, certFile, keyFile, CAFile, serverName, insecureSkipVerify)
|
|
|
|
if err != nil {
|
2024-07-09 20:32:54 +00:00
|
|
|
t.Fatalf("unexpected error %s", err)
|
2020-06-28 11:26:22 +00:00
|
|
|
}
|
|
|
|
if tr.TLSClientConfig == nil {
|
2024-07-09 20:32:54 +00:00
|
|
|
t.Fatalf("expected TLSClientConfig to be set, got nil")
|
2020-06-28 11:26:22 +00:00
|
|
|
}
|
|
|
|
}
|