Fix #22: Improve alerting provider tests by mocking HTTP client

This commit is contained in:
TwiN
2021-12-02 23:10:21 -05:00
parent 6954e9dde7
commit 8c73ae6035
10 changed files with 661 additions and 0 deletions

View File

@ -14,8 +14,14 @@ import (
"github.com/go-ping/ping"
)
// injectedHTTPClient is used for testing purposes
var injectedHTTPClient *http.Client
// GetHTTPClient returns the shared HTTP client
func GetHTTPClient(config *Config) *http.Client {
if injectedHTTPClient != nil {
return injectedHTTPClient
}
if config == nil {
return defaultConfig.getHTTPClient()
}
@ -104,3 +110,8 @@ func Ping(address string, config *Config) (bool, time.Duration) {
}
return true, 0
}
// InjectHTTPClient is used to inject a custom HTTP client for testing purposes
func InjectHTTPClient(httpClient *http.Client) {
injectedHTTPClient = httpClient
}