2021-07-29 01:41:26 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2021-07-29 22:13:37 +00:00
|
|
|
func TestConfig_getHTTPClient(t *testing.T) {
|
2021-07-29 01:41:26 +00:00
|
|
|
insecureConfig := &Config{Insecure: true}
|
|
|
|
insecureConfig.ValidateAndSetDefaults()
|
2021-07-29 22:13:37 +00:00
|
|
|
insecureClient := insecureConfig.getHTTPClient()
|
2021-07-29 01:41:26 +00:00
|
|
|
if !(insecureClient.Transport).(*http.Transport).TLSClientConfig.InsecureSkipVerify {
|
|
|
|
t.Error("expected Config.Insecure set to true to cause the HTTP client to skip certificate verification")
|
|
|
|
}
|
|
|
|
if insecureClient.Timeout != defaultHTTPTimeout {
|
|
|
|
t.Error("expected Config.Timeout to default the HTTP client to a timeout of 10s")
|
|
|
|
}
|
|
|
|
request, _ := http.NewRequest("GET", "", nil)
|
|
|
|
if err := insecureClient.CheckRedirect(request, nil); err != nil {
|
|
|
|
t.Error("expected Config.IgnoreRedirect set to false to cause the HTTP client's CheckRedirect to return nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
secureConfig := &Config{IgnoreRedirect: true, Timeout: 5 * time.Second}
|
|
|
|
secureConfig.ValidateAndSetDefaults()
|
2021-07-29 22:13:37 +00:00
|
|
|
secureClient := secureConfig.getHTTPClient()
|
2021-07-29 01:41:26 +00:00
|
|
|
if (secureClient.Transport).(*http.Transport).TLSClientConfig.InsecureSkipVerify {
|
|
|
|
t.Error("expected Config.Insecure set to false to cause the HTTP client to not skip certificate verification")
|
|
|
|
}
|
|
|
|
if secureClient.Timeout != 5*time.Second {
|
|
|
|
t.Error("expected Config.Timeout to cause the HTTP client to have a timeout of 5s")
|
|
|
|
}
|
|
|
|
request, _ = http.NewRequest("GET", "", nil)
|
|
|
|
if err := secureClient.CheckRedirect(request, nil); err != http.ErrUseLastResponse {
|
|
|
|
t.Error("expected Config.IgnoreRedirect set to true to cause the HTTP client's CheckRedirect to return http.ErrUseLastResponse")
|
|
|
|
}
|
|
|
|
}
|