Skip to content

Commit 6d3dfc6

Browse files
authored
Don't update httpClient passed to NewClient (#3011)
1 parent ee55955 commit 6d3dfc6

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

github/github.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ type service struct {
220220
}
221221

222222
// Client returns the http.Client used by this GitHub client.
223+
// This should only be used for requests to the GitHub API because
224+
// request headers will contain an authorization token.
223225
func (c *Client) Client() *http.Client {
224226
c.clientMu.Lock()
225227
defer c.clientMu.Unlock()
@@ -315,7 +317,11 @@ func addOptions(s string, opts interface{}) (string, error) {
315317
// an http.Client that will perform the authentication for you (such as that
316318
// provided by the golang.org/x/oauth2 library).
317319
func NewClient(httpClient *http.Client) *Client {
318-
c := &Client{client: httpClient}
320+
if httpClient == nil {
321+
httpClient = &http.Client{}
322+
}
323+
httpClient2 := *httpClient
324+
c := &Client{client: &httpClient2}
319325
c.initialize()
320326
return c
321327
}

github/github_test.go

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -321,26 +321,45 @@ func TestClient(t *testing.T) {
321321

322322
func TestWithAuthToken(t *testing.T) {
323323
token := "gh_test_token"
324-
var gotAuthHeaderVals []string
325-
wantAuthHeaderVals := []string{"Bearer " + token}
326-
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
327-
gotAuthHeaderVals = r.Header["Authorization"]
328-
}))
329-
validate := func(c *Client) {
324+
325+
validate := func(t *testing.T, c *http.Client, token string) {
330326
t.Helper()
331-
gotAuthHeaderVals = nil
332-
_, err := c.Client().Get(srv.URL)
333-
if err != nil {
334-
t.Fatalf("Get returned unexpected error: %v", err)
327+
want := token
328+
if want != "" {
329+
want = "Bearer " + want
330+
}
331+
gotReq := false
332+
headerVal := ""
333+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
334+
gotReq = true
335+
headerVal = r.Header.Get("Authorization")
336+
}))
337+
_, err := c.Get(srv.URL)
338+
assertNilError(t, err)
339+
if !gotReq {
340+
t.Error("request not sent")
335341
}
336-
diff := cmp.Diff(wantAuthHeaderVals, gotAuthHeaderVals)
337-
if diff != "" {
338-
t.Errorf("Authorization header values mismatch (-want +got):\n%s", diff)
342+
if headerVal != want {
343+
t.Errorf("Authorization header is %v, want %v", headerVal, want)
339344
}
340345
}
341-
validate(NewClient(nil).WithAuthToken(token))
342-
validate(new(Client).WithAuthToken(token))
343-
validate(NewTokenClient(context.Background(), token))
346+
347+
t.Run("zero-value Client", func(t *testing.T) {
348+
c := new(Client).WithAuthToken(token)
349+
validate(t, c.Client(), token)
350+
})
351+
352+
t.Run("NewClient", func(t *testing.T) {
353+
httpClient := &http.Client{}
354+
client := NewClient(httpClient).WithAuthToken(token)
355+
validate(t, client.Client(), token)
356+
// make sure the original client isn't setting auth headers now
357+
validate(t, httpClient, "")
358+
})
359+
360+
t.Run("NewTokenClient", func(t *testing.T) {
361+
validate(t, NewTokenClient(context.Background(), token).Client(), token)
362+
})
344363
}
345364

346365
func TestWithEnterpriseURLs(t *testing.T) {

0 commit comments

Comments
 (0)