From 77bb67923aa1e6e3d98a7427b6fdfa1c844d4232 Mon Sep 17 00:00:00 2001 From: Matt Cotter Date: Thu, 24 Oct 2024 15:22:47 -0500 Subject: [PATCH] pass viper to function for cleaner testing --- internal/commands/sendtestdata/postdata.go | 8 ++++---- internal/commands/sendtestdata/postdata_test.go | 7 ++++--- internal/commands/sendtestdata/sendtestdata.go | 3 ++- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/internal/commands/sendtestdata/postdata.go b/internal/commands/sendtestdata/postdata.go index 7c050d68..cfa780df 100644 --- a/internal/commands/sendtestdata/postdata.go +++ b/internal/commands/sendtestdata/postdata.go @@ -40,9 +40,9 @@ func PostTestData(data any, URL string, headers map[string]string) (string, erro return bodyString, nil } -func PostTestDataToObserve(data any, path string) (string, error) { - collector_url := viper.GetString("observe_url") - endpoint := fmt.Sprintf("%s/v1/http/%s", strings.TrimRight(collector_url, "/"), strings.TrimLeft(path, "/")) - authToken := fmt.Sprintf("Bearer %s", viper.GetString("token")) +func PostTestDataToObserve(data any, path string, v *viper.Viper) (string, error) { + collector_url := v.GetString("observe_url") + endpoint := fmt.Sprintf("%s/v1/http%s", strings.TrimRight(collector_url, "/"), path) + authToken := fmt.Sprintf("Bearer %s", v.GetString("token")) return PostTestData(data, endpoint, map[string]string{"Authorization": authToken}) } diff --git a/internal/commands/sendtestdata/postdata_test.go b/internal/commands/sendtestdata/postdata_test.go index 3408c98e..4a64bcd0 100644 --- a/internal/commands/sendtestdata/postdata_test.go +++ b/internal/commands/sendtestdata/postdata_test.go @@ -44,10 +44,11 @@ func TestPostTestDataToObserve(t *testing.T) { httpmock.NewStringResponder(200, expectedResponse), ) - viper.Set("observe_url", "https://123456.collect.observe-eng.com/") - viper.Set("token", "test-token") + v := viper.New() + v.Set("observe_url", "https://123456.collect.observe-eng.com/") + v.Set("token", "test-token") testData := map[string]string{"hello": "world"} - resp, err := PostTestDataToObserve(testData, "/test") + resp, err := PostTestDataToObserve(testData, "/test", v) assert.NoError(t, err) assert.Equal(t, expectedResponse, resp) } diff --git a/internal/commands/sendtestdata/sendtestdata.go b/internal/commands/sendtestdata/sendtestdata.go index 6f794607..1689147f 100644 --- a/internal/commands/sendtestdata/sendtestdata.go +++ b/internal/commands/sendtestdata/sendtestdata.go @@ -9,6 +9,7 @@ import ( "github.com/observeinc/observe-agent/internal/root" "github.com/spf13/cobra" + "github.com/spf13/viper" ) const TestDataPath = "/observe-agent/test" @@ -33,7 +34,7 @@ func NewSendTestDataCmd() *cobra.Command { } else { testData = defaultTestData } - respBody, err := PostTestDataToObserve(testData, TestDataPath) + respBody, err := PostTestDataToObserve(testData, TestDataPath, viper.GetViper()) if err != nil { return err }