Skip to content

Commit

Permalink
Support HTTP Proxy from environment (#2686)
Browse files Browse the repository at this point in the history
  • Loading branch information
蒋航 authored Mar 8, 2023
1 parent 73075b9 commit 14bb610
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,11 @@ func NewClient(httpClient *http.Client) *Client {
return c
}

// NewClientWithEnvProxy enhances NewClient with the HttpProxy env.
func NewClientWithEnvProxy() *Client {
return NewClient(&http.Client{Transport: &http.Transport{Proxy: http.ProxyFromEnvironment}})
}

// NewTokenClient returns a new GitHub API client authenticated with the provided token.
func NewTokenClient(ctx context.Context, token string) *Client {
return NewClient(oauth2.NewClient(ctx, oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})))
Expand Down
7 changes: 7 additions & 0 deletions github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,13 @@ func TestNewClient(t *testing.T) {
}
}

func TestNewClientWithEnvProxy(t *testing.T) {
client := NewClientWithEnvProxy()
if got, want := client.BaseURL.String(), defaultBaseURL; got != want {
t.Errorf("NewClient BaseURL is %v, want %v", got, want)
}
}

func TestClient(t *testing.T) {
c := NewClient(nil)
c2 := c.Client()
Expand Down

0 comments on commit 14bb610

Please sign in to comment.