From 8d37d49889d9202cace7e444f48238547d19a9c4 Mon Sep 17 00:00:00 2001 From: Zyko0 Date: Wed, 24 Jul 2024 18:21:32 +0200 Subject: [PATCH] Fixed config override with GetBackendWithConfig --- stripe.go | 37 +++++++++++++++++++------------------ stripe_test.go | 18 ++++++++++++++++++ 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/stripe.go b/stripe.go index 1f66b24f3c..d841ddc0f4 100644 --- a/stripe.go +++ b/stripe.go @@ -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 diff --git a/stripe_test.go b/stripe_test.go index 69d2254e9b..335465d19d 100644 --- a/stripe_test.go +++ b/stripe_test.go @@ -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{}