Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions internal/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ func (c *Client) baseURL() string {

func (c *Client) get(ctx context.Context, path string, result any) error {
url := c.baseURL() + path
if strings.Contains(path, "?") {
url += "&source=cli"
} else {
url += "?source=cli"
}

req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
Expand Down
30 changes: 30 additions & 0 deletions internal/api/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,36 @@ func TestRetryAfterInvalidFallback(t *testing.T) {
assert.ErrorIs(t, err, ErrRateLimited)
}

func TestSourceCLIAppendedWithoutExistingQuery(t *testing.T) {
var gotSource string
c, srv := testClient(func(w http.ResponseWriter, r *http.Request) {
gotSource = r.URL.Query().Get("source")
w.WriteHeader(200)
_, _ = w.Write([]byte("{}"))
})
defer srv.Close()

var result map[string]any
_ = c.get(context.Background(), "/test", &result)
assert.Equal(t, "cli", gotSource)
}

func TestSourceCLIAppendedWithExistingQuery(t *testing.T) {
var gotSource, gotExisting string
c, srv := testClient(func(w http.ResponseWriter, r *http.Request) {
gotSource = r.URL.Query().Get("source")
gotExisting = r.URL.Query().Get("foo")
w.WriteHeader(200)
_, _ = w.Write([]byte("{}"))
})
defer srv.Close()

var result map[string]any
_ = c.get(context.Background(), "/test?foo=bar", &result)
assert.Equal(t, "cli", gotSource)
assert.Equal(t, "bar", gotExisting)
}

func TestRequirePaid(t *testing.T) {
cfg := &config.Config{Tier: config.TierDemo}
c := NewClient(cfg)
Expand Down
2 changes: 1 addition & 1 deletion internal/ws/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (c *Client) Close() error {

// connect dials the WebSocket endpoint and waits for the welcome message.
func (c *Client) connect(ctx context.Context) error {
url := c.wsURL + "?x_cg_pro_api_key=" + c.cfg.APIKey
url := c.wsURL + "?x_cg_pro_api_key=" + c.cfg.APIKey + "&source=cli"

header := http.Header{}
header.Set("User-Agent", c.UserAgent)
Expand Down
36 changes: 36 additions & 0 deletions internal/ws/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,42 @@ func TestConnect_APIKeyInQueryParam(t *testing.T) {
require.NoError(t, client.Close())
}

func TestConnect_SourceCLIQueryParam(t *testing.T) {
var receivedSource string
var mu sync.Mutex

srv := newTestWSServer(t, func(conn *websocket.Conn) {
happyHandshake(t, conn)
for {
if _, _, err := conn.ReadMessage(); err != nil {
return
}
}
})
origHandler := srv.Config.Handler
srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
receivedSource = r.URL.Query().Get("source")
mu.Unlock()
origHandler.ServeHTTP(w, r)
})

client := NewClient(paidCfg(), []string{"bitcoin"})
client.SetURL(wsURL(srv))

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

_, err := client.Connect(ctx)
require.NoError(t, err)

mu.Lock()
assert.Equal(t, "cli", receivedSource)
mu.Unlock()

require.NoError(t, client.Close())
}

func TestConnect_UserAgentHeader(t *testing.T) {
var gotUA string
var mu sync.Mutex
Expand Down