From b719ec5a843ee10abce7b1de9fc5fb90e742f153 Mon Sep 17 00:00:00 2001 From: lei shi Date: Thu, 22 Feb 2024 15:01:27 -0800 Subject: [PATCH] remove trailing slash in mercury credentials' URLs --- .../ocr2keeper/evmregistry/v21/registry.go | 4 ++ .../evmregistry/v21/registry_test.go | 53 ++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/registry.go b/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/registry.go index fd7bfa91d7f..d492a956e2a 100644 --- a/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/registry.go +++ b/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/registry.go @@ -6,6 +6,7 @@ import ( "fmt" "math/big" "net/http" + "strings" "sync" "time" @@ -149,6 +150,9 @@ func NewMercuryConfig(credentials *types.MercuryCredentials, abi abi.ABI) *Mercu } func (c *MercuryConfig) Credentials() *types.MercuryCredentials { + // remove the trailing slash from the URL if it exists + c.cred.URL = strings.TrimRight(c.cred.URL, "/") + c.cred.LegacyURL = strings.TrimRight(c.cred.LegacyURL, "/") return c.cred } diff --git a/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/registry_test.go b/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/registry_test.go index dc48c3d75f6..4e2040ee4c5 100644 --- a/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/registry_test.go +++ b/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/registry_test.go @@ -8,7 +8,7 @@ import ( "testing" "time" - types2 "github.com/smartcontractkit/chainlink-automation/pkg/v3/types" + "github.com/patrickmn/go-cache" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" @@ -16,6 +16,9 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" + types2 "github.com/smartcontractkit/chainlink-automation/pkg/v3/types" + "github.com/smartcontractkit/chainlink-common/pkg/types" + types3 "github.com/smartcontractkit/chainlink/v2/core/chains/evm/headtracker/types" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller/mocks" @@ -30,6 +33,54 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/services/pg" ) +func TestCredentials_RemoveTrailingSlash(t *testing.T) { + tests := []struct { + Name string + URL string + LegacyURL string + }{ + { + Name: "Both have trailing slashes", + URL: "http://example.com/", + LegacyURL: "http://legacy.example.com/", + }, + { + Name: "One has trailing slashes", + URL: "http://example.com", + LegacyURL: "http://legacy.example.com/", + }, + { + Name: "Neither has trailing slashes", + URL: "http://example.com", + LegacyURL: "http://legacy.example.com", + }, + } + + for _, test := range tests { + t.Run(test.Name, func(t *testing.T) { + mockConfig := &MercuryConfig{ + cred: &types.MercuryCredentials{ + URL: test.URL, + LegacyURL: test.LegacyURL, + Username: "user", + Password: "pass", + }, + Abi: core.StreamsCompatibleABI, + AllowListCache: cache.New(defaultPluginRetryExpiration, cleanupInterval), + pluginRetryCache: cache.New(defaultPluginRetryExpiration, cleanupInterval), + } + + result := mockConfig.Credentials() + + // Assert that trailing slashes are removed + assert.Equal(t, "http://example.com", result.URL) + assert.Equal(t, "http://legacy.example.com", result.LegacyURL) + assert.Equal(t, "user", result.Username) + assert.Equal(t, "pass", result.Password) + }) + } +} + func TestPollLogs(t *testing.T) { tests := []struct { Name string