@@ -321,26 +321,45 @@ func TestClient(t *testing.T) {
321321
322322func 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
346365func TestWithEnterpriseURLs (t * testing.T ) {
0 commit comments