Skip to content

Commit

Permalink
Add Transport Option to allow egress proxies (#217)
Browse files Browse the repository at this point in the history
This commit adds a `Transport` to the `Options` struct when constructing
a new instance of the Go SDK. If set it allows the end user to pass in
their own `http.RoundTripper` instead of using `http.DefaultTransport`.

The reasoning for this change it to allow users to provide a transport
which is capabile of acting as middleware for the outbound requests made
by the SDK (such as allowing routing via an authenticated egress proxy).

Co-authored-by: Dominic Black <[email protected]>
  • Loading branch information
kenny-statsig and DomBlack authored Aug 21, 2024
1 parent 3ad0fae commit 8f92476
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
5 changes: 3 additions & 2 deletions statsig.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ func Initialize(sdkKey string) {
type Options struct {
API string `json:"api"`
APIOverrides APIOverrides `json:"api_overrides"`
Environment Environment `json:"environment"`
LocalMode bool `json:"localMode"`
Transport http.RoundTripper
Environment Environment `json:"environment"`
LocalMode bool `json:"localMode"`
ConfigSyncInterval time.Duration
IDListSyncInterval time.Duration
LoggingInterval time.Duration
Expand Down
7 changes: 5 additions & 2 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ func newTransport(secret string, options *Options) *transport {
api: api,
metadata: getStatsigMetadata(),
sdkKey: secret,
client: &http.Client{Timeout: time.Second * 3},
options: options,
client: &http.Client{
Timeout: time.Second * 3,
Transport: options.Transport,
},
options: options,
}
}

Expand Down
21 changes: 21 additions & 0 deletions transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)

Expand Down Expand Up @@ -85,3 +86,23 @@ func TestRetries(t *testing.T) {
t.Errorf("Expected successful request but got error")
}
}

func TestProxy(t *testing.T) {
testServerHit := false
testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(http.StatusOK)
testServerHit = true
}))
defer testServer.Close()
in := Empty{}
var out ServerResponse
url, _ := url.Parse(testServer.URL)
opt := &Options{
Transport: &http.Transport{Proxy: http.ProxyURL(url)},
}
n := newTransport("secret-123", opt)
_, _ = n.post("/123", in, &out, RequestOptions{})
if !testServerHit {
t.Errorf("Expected request to hit proxy server")
}
}

0 comments on commit 8f92476

Please sign in to comment.