Skip to content

Commit

Permalink
build http client with default configuration if there's no client con…
Browse files Browse the repository at this point in the history
…figuration in the config (#20)
  • Loading branch information
Neur0toxine authored Nov 13, 2020
1 parent 6c029e9 commit 2f3b17a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 14 deletions.
42 changes: 29 additions & 13 deletions core/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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...)

if err != nil {
panic(err)
} else {
e.httpClient = client
}
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
}

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 {
Expand Down
32 changes: 32 additions & 0 deletions core/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/url"
"os"
"testing"
"time"

"github.com/DATA-DOG/go-sqlmock"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion core/http_client_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2f3b17a

Please sign in to comment.