build http client with default configuration if there's no client configuration in the config (#20)

This commit is contained in:
Pavel 2020-11-13 12:04:32 +03:00 committed by GitHub
parent 6c029e905f
commit 2f3b17ad07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 13 deletions

View File

@ -14,6 +14,15 @@ import (
"golang.org/x/text/language"
)
var boolTrue = true
// DefaultHTTPClientConfig is a default config for HTTP client. It will be used by Engine for building HTTP client
// if HTTP client config is not present in the configuration.
var DefaultHTTPClientConfig = &HTTPClientConfig{
Timeout: 30,
SSLVerification: &boolTrue,
}
// Engine struct
type Engine struct {
Localizer
@ -180,7 +189,6 @@ func (e *Engine) SetLogger(l LoggerInterface) *Engine {
// BuildHTTPClient builds HTTP client with provided configuration
func (e *Engine) BuildHTTPClient(certs *x509.CertPool, replaceDefault ...bool) *Engine {
if e.Config.GetHTTPClientConfig() != nil {
client, err := NewHTTPClientBuilder().
WithLogger(e.Logger()).
SetLogging(e.Config.IsDebug()).
@ -193,11 +201,19 @@ func (e *Engine) BuildHTTPClient(certs *x509.CertPool, replaceDefault ...bool) *
} else {
e.httpClient = client
}
}
return e
}
// GetHTTPClientConfig returns configuration for HTTP client
func (e *Engine) GetHTTPClientConfig() *HTTPClientConfig {
if e.Config.GetHTTPClientConfig() != nil {
return e.Config.GetHTTPClientConfig()
}
return DefaultHTTPClientConfig
}
// SetHTTPClient sets HTTP client to engine
func (e *Engine) SetHTTPClient(client *http.Client) *Engine {
if client != nil {

View File

@ -10,6 +10,7 @@ import (
"net/url"
"os"
"testing"
"time"
"github.com/DATA-DOG/go-sqlmock"
"github.com/gin-gonic/gin"
@ -177,6 +178,37 @@ func (e *EngineTest) Test_BuildHTTPClient() {
e.engine.BuildHTTPClient(x509.NewCertPool())
assert.NotNil(e.T(), e.engine.httpClient)
assert.NotNil(e.T(), e.engine.httpClient.Transport)
transport := e.engine.httpClient.Transport.(*http.Transport)
assert.NotNil(e.T(), transport.TLSClientConfig)
assert.NotNil(e.T(), transport.TLSClientConfig.RootCAs)
}
func (e *EngineTest) Test_BuildHTTPClient_NoConfig() {
e.engine.Config = &Config{}
e.engine.BuildHTTPClient(x509.NewCertPool())
assert.NotNil(e.T(), e.engine.httpClient)
assert.NotNil(e.T(), e.engine.httpClient.Transport)
transport := e.engine.httpClient.Transport.(*http.Transport)
assert.NotNil(e.T(), transport.TLSClientConfig)
assert.NotNil(e.T(), transport.TLSClientConfig.RootCAs)
}
func (e *EngineTest) Test_GetHTTPClientConfig() {
e.engine.Config = &Config{}
assert.Equal(e.T(), DefaultHTTPClientConfig, e.engine.GetHTTPClientConfig())
e.engine.Config = &Config{
HTTPClientConfig: &HTTPClientConfig{
Timeout: 10,
SSLVerification: boolPtr(true),
},
}
assert.NotEqual(e.T(), DefaultHTTPClientConfig, e.engine.GetHTTPClientConfig())
assert.Equal(e.T(), time.Duration(10), e.engine.GetHTTPClientConfig().Timeout)
}
func (e *EngineTest) Test_WithCookieSessions() {

View File

@ -155,7 +155,7 @@ func (b *HTTPClientBuilder) FromConfig(config *HTTPClientConfig) *HTTPClientBuil
// FromEngine fulfills mock configuration from ConfigInterface inside Engine
func (b *HTTPClientBuilder) FromEngine(engine *Engine) *HTTPClientBuilder {
return b.FromConfig(engine.Config.GetHTTPClientConfig())
return b.FromConfig(engine.GetHTTPClientConfig())
}
// buildDialer initializes dialer with provided timeout