From 1e99be1b166403a8aaf1d2d2b3db6ea9c5a72fc2 Mon Sep 17 00:00:00 2001 From: Parth Suthar Date: Tue, 20 Aug 2024 16:45:04 -0400 Subject: [PATCH] feat: update config url to support v2 configs (#267) --- .github/workflows/run-test-harness.yml | 2 +- configmanager.go | 5 +++-- configmanager_test.go | 23 +++++++++++++---------- testing_helpers_test.go | 4 +++- 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/.github/workflows/run-test-harness.yml b/.github/workflows/run-test-harness.yml index 46d1f2ff..7dd7cf88 100644 --- a/.github/workflows/run-test-harness.yml +++ b/.github/workflows/run-test-harness.yml @@ -12,7 +12,7 @@ jobs: steps: - uses: DevCycleHQ/test-harness@main env: - SDK_CAPABILITIES: '["cloud","edgeDB","clientCustomData","multithreading","defaultReason","etagReporting","lastModifiedHeader","sdkConfigEvent","clientUUID"]' + SDK_CAPABILITIES: '["cloud","edgeDB","clientCustomData","multithreading","defaultReason","etagReporting","lastModifiedHeader","sdkConfigEvent","clientUUID","v2Config"]' with: sdks-to-test: '["go"]' sdk-github-sha: ${{github.event.pull_request.head.sha}} diff --git a/configmanager.go b/configmanager.go index 9d992720..79d78c8a 100644 --- a/configmanager.go +++ b/configmanager.go @@ -4,12 +4,13 @@ import ( "context" "encoding/json" "fmt" - "github.com/devcyclehq/go-server-sdk/v2/api" "io" "net/http" "sync" "time" + "github.com/devcyclehq/go-server-sdk/v2/api" + "github.com/devcyclehq/go-server-sdk/v2/util" ) @@ -392,7 +393,7 @@ func (e *EnvironmentConfigManager) setConfig(config []byte, eTag, rayId, lastMod func (e *EnvironmentConfigManager) getConfigURL() string { configBasePath := e.cfg.ConfigCDNBasePath - return fmt.Sprintf("%s/config/v1/server/%s.json", configBasePath, e.sdkKey) + return fmt.Sprintf("%s/config/v2/server/%s.json", configBasePath, e.sdkKey) } func (e *EnvironmentConfigManager) HasConfig() bool { diff --git a/configmanager_test.go b/configmanager_test.go index c28b4c06..fcf2b018 100644 --- a/configmanager_test.go +++ b/configmanager_test.go @@ -2,12 +2,13 @@ package devcycle import ( "fmt" - "github.com/devcyclehq/go-server-sdk/v2/api" - "github.com/jarcoal/httpmock" - "github.com/stretchr/testify/require" "sync" "testing" "time" + + "github.com/devcyclehq/go-server-sdk/v2/api" + "github.com/jarcoal/httpmock" + "github.com/stretchr/testify/require" ) type recordingConfigReceiver struct { @@ -62,6 +63,8 @@ func (r *recordingConfigReceiver) GetLastModified() string { return r.lastModified } +const CONFIG_SDK_URL = "https://config-cdn.devcycle.com/config/v2/server/%s.json" + func TestEnvironmentConfigManager_fetchConfig_success(t *testing.T) { sdkKey, _ := httpConfigMock(200) @@ -113,7 +116,7 @@ func TestEnvironmentConfigManager_fetchConfig_refuseOld(t *testing.T) { secondResponse := httpCustomConfigMock(sdkKey, 200, test_config, true, olderHeaders) thirdResponse := httpCustomConfigMock(sdkKey, 200, test_config, true, newestHeaders) - httpmock.RegisterResponder("GET", "https://config-cdn.devcycle.com/config/v1/server/"+sdkKey+".json", + httpmock.RegisterResponder("GET", fmt.Sprintf(CONFIG_SDK_URL, sdkKey), firstResponse.Then(secondResponse).Then(thirdResponse), ) localBucketing := &recordingConfigReceiver{} @@ -191,7 +194,7 @@ func TestEnvironmentConfigManager_fetchConfig_retries500(t *testing.T) { error500Response := httpCustomConfigMock(sdkKey, 500, "Connection error", true) - httpmock.RegisterResponder("GET", "https://config-cdn.devcycle.com/config/v1/server/"+sdkKey+".json", + httpmock.RegisterResponder("GET", fmt.Sprintf(CONFIG_SDK_URL, sdkKey), errorResponseChain(sdkKey, error500Response, CONFIG_RETRIES), ) @@ -226,7 +229,7 @@ func TestEnvironmentConfigManager_fetchConfig_retries_until_abort(t *testing.T) firstResponse := httpCustomConfigMock(sdkKey, 200, test_config, true, initialHeaders) secondResponse := httpCustomConfigMock(sdkKey, 200, test_config, true, olderHeaders) - httpmock.RegisterResponder("GET", "https://config-cdn.devcycle.com/config/v1/server/"+sdkKey+".json", + httpmock.RegisterResponder("GET", fmt.Sprintf(CONFIG_SDK_URL, sdkKey), firstResponse.Then(secondResponse).Then(secondResponse).Then(secondResponse), ) localBucketing := &recordingConfigReceiver{} @@ -275,7 +278,7 @@ func TestEnvironmentConfigManager_fetchConfig_retries_until_abort(t *testing.T) func TestEnvironmentConfigManager_fetchConfig_retries_errors(t *testing.T) { sdkKey := generateTestSDKKey() connectionErrorResponse := httpCustomConfigMock(sdkKey, 500, "Connection error", true) - httpmock.RegisterResponder("GET", "https://config-cdn.devcycle.com/config/v1/server/"+sdkKey+".json", + httpmock.RegisterResponder("GET", fmt.Sprintf(CONFIG_SDK_URL, sdkKey), errorResponseChain(sdkKey, connectionErrorResponse, CONFIG_RETRIES), ) @@ -301,7 +304,7 @@ func TestEnvironmentConfigManager_fetchConfig_retries_errors_sse(t *testing.T) { httpSSEConnectionMock() connectionErrorResponse := httpmock.NewErrorResponder(fmt.Errorf("connection error")) - httpmock.RegisterResponder("GET", "https://config-cdn.devcycle.com/config/v1/server/"+sdkKey+".json", + httpmock.RegisterResponder("GET", fmt.Sprintf(CONFIG_SDK_URL, sdkKey), errorResponseChain(sdkKey, connectionErrorResponse, CONFIG_RETRIES, httpSSEConfigMock), ) @@ -321,7 +324,7 @@ func TestEnvironmentConfigManager_fetchConfig_returns_errors(t *testing.T) { sdkKey := generateTestSDKKey() connectionErrorResponse := httpmock.NewErrorResponder(fmt.Errorf("connection error")) - httpmock.RegisterResponder("GET", "https://config-cdn.devcycle.com/config/v1/server/"+sdkKey+".json", + httpmock.RegisterResponder("GET", fmt.Sprintf(CONFIG_SDK_URL, sdkKey), errorResponseChain(sdkKey, connectionErrorResponse, CONFIG_RETRIES+1), ) @@ -338,7 +341,7 @@ func TestEnvironmentConfigManager_fetchConfig_returns_errors_sse(t *testing.T) { connectionErrorResponse := httpmock.NewErrorResponder(fmt.Errorf("connection error")) sdkKey := generateTestSDKKey() - httpmock.RegisterResponder("GET", "https://config-cdn.devcycle.com/config/v1/server/"+sdkKey+".json", + httpmock.RegisterResponder("GET", fmt.Sprintf(CONFIG_SDK_URL, sdkKey), errorResponseChain(sdkKey, connectionErrorResponse, CONFIG_RETRIES+1), ) diff --git a/testing_helpers_test.go b/testing_helpers_test.go index 0c84817e..4a98e516 100644 --- a/testing_helpers_test.go +++ b/testing_helpers_test.go @@ -3,6 +3,7 @@ package devcycle import ( _ "embed" "flag" + "fmt" "log" "math/rand" "net/http" @@ -107,7 +108,8 @@ func httpCustomConfigMock(sdkKey string, respcode int, config string, skipRegist return resp, nil } if !skipRegister { - httpmock.RegisterResponder("GET", "https://config-cdn.devcycle.com/config/v1/server/"+sdkKey+".json", responder) + const CONFIG_URL_FORMAT = "https://config-cdn.devcycle.com/config/v2/server/%s.json" + httpmock.RegisterResponder("GET", fmt.Sprintf(CONFIG_URL_FORMAT, sdkKey), responder) } return responder }