Skip to content

Commit

Permalink
pass viper to function for cleaner testing
Browse files Browse the repository at this point in the history
  • Loading branch information
obs-gh-mattcotter committed Oct 28, 2024
1 parent 5b089b7 commit 77bb679
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
8 changes: 4 additions & 4 deletions internal/commands/sendtestdata/postdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}
7 changes: 4 additions & 3 deletions internal/commands/sendtestdata/postdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
3 changes: 2 additions & 1 deletion internal/commands/sendtestdata/sendtestdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand Down

0 comments on commit 77bb679

Please sign in to comment.