Skip to content

Commit 328e767

Browse files
authored
feat: swappable http client (#173)
1 parent 1dd3eba commit 328e767

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

client.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ func NewClient(apiKey, apiSecret string) (*Client, error) {
9191
return client, nil
9292
}
9393

94+
// SetClient sets a new underlying HTTP client.
95+
func (c *Client) SetClient(client *http.Client) {
96+
c.HTTP = client
97+
}
98+
9499
// Channel returns a Channel object for future API calls.
95100
func (c *Client) Channel(channelType, channelID string) *Channel {
96101
return &Channel{

client_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package stream_chat
22

33
import (
44
"context"
5+
"net/http"
6+
"net/url"
57
"testing"
68
"time"
79

@@ -25,6 +27,24 @@ func initChannel(t *testing.T, c *Client, membersID ...string) *Channel {
2527
return resp.Channel
2628
}
2729

30+
func TestClient_SwapHttpClient(t *testing.T) {
31+
c := initClient(t)
32+
ctx := context.Background()
33+
34+
tr := http.DefaultTransport.(*http.Transport).Clone() //nolint:forcetypeassert
35+
proxyURL, _ := url.Parse("http://getstream.io")
36+
tr.Proxy = http.ProxyURL(proxyURL)
37+
cl := &http.Client{Transport: tr}
38+
c.SetClient(cl)
39+
_, err := c.GetAppConfig(ctx)
40+
require.Error(t, err)
41+
42+
cl = &http.Client{}
43+
c.SetClient(cl)
44+
_, err = c.GetAppConfig(ctx)
45+
require.NoError(t, err)
46+
}
47+
2848
//nolint: lll
2949
func TestClient_CreateToken(t *testing.T) {
3050
type args struct {

0 commit comments

Comments
 (0)