diff --git a/core/engine.go b/core/engine.go index 56701e5..290609b 100644 --- a/core/engine.go +++ b/core/engine.go @@ -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,24 +189,31 @@ 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()). - SetCertPool(certs). - FromEngine(e). - Build(replaceDefault...) + client, err := NewHTTPClientBuilder(). + WithLogger(e.Logger()). + SetLogging(e.Config.IsDebug()). + SetCertPool(certs). + FromEngine(e). + Build(replaceDefault...) - if err != nil { - panic(err) - } else { - e.httpClient = client - } + if err != nil { + panic(err) + } 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 { diff --git a/core/engine_test.go b/core/engine_test.go index 3e2a554..0885b7d 100644 --- a/core/engine_test.go +++ b/core/engine_test.go @@ -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() { diff --git a/core/http_client_builder.go b/core/http_client_builder.go index cf8b40c..ddaefe9 100644 --- a/core/http_client_builder.go +++ b/core/http_client_builder.go @@ -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