Skip to content
2 changes: 1 addition & 1 deletion cloudapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (c *Client) prepareHeaders(req *http.Request) {
req.Header.Set("Content-Type", "application/json")
}

if c.token != "" {
if c.token != "" && req.Header.Get("Authorization") == "" {
req.Header.Set("Authorization", fmt.Sprintf("Token %s", c.token))
}

Expand Down
47 changes: 47 additions & 0 deletions cloudapi/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cloudapi

import (
"net/http"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestPrepareHeaders(t *testing.T) {
t.Parallel()

tests := []struct {
name string
preset string
wantAuth string
}{
{
name: "respects preset authorization",
preset: "Bearer pre-set-token",
wantAuth: "Bearer pre-set-token",
},
{
name: "sets token auth when unset",
preset: "",
wantAuth: "Token mytoken",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

c := NewClient(nil, "mytoken", "http://example.com", "v0.1", 1*time.Second)
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "http://example.com", nil)
require.NoError(t, err)
if tt.preset != "" {
req.Header.Set("Authorization", tt.preset)
}
c.prepareHeaders(req)

assert.Equal(t, tt.wantAuth, req.Header.Get("Authorization"))
})
}
}
13 changes: 12 additions & 1 deletion cloudapi/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ type Config struct {
// no test run id, and we may still need an identifier to correlate all the things.
PushRefID null.String `json:"pushRefID" envconfig:"K6_CLOUD_PUSH_REF_ID"`

// MetricsPushURL is the URL to push metrics to; set from the provisioning API response.
MetricsPushURL null.String `json:"metricsPushURL"`
// TestRunToken is the scoped token for metrics push; set from the provisioning API response.
TestRunToken null.String `json:"testRunToken"`

// Defines the max allowed number of time series in a single batch.
MaxTimeSeriesInBatch null.Int `json:"maxTimeSeriesInBatch" envconfig:"K6_CLOUD_MAX_TIME_SERIES_IN_BATCH"`

Expand Down Expand Up @@ -104,7 +109,7 @@ func NewConfig() Config {

// Apply saves config non-zero config values from the passed config in the receiver.
//
//nolint:cyclop,gocognit
//nolint:cyclop,gocognit,funlen
func (c Config) Apply(cfg Config) Config {
if cfg.StackID.Valid {
c.StackID = cfg.StackID
Expand Down Expand Up @@ -136,6 +141,12 @@ func (c Config) Apply(cfg Config) Config {
if cfg.PushRefID.Valid {
c.PushRefID = cfg.PushRefID
}
if cfg.MetricsPushURL.Valid {
c.MetricsPushURL = cfg.MetricsPushURL
}
if cfg.TestRunToken.Valid {
c.TestRunToken = cfg.TestRunToken
}
if cfg.WebAppURL.Valid {
c.WebAppURL = cfg.WebAppURL
}
Expand Down
Loading
Loading