Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
sachaarbonel committed Oct 25, 2024
1 parent ea2ef07 commit 4fc50cb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
18 changes: 13 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ func WithLogger(logger Logger) ClientOption {
}
}

// With authToken sets the auth token for the client.
func WithAuthToken(authToken string) ClientOption {
return func(c *Client) {
c.authToken = authToken
}
}

type tokenOptions struct {
claims *Claims
expiration *time.Duration
Expand Down Expand Up @@ -161,12 +168,13 @@ func newClient(apiKey, apiSecret string, options ...ClientOption) (*Client, erro
Timeout: client.defaultTimeout,
}

token, err := client.createTokenWithClaims(jwt.MapClaims{"server": true})
if err != nil {
return nil, err
if client.authToken == "" {
token, err := client.createTokenWithClaims(jwt.MapClaims{"server": true})
if err != nil {
return nil, err
}
client.authToken = token
}

client.authToken = token
return client, nil
}

Expand Down
15 changes: 3 additions & 12 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,7 @@ func TestRequestURL(t *testing.T) {
}

func TestNewRequest(t *testing.T) {
client := &Client{
baseUrl: "https://api.example.com", // Set baseUrl
apiKey: "testkey",
authToken: "Bearer testtoken",
}
client, _ := newClient("testkey", "testsecret", WithBaseUrl("https://api.example.com"), WithAuthToken("Bearer testtoken"))

ctx := context.Background()

Expand Down Expand Up @@ -278,10 +274,7 @@ func TestNewRequest(t *testing.T) {
})

t.Run("Unsupported data type", func(t *testing.T) {
client := &Client{
baseUrl: "https://api.stream-io-api.com",
apiKey: "key",
}
client, _ := newClient("testkey", "testsecret", WithBaseUrl("https://api.example.com"))
ctx := context.Background()
unsupportedData := make(chan int)

Expand All @@ -293,9 +286,7 @@ func TestNewRequest(t *testing.T) {
}

func TestSetHeaders(t *testing.T) {
client := &Client{
authToken: "Bearer testtoken",
}
client, _ := newClient("testkey", "testsecret", WithAuthToken("Bearer testtoken"))

req, err := http.NewRequest(http.MethodGet, "https://api.example.com", nil)
if err != nil {
Expand Down

0 comments on commit 4fc50cb

Please sign in to comment.