Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed config override with GetBackendWithConfig #1895

Merged
merged 2 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading