Skip to content

Commit

Permalink
Fixed config override with GetBackendWithConfig (#1895)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zyko0 authored Jul 30, 2024
1 parent 1066bd9 commit cc5e657
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
37 changes: 19 additions & 18 deletions stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -1102,45 +1102,46 @@ func GetBackend(backendType SupportedBackend) Backend {
// configuration struct that will configure certain aspects of the backend
// that's return.
func GetBackendWithConfig(backendType SupportedBackend, config *BackendConfig) Backend {
if config.HTTPClient == nil {
config.HTTPClient = httpClient
cfg := *config
if cfg.HTTPClient == nil {
cfg.HTTPClient = httpClient
}

if config.LeveledLogger == nil {
config.LeveledLogger = DefaultLeveledLogger
if cfg.LeveledLogger == nil {
cfg.LeveledLogger = DefaultLeveledLogger
}

if config.MaxNetworkRetries == nil {
config.MaxNetworkRetries = Int64(DefaultMaxNetworkRetries)
if cfg.MaxNetworkRetries == nil {
cfg.MaxNetworkRetries = Int64(DefaultMaxNetworkRetries)
}

switch backendType {
case APIBackend:
if config.URL == nil {
config.URL = String(APIURL)
if cfg.URL == nil {
cfg.URL = String(APIURL)
}

config.URL = String(normalizeURL(*config.URL))
cfg.URL = String(normalizeURL(*cfg.URL))

return newBackendImplementation(backendType, config)
return newBackendImplementation(backendType, &cfg)

case UploadsBackend:
if config.URL == nil {
config.URL = String(UploadsURL)
if cfg.URL == nil {
cfg.URL = String(UploadsURL)
}

config.URL = String(normalizeURL(*config.URL))
cfg.URL = String(normalizeURL(*cfg.URL))

return newBackendImplementation(backendType, config)
return newBackendImplementation(backendType, &cfg)

case ConnectBackend:
if config.URL == nil {
config.URL = String(ConnectURL)
if cfg.URL == nil {
cfg.URL = String(ConnectURL)
}

config.URL = String(normalizeURL(*config.URL))
cfg.URL = String(normalizeURL(*cfg.URL))

return newBackendImplementation(backendType, config)
return newBackendImplementation(backendType, &cfg)
}

return nil
Expand Down
18 changes: 18 additions & 0 deletions stripe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,24 @@ func TestFormatURLPath(t *testing.T) {
FormatURLPath("/v1/resources/%s", "%"))
}

func TestGetBackendWithConfig(t *testing.T) {
// No config overrides: https://github.com/stripe/stripe-go/issues/1894
{
config := &BackendConfig{}
_ = GetBackendWithConfig(
APIBackend,
config,
).(*BackendImplementation)

// Config properties left unchanged
assert.Nil(t, config.EnableTelemetry)
assert.Nil(t, config.HTTPClient)
assert.Nil(t, config.LeveledLogger)
assert.Nil(t, config.MaxNetworkRetries)
assert.Nil(t, config.URL)
}
}

func TestGetBackendWithConfig_Loggers(t *testing.T) {
leveledLogger := &LeveledLogger{}

Expand Down

0 comments on commit cc5e657

Please sign in to comment.