mirror of
https://github.com/retailcrm/mg-transport-core.git
synced 2024-11-21 20:56:04 +03:00
build http client with default configuration if there's no client configuration in the config (#20)
This commit is contained in:
parent
6c029e905f
commit
2f3b17ad07
@ -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 {
|
||||
|
@ -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() {
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user