Skip to content

Commit a7e8da7

Browse files
pass viper to function for cleaner testing
1 parent b1fd8e0 commit a7e8da7

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

internal/commands/sendtestdata/postdata.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ func PostTestData(data any, URL string, headers map[string]string) (string, erro
4040
return bodyString, nil
4141
}
4242

43-
func PostTestDataToObserve(data any, path string) (string, error) {
44-
collector_url := viper.GetString("observe_url")
45-
endpoint := fmt.Sprintf("%s/v1/http/%s", strings.TrimRight(collector_url, "/"), strings.TrimLeft(path, "/"))
46-
authToken := fmt.Sprintf("Bearer %s", viper.GetString("token"))
43+
func PostTestDataToObserve(data any, path string, v *viper.Viper) (string, error) {
44+
collector_url := v.GetString("observe_url")
45+
endpoint := fmt.Sprintf("%s/v1/http%s", strings.TrimRight(collector_url, "/"), path)
46+
authToken := fmt.Sprintf("Bearer %s", v.GetString("token"))
4747
return PostTestData(data, endpoint, map[string]string{"Authorization": authToken})
4848
}

internal/commands/sendtestdata/postdata_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,11 @@ func TestPostTestDataToObserve(t *testing.T) {
4444
httpmock.NewStringResponder(200, expectedResponse),
4545
)
4646

47-
viper.Set("observe_url", "https://123456.collect.observe-eng.com/")
48-
viper.Set("token", "test-token")
47+
v := viper.New()
48+
v.Set("observe_url", "https://123456.collect.observe-eng.com/")
49+
v.Set("token", "test-token")
4950
testData := map[string]string{"hello": "world"}
50-
resp, err := PostTestDataToObserve(testData, "/test")
51+
resp, err := PostTestDataToObserve(testData, "/test", v)
5152
assert.NoError(t, err)
5253
assert.Equal(t, expectedResponse, resp)
5354
}

internal/commands/sendtestdata/sendtestdata.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/observeinc/observe-agent/internal/root"
1111
"github.com/spf13/cobra"
12+
"github.com/spf13/viper"
1213
)
1314

1415
const TestDataPath = "/observe-agent/test"
@@ -33,7 +34,7 @@ func NewSendTestDataCmd() *cobra.Command {
3334
} else {
3435
testData = defaultTestData
3536
}
36-
respBody, err := PostTestDataToObserve(testData, TestDataPath)
37+
respBody, err := PostTestDataToObserve(testData, TestDataPath, viper.GetViper())
3738
if err != nil {
3839
return err
3940
}

0 commit comments

Comments
 (0)