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" "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 // Engine struct
type Engine struct { type Engine struct {
Localizer Localizer
@ -180,24 +189,31 @@ func (e *Engine) SetLogger(l LoggerInterface) *Engine {
// BuildHTTPClient builds HTTP client with provided configuration // BuildHTTPClient builds HTTP client with provided configuration
func (e *Engine) BuildHTTPClient(certs *x509.CertPool, replaceDefault ...bool) *Engine { func (e *Engine) BuildHTTPClient(certs *x509.CertPool, replaceDefault ...bool) *Engine {
if e.Config.GetHTTPClientConfig() != nil { client, err := NewHTTPClientBuilder().
client, err := NewHTTPClientBuilder(). WithLogger(e.Logger()).
WithLogger(e.Logger()). SetLogging(e.Config.IsDebug()).
SetLogging(e.Config.IsDebug()). SetCertPool(certs).
SetCertPool(certs). FromEngine(e).
FromEngine(e). Build(replaceDefault...)
Build(replaceDefault...)
if err != nil { if err != nil {
panic(err) panic(err)
} else { } else {
e.httpClient = client e.httpClient = client
}
} }
return e 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 // SetHTTPClient sets HTTP client to engine
func (e *Engine) SetHTTPClient(client *http.Client) *Engine { func (e *Engine) SetHTTPClient(client *http.Client) *Engine {
if client != nil { if client != nil {

View File

@ -10,6 +10,7 @@ import (
"net/url" "net/url"
"os" "os"
"testing" "testing"
"time"
"github.com/DATA-DOG/go-sqlmock" "github.com/DATA-DOG/go-sqlmock"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -177,6 +178,37 @@ func (e *EngineTest) Test_BuildHTTPClient() {
e.engine.BuildHTTPClient(x509.NewCertPool()) e.engine.BuildHTTPClient(x509.NewCertPool())
assert.NotNil(e.T(), e.engine.httpClient) 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() { 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 // FromEngine fulfills mock configuration from ConfigInterface inside Engine
func (b *HTTPClientBuilder) FromEngine(engine *Engine) *HTTPClientBuilder { func (b *HTTPClientBuilder) FromEngine(engine *Engine) *HTTPClientBuilder {
return b.FromConfig(engine.Config.GetHTTPClientConfig()) return b.FromConfig(engine.GetHTTPClientConfig())
} }
// buildDialer initializes dialer with provided timeout // buildDialer initializes dialer with provided timeout