2024-02-08 15:52:00 +01:00
|
|
|
package httputils
|
2020-06-28 13:26:22 +02: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 22:32:54 +02:00
|
|
|
t.Fatalf("unexpected error %s", err)
|
2020-06-28 13:26:22 +02:00
|
|
|
}
|
|
|
|
if tlsCfg == nil {
|
2024-07-09 22:32:54 +02:00
|
|
|
t.Fatalf("expected tlsConfig to be set, got nil")
|
2020-06-28 13:26:22 +02:00
|
|
|
}
|
|
|
|
if tlsCfg.ServerName != serverName {
|
2024-07-09 22:32:54 +02:00
|
|
|
t.Fatalf("unexpected ServerName, want %s, got %s", serverName, tlsCfg.ServerName)
|
2020-06-28 13:26:22 +02:00
|
|
|
}
|
|
|
|
if tlsCfg.InsecureSkipVerify != insecureSkipVerify {
|
2024-07-09 22:32:54 +02:00
|
|
|
t.Fatalf("unexpected InsecureSkipVerify, want %v, got %v", insecureSkipVerify, tlsCfg.InsecureSkipVerify)
|
2020-06-28 13:26:22 +02:00
|
|
|
}
|
|
|
|
certFile = "/path/to/nonexisting/cert/file"
|
|
|
|
_, err = TLSConfig(certFile, keyFile, CAFile, serverName, insecureSkipVerify)
|
|
|
|
if err == nil {
|
2024-07-09 22:32:54 +02:00
|
|
|
t.Fatalf("expected keypair error, got nil")
|
2020-06-28 13:26:22 +02:00
|
|
|
}
|
|
|
|
certFile = ""
|
|
|
|
CAFile = "/path/to/nonexisting/cert/file"
|
|
|
|
_, err = TLSConfig(certFile, keyFile, CAFile, serverName, insecureSkipVerify)
|
|
|
|
if err == nil {
|
2024-07-09 22:32:54 +02:00
|
|
|
t.Fatalf("expected read error, got nil")
|
2020-06-28 13:26:22 +02: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 22:32:54 +02:00
|
|
|
t.Fatalf("unexpected error %s", err)
|
2020-06-28 13:26:22 +02:00
|
|
|
}
|
|
|
|
URL = "https://victoriametrics.com"
|
|
|
|
tr, err := Transport(URL, certFile, keyFile, CAFile, serverName, insecureSkipVerify)
|
|
|
|
if err != nil {
|
2024-07-09 22:32:54 +02:00
|
|
|
t.Fatalf("unexpected error %s", err)
|
2020-06-28 13:26:22 +02:00
|
|
|
}
|
|
|
|
if tr.TLSClientConfig == nil {
|
2024-07-09 22:32:54 +02:00
|
|
|
t.Fatalf("expected TLSClientConfig to be set, got nil")
|
2020-06-28 13:26:22 +02:00
|
|
|
}
|
2024-08-16 11:32:04 +02:00
|
|
|
|
|
|
|
noSchemaURL := "127.0.0.1:8880"
|
|
|
|
_, err = Transport(noSchemaURL, certFile, keyFile, CAFile, serverName, insecureSkipVerify)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected to have parse error for URL without specified schema; got nil instead")
|
|
|
|
}
|
2020-06-28 13:26:22 +02:00
|
|
|
}
|