Skip to content

Commit

Permalink
Return new http.Client when nil passed to NewClient (google#1178)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmlewis authored May 30, 2019
1 parent 0ab167c commit 6264c79
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
6 changes: 3 additions & 3 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,12 @@ func addOptions(s string, opt interface{}) (string, error) {
}

// NewClient returns a new GitHub API client. If a nil httpClient is
// provided, http.DefaultClient will be used. To use API methods which require
// provided, a new http.Client will be used. To use API methods which require
// authentication, provide an http.Client that will perform the authentication
// for you (such as that provided by the golang.org/x/oauth2 library).
func NewClient(httpClient *http.Client) *Client {
if httpClient == nil {
httpClient = http.DefaultClient
httpClient = &http.Client{}
}
baseURL, _ := url.Parse(defaultBaseURL)
uploadURL, _ := url.Parse(uploadBaseURL)
Expand Down Expand Up @@ -283,7 +283,7 @@ func NewClient(httpClient *http.Client) *Client {
// NewEnterpriseClient returns a new GitHub API client with provided
// base URL and upload URL (often the same URL).
// If either URL does not have a trailing slash, one is added automatically.
// If a nil httpClient is provided, http.DefaultClient will be used.
// If a nil httpClient is provided, a new http.Client will be used.
//
// Note that NewEnterpriseClient is a convenience helper only;
// its behavior is equivalent to using NewClient, followed by setting
Expand Down
5 changes: 5 additions & 0 deletions github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ func TestNewClient(t *testing.T) {
if got, want := c.UserAgent, userAgent; got != want {
t.Errorf("NewClient UserAgent is %v, want %v", got, want)
}

c2 := NewClient(nil)
if c.client == c2.client {
t.Error("NewClient returned same http.Clients, but they should differ")
}
}

func TestNewEnterpriseClient(t *testing.T) {
Expand Down

0 comments on commit 6264c79

Please sign in to comment.