Skip to content

Commit 6ddeeb4

Browse files
authored
feat: initializes proxy for Transport if needed (#146)
* feat: initializes proxy for Transport if needed * chore: adds test checking client creation with HTTP_PROXY / HTTPS_PROXY / NO_PROXY env vars
1 parent e9a7ebc commit 6ddeeb4

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

github/client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func NewClient(config *Config) (*Client, error) {
7575
Timeout: reqTimeout / 2,
7676
}).DialContext,
7777
TLSHandshakeTimeout: reqTimeout / 2,
78+
Proxy: http.ProxyFromEnvironment,
7879
}
7980

8081
// Create an GitHub App installation authenticated clone of transport.

github/client_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import (
77
"fmt"
88
"net/http"
99
"net/http/httptest"
10+
"os"
1011
"testing"
1112
"time"
1213

14+
"github.com/bradleyfalzon/ghinstallation"
1315
"github.com/hashicorp/vault/sdk/logical"
1416
"gotest.tools/assert"
1517

@@ -541,3 +543,35 @@ func TestClient_RevokeToken(t *testing.T) {
541543
})
542544
}
543545
}
546+
547+
func TestNewClient_WithProxy(t *testing.T) {
548+
t.Parallel()
549+
550+
proxyURL := "http://proxy.example.com:8080"
551+
os.Setenv("HTTP_PROXY", proxyURL)
552+
os.Setenv("HTTPS_PROXY", proxyURL)
553+
os.Setenv("NO_PROXY", "localhost,127.0.0.1")
554+
defer os.Unsetenv("HTTP_PROXY")
555+
defer os.Unsetenv("HTTPS_PROXY")
556+
defer os.Unsetenv("NO_PROXY")
557+
558+
conf := &Config{
559+
AppID: testAppID1,
560+
PrvKey: testPrvKeyValid,
561+
BaseURL: testBaseURLValid,
562+
}
563+
564+
client, err := NewClient(conf)
565+
assert.NilError(t, err)
566+
567+
// checks that the Transport wrapped by the client with a AppsTransport handles Proxy properly
568+
appTransport, ok := client.installationsClient.Transport.(*ghinstallation.AppsTransport)
569+
transport := appTransport.Client.(*http.Client).Transport.(*http.Transport)
570+
assert.Assert(t, ok)
571+
assert.Assert(t, appTransport != nil)
572+
573+
req, _ := http.NewRequest("GET", "http://example.com", nil)
574+
proxy, err := transport.Proxy(req)
575+
assert.NilError(t, err)
576+
assert.Equal(t, proxy.String(), proxyURL)
577+
}

0 commit comments

Comments
 (0)