Skip to content

Commit 6264c79

Browse files
authored
Return new http.Client when nil passed to NewClient (#1178)
Fixes #1173.
1 parent 0ab167c commit 6264c79

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

github/github.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,12 @@ func addOptions(s string, opt interface{}) (string, error) {
244244
}
245245

246246
// NewClient returns a new GitHub API client. If a nil httpClient is
247-
// provided, http.DefaultClient will be used. To use API methods which require
247+
// provided, a new http.Client will be used. To use API methods which require
248248
// authentication, provide an http.Client that will perform the authentication
249249
// for you (such as that provided by the golang.org/x/oauth2 library).
250250
func NewClient(httpClient *http.Client) *Client {
251251
if httpClient == nil {
252-
httpClient = http.DefaultClient
252+
httpClient = &http.Client{}
253253
}
254254
baseURL, _ := url.Parse(defaultBaseURL)
255255
uploadURL, _ := url.Parse(uploadBaseURL)
@@ -283,7 +283,7 @@ func NewClient(httpClient *http.Client) *Client {
283283
// NewEnterpriseClient returns a new GitHub API client with provided
284284
// base URL and upload URL (often the same URL).
285285
// If either URL does not have a trailing slash, one is added automatically.
286-
// If a nil httpClient is provided, http.DefaultClient will be used.
286+
// If a nil httpClient is provided, a new http.Client will be used.
287287
//
288288
// Note that NewEnterpriseClient is a convenience helper only;
289289
// its behavior is equivalent to using NewClient, followed by setting

github/github_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ func TestNewClient(t *testing.T) {
172172
if got, want := c.UserAgent, userAgent; got != want {
173173
t.Errorf("NewClient UserAgent is %v, want %v", got, want)
174174
}
175+
176+
c2 := NewClient(nil)
177+
if c.client == c2.client {
178+
t.Error("NewClient returned same http.Clients, but they should differ")
179+
}
175180
}
176181

177182
func TestNewEnterpriseClient(t *testing.T) {

0 commit comments

Comments
 (0)