github.com/google/go-github/v64@v64.0.0/github/github_test.go (about)

     1  // Copyright 2013 The go-github AUTHORS. All rights reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package github
     7  
     8  import (
     9  	"context"
    10  	"encoding/json"
    11  	"errors"
    12  	"fmt"
    13  	"io"
    14  	"net/http"
    15  	"net/http/httptest"
    16  	"net/url"
    17  	"os"
    18  	"path"
    19  	"reflect"
    20  	"strconv"
    21  	"strings"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/google/go-cmp/cmp"
    26  )
    27  
    28  const (
    29  	// baseURLPath is a non-empty Client.BaseURL path to use during tests,
    30  	// to ensure relative URLs are used for all endpoints. See issue #752.
    31  	baseURLPath = "/api-v3"
    32  )
    33  
    34  // setup sets up a test HTTP server along with a github.Client that is
    35  // configured to talk to that test server. Tests should register handlers on
    36  // mux which provide mock responses for the API method being tested.
    37  func setup() (client *Client, mux *http.ServeMux, serverURL string, teardown func()) {
    38  	// mux is the HTTP request multiplexer used with the test server.
    39  	mux = http.NewServeMux()
    40  
    41  	// We want to ensure that tests catch mistakes where the endpoint URL is
    42  	// specified as absolute rather than relative. It only makes a difference
    43  	// when there's a non-empty base URL path. So, use that. See issue #752.
    44  	apiHandler := http.NewServeMux()
    45  	apiHandler.Handle(baseURLPath+"/", http.StripPrefix(baseURLPath, mux))
    46  	apiHandler.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
    47  		fmt.Fprintln(os.Stderr, "FAIL: Client.BaseURL path prefix is not preserved in the request URL:")
    48  		fmt.Fprintln(os.Stderr)
    49  		fmt.Fprintln(os.Stderr, "\t"+req.URL.String())
    50  		fmt.Fprintln(os.Stderr)
    51  		fmt.Fprintln(os.Stderr, "\tDid you accidentally use an absolute endpoint URL rather than relative?")
    52  		fmt.Fprintln(os.Stderr, "\tSee https://github.com/google/go-github/issues/752 for information.")
    53  		http.Error(w, "Client.BaseURL path prefix is not preserved in the request URL.", http.StatusInternalServerError)
    54  	})
    55  
    56  	// server is a test HTTP server used to provide mock API responses.
    57  	server := httptest.NewServer(apiHandler)
    58  
    59  	// client is the GitHub client being tested and is
    60  	// configured to use test server.
    61  	client = NewClient(nil)
    62  	url, _ := url.Parse(server.URL + baseURLPath + "/")
    63  	client.BaseURL = url
    64  	client.UploadURL = url
    65  
    66  	return client, mux, server.URL, server.Close
    67  }
    68  
    69  // openTestFile creates a new file with the given name and content for testing.
    70  // In order to ensure the exact file name, this function will create a new temp
    71  // directory, and create the file in that directory. It is the caller's
    72  // responsibility to remove the directory and its contents when no longer needed.
    73  func openTestFile(name, content string) (file *os.File, dir string, err error) {
    74  	dir, err = os.MkdirTemp("", "go-github")
    75  	if err != nil {
    76  		return nil, dir, err
    77  	}
    78  
    79  	file, err = os.OpenFile(path.Join(dir, name), os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
    80  	if err != nil {
    81  		return nil, dir, err
    82  	}
    83  
    84  	fmt.Fprint(file, content)
    85  
    86  	// close and re-open the file to keep file.Stat() happy
    87  	file.Close()
    88  	file, err = os.Open(file.Name())
    89  	if err != nil {
    90  		return nil, dir, err
    91  	}
    92  
    93  	return file, dir, err
    94  }
    95  
    96  func testMethod(t *testing.T, r *http.Request, want string) {
    97  	t.Helper()
    98  	if got := r.Method; got != want {
    99  		t.Errorf("Request method: %v, want %v", got, want)
   100  	}
   101  }
   102  
   103  type values map[string]string
   104  
   105  func testFormValues(t *testing.T, r *http.Request, values values) {
   106  	t.Helper()
   107  	want := url.Values{}
   108  	for k, v := range values {
   109  		want.Set(k, v)
   110  	}
   111  
   112  	assertNilError(t, r.ParseForm())
   113  	if got := r.Form; !cmp.Equal(got, want) {
   114  		t.Errorf("Request parameters: %v, want %v", got, want)
   115  	}
   116  }
   117  
   118  func testHeader(t *testing.T, r *http.Request, header string, want string) {
   119  	t.Helper()
   120  	if got := r.Header.Get(header); got != want {
   121  		t.Errorf("Header.Get(%q) returned %q, want %q", header, got, want)
   122  	}
   123  }
   124  
   125  func testURLParseError(t *testing.T, err error) {
   126  	t.Helper()
   127  	if err == nil {
   128  		t.Errorf("Expected error to be returned")
   129  	}
   130  	if err, ok := err.(*url.Error); !ok || err.Op != "parse" {
   131  		t.Errorf("Expected URL parse error, got %+v", err)
   132  	}
   133  }
   134  
   135  func testBody(t *testing.T, r *http.Request, want string) {
   136  	t.Helper()
   137  	b, err := io.ReadAll(r.Body)
   138  	if err != nil {
   139  		t.Errorf("Error reading request body: %v", err)
   140  	}
   141  	if got := string(b); got != want {
   142  		t.Errorf("request Body is %s, want %s", got, want)
   143  	}
   144  }
   145  
   146  // Test whether the marshaling of v produces JSON that corresponds
   147  // to the want string.
   148  func testJSONMarshal(t *testing.T, v interface{}, want string) {
   149  	t.Helper()
   150  	// Unmarshal the wanted JSON, to verify its correctness, and marshal it back
   151  	// to sort the keys.
   152  	u := reflect.New(reflect.TypeOf(v)).Interface()
   153  	if err := json.Unmarshal([]byte(want), &u); err != nil {
   154  		t.Errorf("Unable to unmarshal JSON for %v: %v", want, err)
   155  	}
   156  	w, err := json.MarshalIndent(u, "", "  ")
   157  	if err != nil {
   158  		t.Errorf("Unable to marshal JSON for %#v", u)
   159  	}
   160  
   161  	// Marshal the target value.
   162  	got, err := json.MarshalIndent(v, "", "  ")
   163  	if err != nil {
   164  		t.Errorf("Unable to marshal JSON for %#v", v)
   165  	}
   166  
   167  	if diff := cmp.Diff(string(w), string(got)); diff != "" {
   168  		t.Errorf("json.Marshal returned:\n%s\nwant:\n%s\ndiff:\n%v", got, w, diff)
   169  	}
   170  }
   171  
   172  // Test whether the v fields have the url tag and the parsing of v
   173  // produces query parameters that corresponds to the want string.
   174  func testAddURLOptions(t *testing.T, url string, v interface{}, want string) {
   175  	t.Helper()
   176  
   177  	vt := reflect.Indirect(reflect.ValueOf(v)).Type()
   178  	for i := 0; i < vt.NumField(); i++ {
   179  		field := vt.Field(i)
   180  		if alias, ok := field.Tag.Lookup("url"); ok {
   181  			if alias == "" {
   182  				t.Errorf("The field %+v has a blank url tag", field)
   183  			}
   184  		} else {
   185  			t.Errorf("The field %+v has no url tag specified", field)
   186  		}
   187  	}
   188  
   189  	got, err := addOptions(url, v)
   190  	if err != nil {
   191  		t.Errorf("Unable to add %#v as query parameters", v)
   192  	}
   193  
   194  	if got != want {
   195  		t.Errorf("addOptions(%q, %#v) returned %v, want %v", url, v, got, want)
   196  	}
   197  }
   198  
   199  // Test how bad options are handled. Method f under test should
   200  // return an error.
   201  func testBadOptions(t *testing.T, methodName string, f func() error) {
   202  	t.Helper()
   203  	if methodName == "" {
   204  		t.Error("testBadOptions: must supply method methodName")
   205  	}
   206  	if err := f(); err == nil {
   207  		t.Errorf("bad options %v err = nil, want error", methodName)
   208  	}
   209  }
   210  
   211  // Test function under NewRequest failure and then s.client.Do failure.
   212  // Method f should be a regular call that would normally succeed, but
   213  // should return an error when NewRequest or s.client.Do fails.
   214  func testNewRequestAndDoFailure(t *testing.T, methodName string, client *Client, f func() (*Response, error)) {
   215  	testNewRequestAndDoFailureCategory(t, methodName, client, CoreCategory, f)
   216  }
   217  
   218  // testNewRequestAndDoFailureCategory works Like testNewRequestAndDoFailure, but allows setting the category
   219  func testNewRequestAndDoFailureCategory(t *testing.T, methodName string, client *Client, category RateLimitCategory, f func() (*Response, error)) {
   220  	t.Helper()
   221  	if methodName == "" {
   222  		t.Error("testNewRequestAndDoFailure: must supply method methodName")
   223  	}
   224  
   225  	client.BaseURL.Path = ""
   226  	resp, err := f()
   227  	if resp != nil {
   228  		t.Errorf("client.BaseURL.Path='' %v resp = %#v, want nil", methodName, resp)
   229  	}
   230  	if err == nil {
   231  		t.Errorf("client.BaseURL.Path='' %v err = nil, want error", methodName)
   232  	}
   233  
   234  	client.BaseURL.Path = "/api-v3/"
   235  	client.rateLimits[category].Reset.Time = time.Now().Add(10 * time.Minute)
   236  	resp, err = f()
   237  	if bypass := resp.Request.Context().Value(bypassRateLimitCheck); bypass != nil {
   238  		return
   239  	}
   240  	if want := http.StatusForbidden; resp == nil || resp.Response.StatusCode != want {
   241  		if resp != nil {
   242  			t.Errorf("rate.Reset.Time > now %v resp = %#v, want StatusCode=%v", methodName, resp.Response, want)
   243  		} else {
   244  			t.Errorf("rate.Reset.Time > now %v resp = nil, want StatusCode=%v", methodName, want)
   245  		}
   246  	}
   247  	if err == nil {
   248  		t.Errorf("rate.Reset.Time > now %v err = nil, want error", methodName)
   249  	}
   250  }
   251  
   252  // Test that all error response types contain the status code.
   253  func testErrorResponseForStatusCode(t *testing.T, code int) {
   254  	t.Helper()
   255  	client, mux, _, teardown := setup()
   256  	defer teardown()
   257  
   258  	mux.HandleFunc("/repos/o/r/hooks", func(w http.ResponseWriter, r *http.Request) {
   259  		testMethod(t, r, "GET")
   260  		w.WriteHeader(code)
   261  	})
   262  
   263  	ctx := context.Background()
   264  	_, _, err := client.Repositories.ListHooks(ctx, "o", "r", nil)
   265  
   266  	switch e := err.(type) {
   267  	case *ErrorResponse:
   268  	case *RateLimitError:
   269  	case *AbuseRateLimitError:
   270  		if code != e.Response.StatusCode {
   271  			t.Error("Error response does not contain status code")
   272  		}
   273  	default:
   274  		t.Error("Unknown error response type")
   275  	}
   276  }
   277  
   278  func assertNoDiff(t *testing.T, want, got interface{}) {
   279  	t.Helper()
   280  	if diff := cmp.Diff(want, got); diff != "" {
   281  		t.Errorf("diff mismatch (-want +got):\n%v", diff)
   282  	}
   283  }
   284  
   285  func assertNilError(t *testing.T, err error) {
   286  	t.Helper()
   287  	if err != nil {
   288  		t.Errorf("unexpected error: %v", err)
   289  	}
   290  }
   291  
   292  func assertWrite(t *testing.T, w io.Writer, data []byte) {
   293  	t.Helper()
   294  	_, err := w.Write(data)
   295  	assertNilError(t, err)
   296  }
   297  
   298  func TestNewClient(t *testing.T) {
   299  	c := NewClient(nil)
   300  
   301  	if got, want := c.BaseURL.String(), defaultBaseURL; got != want {
   302  		t.Errorf("NewClient BaseURL is %v, want %v", got, want)
   303  	}
   304  	if got, want := c.UserAgent, defaultUserAgent; got != want {
   305  		t.Errorf("NewClient UserAgent is %v, want %v", got, want)
   306  	}
   307  
   308  	c2 := NewClient(nil)
   309  	if c.client == c2.client {
   310  		t.Error("NewClient returned same http.Clients, but they should differ")
   311  	}
   312  }
   313  
   314  func TestNewClientWithEnvProxy(t *testing.T) {
   315  	client := NewClientWithEnvProxy()
   316  	if got, want := client.BaseURL.String(), defaultBaseURL; got != want {
   317  		t.Errorf("NewClient BaseURL is %v, want %v", got, want)
   318  	}
   319  }
   320  
   321  func TestClient(t *testing.T) {
   322  	c := NewClient(nil)
   323  	c2 := c.Client()
   324  	if c.client == c2 {
   325  		t.Error("Client returned same http.Client, but should be different")
   326  	}
   327  }
   328  
   329  func TestWithAuthToken(t *testing.T) {
   330  	token := "gh_test_token"
   331  
   332  	validate := func(t *testing.T, c *http.Client, token string) {
   333  		t.Helper()
   334  		want := token
   335  		if want != "" {
   336  			want = "Bearer " + want
   337  		}
   338  		gotReq := false
   339  		headerVal := ""
   340  		srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   341  			gotReq = true
   342  			headerVal = r.Header.Get("Authorization")
   343  		}))
   344  		_, err := c.Get(srv.URL)
   345  		assertNilError(t, err)
   346  		if !gotReq {
   347  			t.Error("request not sent")
   348  		}
   349  		if headerVal != want {
   350  			t.Errorf("Authorization header is %v, want %v", headerVal, want)
   351  		}
   352  	}
   353  
   354  	t.Run("zero-value Client", func(t *testing.T) {
   355  		c := new(Client).WithAuthToken(token)
   356  		validate(t, c.Client(), token)
   357  	})
   358  
   359  	t.Run("NewClient", func(t *testing.T) {
   360  		httpClient := &http.Client{}
   361  		client := NewClient(httpClient).WithAuthToken(token)
   362  		validate(t, client.Client(), token)
   363  		// make sure the original client isn't setting auth headers now
   364  		validate(t, httpClient, "")
   365  	})
   366  
   367  	t.Run("NewTokenClient", func(t *testing.T) {
   368  		validate(t, NewTokenClient(context.Background(), token).Client(), token)
   369  	})
   370  }
   371  
   372  func TestWithEnterpriseURLs(t *testing.T) {
   373  	for _, test := range []struct {
   374  		name          string
   375  		baseURL       string
   376  		wantBaseURL   string
   377  		uploadURL     string
   378  		wantUploadURL string
   379  		wantErr       string
   380  	}{
   381  		{
   382  			name:          "does not modify properly formed URLs",
   383  			baseURL:       "https://custom-url/api/v3/",
   384  			wantBaseURL:   "https://custom-url/api/v3/",
   385  			uploadURL:     "https://custom-upload-url/api/uploads/",
   386  			wantUploadURL: "https://custom-upload-url/api/uploads/",
   387  		},
   388  		{
   389  			name:          "adds trailing slash",
   390  			baseURL:       "https://custom-url/api/v3",
   391  			wantBaseURL:   "https://custom-url/api/v3/",
   392  			uploadURL:     "https://custom-upload-url/api/uploads",
   393  			wantUploadURL: "https://custom-upload-url/api/uploads/",
   394  		},
   395  		{
   396  			name:          "adds enterprise suffix",
   397  			baseURL:       "https://custom-url/",
   398  			wantBaseURL:   "https://custom-url/api/v3/",
   399  			uploadURL:     "https://custom-upload-url/",
   400  			wantUploadURL: "https://custom-upload-url/api/uploads/",
   401  		},
   402  		{
   403  			name:          "adds enterprise suffix and trailing slash",
   404  			baseURL:       "https://custom-url",
   405  			wantBaseURL:   "https://custom-url/api/v3/",
   406  			uploadURL:     "https://custom-upload-url",
   407  			wantUploadURL: "https://custom-upload-url/api/uploads/",
   408  		},
   409  		{
   410  			name:      "bad base URL",
   411  			baseURL:   "bogus\nbase\nURL",
   412  			uploadURL: "https://custom-upload-url/api/uploads/",
   413  			wantErr:   `invalid control character in URL`,
   414  		},
   415  		{
   416  			name:      "bad upload URL",
   417  			baseURL:   "https://custom-url/api/v3/",
   418  			uploadURL: "bogus\nupload\nURL",
   419  			wantErr:   `invalid control character in URL`,
   420  		},
   421  		{
   422  			name:          "URL has existing API prefix, adds trailing slash",
   423  			baseURL:       "https://api.custom-url",
   424  			wantBaseURL:   "https://api.custom-url/",
   425  			uploadURL:     "https://api.custom-upload-url",
   426  			wantUploadURL: "https://api.custom-upload-url/",
   427  		},
   428  		{
   429  			name:          "URL has existing API prefix and trailing slash",
   430  			baseURL:       "https://api.custom-url/",
   431  			wantBaseURL:   "https://api.custom-url/",
   432  			uploadURL:     "https://api.custom-upload-url/",
   433  			wantUploadURL: "https://api.custom-upload-url/",
   434  		},
   435  		{
   436  			name:          "URL has API subdomain, adds trailing slash",
   437  			baseURL:       "https://catalog.api.custom-url",
   438  			wantBaseURL:   "https://catalog.api.custom-url/",
   439  			uploadURL:     "https://catalog.api.custom-upload-url",
   440  			wantUploadURL: "https://catalog.api.custom-upload-url/",
   441  		},
   442  		{
   443  			name:          "URL has API subdomain and trailing slash",
   444  			baseURL:       "https://catalog.api.custom-url/",
   445  			wantBaseURL:   "https://catalog.api.custom-url/",
   446  			uploadURL:     "https://catalog.api.custom-upload-url/",
   447  			wantUploadURL: "https://catalog.api.custom-upload-url/",
   448  		},
   449  		{
   450  			name:          "URL is not a proper API subdomain, adds enterprise suffix and slash",
   451  			baseURL:       "https://cloud-api.custom-url",
   452  			wantBaseURL:   "https://cloud-api.custom-url/api/v3/",
   453  			uploadURL:     "https://cloud-api.custom-upload-url",
   454  			wantUploadURL: "https://cloud-api.custom-upload-url/api/uploads/",
   455  		},
   456  		{
   457  			name:          "URL is not a proper API subdomain, adds enterprise suffix",
   458  			baseURL:       "https://cloud-api.custom-url/",
   459  			wantBaseURL:   "https://cloud-api.custom-url/api/v3/",
   460  			uploadURL:     "https://cloud-api.custom-upload-url/",
   461  			wantUploadURL: "https://cloud-api.custom-upload-url/api/uploads/",
   462  		},
   463  	} {
   464  		t.Run(test.name, func(t *testing.T) {
   465  			validate := func(c *Client, err error) {
   466  				t.Helper()
   467  				if test.wantErr != "" {
   468  					if err == nil || !strings.Contains(err.Error(), test.wantErr) {
   469  						t.Fatalf("error does not contain expected string %q: %v", test.wantErr, err)
   470  					}
   471  					return
   472  				}
   473  				if err != nil {
   474  					t.Fatalf("got unexpected error: %v", err)
   475  				}
   476  				if c.BaseURL.String() != test.wantBaseURL {
   477  					t.Errorf("BaseURL is %v, want %v", c.BaseURL.String(), test.wantBaseURL)
   478  				}
   479  				if c.UploadURL.String() != test.wantUploadURL {
   480  					t.Errorf("UploadURL is %v, want %v", c.UploadURL.String(), test.wantUploadURL)
   481  				}
   482  			}
   483  			validate(NewClient(nil).WithEnterpriseURLs(test.baseURL, test.uploadURL))
   484  			validate(new(Client).WithEnterpriseURLs(test.baseURL, test.uploadURL))
   485  			validate(NewEnterpriseClient(test.baseURL, test.uploadURL, nil))
   486  		})
   487  	}
   488  }
   489  
   490  // Ensure that length of Client.rateLimits is the same as number of fields in RateLimits struct.
   491  func TestClient_rateLimits(t *testing.T) {
   492  	if got, want := len(Client{}.rateLimits), reflect.TypeOf(RateLimits{}).NumField(); got != want {
   493  		t.Errorf("len(Client{}.rateLimits) is %v, want %v", got, want)
   494  	}
   495  }
   496  
   497  func TestNewRequest(t *testing.T) {
   498  	c := NewClient(nil)
   499  
   500  	inURL, outURL := "/foo", defaultBaseURL+"foo"
   501  	inBody, outBody := &User{Login: String("l")}, `{"login":"l"}`+"\n"
   502  	req, _ := c.NewRequest("GET", inURL, inBody)
   503  
   504  	// test that relative URL was expanded
   505  	if got, want := req.URL.String(), outURL; got != want {
   506  		t.Errorf("NewRequest(%q) URL is %v, want %v", inURL, got, want)
   507  	}
   508  
   509  	// test that body was JSON encoded
   510  	body, _ := io.ReadAll(req.Body)
   511  	if got, want := string(body), outBody; got != want {
   512  		t.Errorf("NewRequest(%q) Body is %v, want %v", inBody, got, want)
   513  	}
   514  
   515  	userAgent := req.Header.Get("User-Agent")
   516  
   517  	// test that default user-agent is attached to the request
   518  	if got, want := userAgent, c.UserAgent; got != want {
   519  		t.Errorf("NewRequest() User-Agent is %v, want %v", got, want)
   520  	}
   521  
   522  	if !strings.Contains(userAgent, Version) {
   523  		t.Errorf("NewRequest() User-Agent should contain %v, found %v", Version, userAgent)
   524  	}
   525  
   526  	apiVersion := req.Header.Get(headerAPIVersion)
   527  	if got, want := apiVersion, defaultAPIVersion; got != want {
   528  		t.Errorf("NewRequest() %v header is %v, want %v", headerAPIVersion, got, want)
   529  	}
   530  
   531  	req, _ = c.NewRequest("GET", inURL, inBody, WithVersion("2022-11-29"))
   532  	apiVersion = req.Header.Get(headerAPIVersion)
   533  	if got, want := apiVersion, "2022-11-29"; got != want {
   534  		t.Errorf("NewRequest() %v header is %v, want %v", headerAPIVersion, got, want)
   535  	}
   536  }
   537  
   538  func TestNewRequest_invalidJSON(t *testing.T) {
   539  	c := NewClient(nil)
   540  
   541  	type T struct {
   542  		A map[interface{}]interface{}
   543  	}
   544  	_, err := c.NewRequest("GET", ".", &T{})
   545  
   546  	if err == nil {
   547  		t.Error("Expected error to be returned.")
   548  	}
   549  	if err, ok := err.(*json.UnsupportedTypeError); !ok {
   550  		t.Errorf("Expected a JSON error; got %#v.", err)
   551  	}
   552  }
   553  
   554  func TestNewRequest_badURL(t *testing.T) {
   555  	c := NewClient(nil)
   556  	_, err := c.NewRequest("GET", ":", nil)
   557  	testURLParseError(t, err)
   558  }
   559  
   560  func TestNewRequest_badMethod(t *testing.T) {
   561  	c := NewClient(nil)
   562  	if _, err := c.NewRequest("BOGUS\nMETHOD", ".", nil); err == nil {
   563  		t.Fatal("NewRequest returned nil; expected error")
   564  	}
   565  }
   566  
   567  // ensure that no User-Agent header is set if the client's UserAgent is empty.
   568  // This caused a problem with Google's internal http client.
   569  func TestNewRequest_emptyUserAgent(t *testing.T) {
   570  	c := NewClient(nil)
   571  	c.UserAgent = ""
   572  	req, err := c.NewRequest("GET", ".", nil)
   573  	if err != nil {
   574  		t.Fatalf("NewRequest returned unexpected error: %v", err)
   575  	}
   576  	if _, ok := req.Header["User-Agent"]; ok {
   577  		t.Fatal("constructed request contains unexpected User-Agent header")
   578  	}
   579  }
   580  
   581  // If a nil body is passed to github.NewRequest, make sure that nil is also
   582  // passed to http.NewRequest. In most cases, passing an io.Reader that returns
   583  // no content is fine, since there is no difference between an HTTP request
   584  // body that is an empty string versus one that is not set at all. However in
   585  // certain cases, intermediate systems may treat these differently resulting in
   586  // subtle errors.
   587  func TestNewRequest_emptyBody(t *testing.T) {
   588  	c := NewClient(nil)
   589  	req, err := c.NewRequest("GET", ".", nil)
   590  	if err != nil {
   591  		t.Fatalf("NewRequest returned unexpected error: %v", err)
   592  	}
   593  	if req.Body != nil {
   594  		t.Fatalf("constructed request contains a non-nil Body")
   595  	}
   596  }
   597  
   598  func TestNewRequest_errorForNoTrailingSlash(t *testing.T) {
   599  	tests := []struct {
   600  		rawurl    string
   601  		wantError bool
   602  	}{
   603  		{rawurl: "https://example.com/api/v3", wantError: true},
   604  		{rawurl: "https://example.com/api/v3/", wantError: false},
   605  	}
   606  	c := NewClient(nil)
   607  	for _, test := range tests {
   608  		u, err := url.Parse(test.rawurl)
   609  		if err != nil {
   610  			t.Fatalf("url.Parse returned unexpected error: %v.", err)
   611  		}
   612  		c.BaseURL = u
   613  		if _, err := c.NewRequest(http.MethodGet, "test", nil); test.wantError && err == nil {
   614  			t.Fatalf("Expected error to be returned.")
   615  		} else if !test.wantError && err != nil {
   616  			t.Fatalf("NewRequest returned unexpected error: %v.", err)
   617  		}
   618  	}
   619  }
   620  
   621  func TestNewFormRequest(t *testing.T) {
   622  	c := NewClient(nil)
   623  
   624  	inURL, outURL := "/foo", defaultBaseURL+"foo"
   625  	form := url.Values{}
   626  	form.Add("login", "l")
   627  	inBody, outBody := strings.NewReader(form.Encode()), "login=l"
   628  	req, _ := c.NewFormRequest(inURL, inBody)
   629  
   630  	// test that relative URL was expanded
   631  	if got, want := req.URL.String(), outURL; got != want {
   632  		t.Errorf("NewFormRequest(%q) URL is %v, want %v", inURL, got, want)
   633  	}
   634  
   635  	// test that body was form encoded
   636  	body, _ := io.ReadAll(req.Body)
   637  	if got, want := string(body), outBody; got != want {
   638  		t.Errorf("NewFormRequest(%q) Body is %v, want %v", inBody, got, want)
   639  	}
   640  
   641  	// test that default user-agent is attached to the request
   642  	if got, want := req.Header.Get("User-Agent"), c.UserAgent; got != want {
   643  		t.Errorf("NewFormRequest() User-Agent is %v, want %v", got, want)
   644  	}
   645  
   646  	apiVersion := req.Header.Get(headerAPIVersion)
   647  	if got, want := apiVersion, defaultAPIVersion; got != want {
   648  		t.Errorf("NewRequest() %v header is %v, want %v", headerAPIVersion, got, want)
   649  	}
   650  
   651  	req, _ = c.NewFormRequest(inURL, inBody, WithVersion("2022-11-29"))
   652  	apiVersion = req.Header.Get(headerAPIVersion)
   653  	if got, want := apiVersion, "2022-11-29"; got != want {
   654  		t.Errorf("NewRequest() %v header is %v, want %v", headerAPIVersion, got, want)
   655  	}
   656  }
   657  
   658  func TestNewFormRequest_badURL(t *testing.T) {
   659  	c := NewClient(nil)
   660  	_, err := c.NewFormRequest(":", nil)
   661  	testURLParseError(t, err)
   662  }
   663  
   664  func TestNewFormRequest_emptyUserAgent(t *testing.T) {
   665  	c := NewClient(nil)
   666  	c.UserAgent = ""
   667  	req, err := c.NewFormRequest(".", nil)
   668  	if err != nil {
   669  		t.Fatalf("NewFormRequest returned unexpected error: %v", err)
   670  	}
   671  	if _, ok := req.Header["User-Agent"]; ok {
   672  		t.Fatal("constructed request contains unexpected User-Agent header")
   673  	}
   674  }
   675  
   676  func TestNewFormRequest_emptyBody(t *testing.T) {
   677  	c := NewClient(nil)
   678  	req, err := c.NewFormRequest(".", nil)
   679  	if err != nil {
   680  		t.Fatalf("NewFormRequest returned unexpected error: %v", err)
   681  	}
   682  	if req.Body != nil {
   683  		t.Fatalf("constructed request contains a non-nil Body")
   684  	}
   685  }
   686  
   687  func TestNewFormRequest_errorForNoTrailingSlash(t *testing.T) {
   688  	tests := []struct {
   689  		rawURL    string
   690  		wantError bool
   691  	}{
   692  		{rawURL: "https://example.com/api/v3", wantError: true},
   693  		{rawURL: "https://example.com/api/v3/", wantError: false},
   694  	}
   695  	c := NewClient(nil)
   696  	for _, test := range tests {
   697  		u, err := url.Parse(test.rawURL)
   698  		if err != nil {
   699  			t.Fatalf("url.Parse returned unexpected error: %v.", err)
   700  		}
   701  		c.BaseURL = u
   702  		if _, err := c.NewFormRequest("test", nil); test.wantError && err == nil {
   703  			t.Fatalf("Expected error to be returned.")
   704  		} else if !test.wantError && err != nil {
   705  			t.Fatalf("NewFormRequest returned unexpected error: %v.", err)
   706  		}
   707  	}
   708  }
   709  
   710  func TestNewUploadRequest_WithVersion(t *testing.T) {
   711  	c := NewClient(nil)
   712  	req, _ := c.NewUploadRequest("https://example.com/", nil, 0, "")
   713  
   714  	apiVersion := req.Header.Get(headerAPIVersion)
   715  	if got, want := apiVersion, defaultAPIVersion; got != want {
   716  		t.Errorf("NewRequest() %v header is %v, want %v", headerAPIVersion, got, want)
   717  	}
   718  
   719  	req, _ = c.NewUploadRequest("https://example.com/", nil, 0, "", WithVersion("2022-11-29"))
   720  	apiVersion = req.Header.Get(headerAPIVersion)
   721  	if got, want := apiVersion, "2022-11-29"; got != want {
   722  		t.Errorf("NewRequest() %v header is %v, want %v", headerAPIVersion, got, want)
   723  	}
   724  }
   725  
   726  func TestNewUploadRequest_badURL(t *testing.T) {
   727  	c := NewClient(nil)
   728  	_, err := c.NewUploadRequest(":", nil, 0, "")
   729  	testURLParseError(t, err)
   730  
   731  	const methodName = "NewUploadRequest"
   732  	testBadOptions(t, methodName, func() (err error) {
   733  		_, err = c.NewUploadRequest("\n", nil, -1, "\n")
   734  		return err
   735  	})
   736  }
   737  
   738  func TestNewUploadRequest_errorForNoTrailingSlash(t *testing.T) {
   739  	tests := []struct {
   740  		rawurl    string
   741  		wantError bool
   742  	}{
   743  		{rawurl: "https://example.com/api/uploads", wantError: true},
   744  		{rawurl: "https://example.com/api/uploads/", wantError: false},
   745  	}
   746  	c := NewClient(nil)
   747  	for _, test := range tests {
   748  		u, err := url.Parse(test.rawurl)
   749  		if err != nil {
   750  			t.Fatalf("url.Parse returned unexpected error: %v.", err)
   751  		}
   752  		c.UploadURL = u
   753  		if _, err = c.NewUploadRequest("test", nil, 0, ""); test.wantError && err == nil {
   754  			t.Fatalf("Expected error to be returned.")
   755  		} else if !test.wantError && err != nil {
   756  			t.Fatalf("NewUploadRequest returned unexpected error: %v.", err)
   757  		}
   758  	}
   759  }
   760  
   761  func TestResponse_populatePageValues(t *testing.T) {
   762  	r := http.Response{
   763  		Header: http.Header{
   764  			"Link": {`<https://api.github.com/?page=1>; rel="first",` +
   765  				` <https://api.github.com/?page=2>; rel="prev",` +
   766  				` <https://api.github.com/?page=4>; rel="next",` +
   767  				` <https://api.github.com/?page=5>; rel="last"`,
   768  			},
   769  		},
   770  	}
   771  
   772  	response := newResponse(&r)
   773  	if got, want := response.FirstPage, 1; got != want {
   774  		t.Errorf("response.FirstPage: %v, want %v", got, want)
   775  	}
   776  	if got, want := response.PrevPage, 2; want != got {
   777  		t.Errorf("response.PrevPage: %v, want %v", got, want)
   778  	}
   779  	if got, want := response.NextPage, 4; want != got {
   780  		t.Errorf("response.NextPage: %v, want %v", got, want)
   781  	}
   782  	if got, want := response.LastPage, 5; want != got {
   783  		t.Errorf("response.LastPage: %v, want %v", got, want)
   784  	}
   785  	if got, want := response.NextPageToken, ""; want != got {
   786  		t.Errorf("response.NextPageToken: %v, want %v", got, want)
   787  	}
   788  }
   789  
   790  func TestResponse_populateSinceValues(t *testing.T) {
   791  	r := http.Response{
   792  		Header: http.Header{
   793  			"Link": {`<https://api.github.com/?since=1>; rel="first",` +
   794  				` <https://api.github.com/?since=2>; rel="prev",` +
   795  				` <https://api.github.com/?since=4>; rel="next",` +
   796  				` <https://api.github.com/?since=5>; rel="last"`,
   797  			},
   798  		},
   799  	}
   800  
   801  	response := newResponse(&r)
   802  	if got, want := response.FirstPage, 1; got != want {
   803  		t.Errorf("response.FirstPage: %v, want %v", got, want)
   804  	}
   805  	if got, want := response.PrevPage, 2; want != got {
   806  		t.Errorf("response.PrevPage: %v, want %v", got, want)
   807  	}
   808  	if got, want := response.NextPage, 4; want != got {
   809  		t.Errorf("response.NextPage: %v, want %v", got, want)
   810  	}
   811  	if got, want := response.LastPage, 5; want != got {
   812  		t.Errorf("response.LastPage: %v, want %v", got, want)
   813  	}
   814  	if got, want := response.NextPageToken, ""; want != got {
   815  		t.Errorf("response.NextPageToken: %v, want %v", got, want)
   816  	}
   817  }
   818  
   819  func TestResponse_SinceWithPage(t *testing.T) {
   820  	r := http.Response{
   821  		Header: http.Header{
   822  			"Link": {`<https://api.github.com/?since=2021-12-04T10%3A43%3A42Z&page=1>; rel="first",` +
   823  				` <https://api.github.com/?since=2021-12-04T10%3A43%3A42Z&page=2>; rel="prev",` +
   824  				` <https://api.github.com/?since=2021-12-04T10%3A43%3A42Z&page=4>; rel="next",` +
   825  				` <https://api.github.com/?since=2021-12-04T10%3A43%3A42Z&page=5>; rel="last"`,
   826  			},
   827  		},
   828  	}
   829  
   830  	response := newResponse(&r)
   831  	if got, want := response.FirstPage, 1; got != want {
   832  		t.Errorf("response.FirstPage: %v, want %v", got, want)
   833  	}
   834  	if got, want := response.PrevPage, 2; want != got {
   835  		t.Errorf("response.PrevPage: %v, want %v", got, want)
   836  	}
   837  	if got, want := response.NextPage, 4; want != got {
   838  		t.Errorf("response.NextPage: %v, want %v", got, want)
   839  	}
   840  	if got, want := response.LastPage, 5; want != got {
   841  		t.Errorf("response.LastPage: %v, want %v", got, want)
   842  	}
   843  	if got, want := response.NextPageToken, ""; want != got {
   844  		t.Errorf("response.NextPageToken: %v, want %v", got, want)
   845  	}
   846  }
   847  
   848  func TestResponse_cursorPagination(t *testing.T) {
   849  	r := http.Response{
   850  		Header: http.Header{
   851  			"Status": {"200 OK"},
   852  			"Link":   {`<https://api.github.com/resource?per_page=2&page=url-encoded-next-page-token>; rel="next"`},
   853  		},
   854  	}
   855  
   856  	response := newResponse(&r)
   857  	if got, want := response.FirstPage, 0; got != want {
   858  		t.Errorf("response.FirstPage: %v, want %v", got, want)
   859  	}
   860  	if got, want := response.PrevPage, 0; want != got {
   861  		t.Errorf("response.PrevPage: %v, want %v", got, want)
   862  	}
   863  	if got, want := response.NextPage, 0; want != got {
   864  		t.Errorf("response.NextPage: %v, want %v", got, want)
   865  	}
   866  	if got, want := response.LastPage, 0; want != got {
   867  		t.Errorf("response.LastPage: %v, want %v", got, want)
   868  	}
   869  	if got, want := response.NextPageToken, "url-encoded-next-page-token"; want != got {
   870  		t.Errorf("response.NextPageToken: %v, want %v", got, want)
   871  	}
   872  
   873  	// cursor-based pagination with "cursor" param
   874  	r = http.Response{
   875  		Header: http.Header{
   876  			"Link": {
   877  				`<https://api.github.com/?cursor=v1_12345678>; rel="next"`,
   878  			},
   879  		},
   880  	}
   881  
   882  	response = newResponse(&r)
   883  	if got, want := response.Cursor, "v1_12345678"; got != want {
   884  		t.Errorf("response.Cursor: %v, want %v", got, want)
   885  	}
   886  }
   887  
   888  func TestResponse_beforeAfterPagination(t *testing.T) {
   889  	r := http.Response{
   890  		Header: http.Header{
   891  			"Link": {`<https://api.github.com/?after=a1b2c3&before=>; rel="next",` +
   892  				` <https://api.github.com/?after=&before=>; rel="first",` +
   893  				` <https://api.github.com/?after=&before=d4e5f6>; rel="prev",`,
   894  			},
   895  		},
   896  	}
   897  
   898  	response := newResponse(&r)
   899  	if got, want := response.Before, "d4e5f6"; got != want {
   900  		t.Errorf("response.Before: %v, want %v", got, want)
   901  	}
   902  	if got, want := response.After, "a1b2c3"; got != want {
   903  		t.Errorf("response.After: %v, want %v", got, want)
   904  	}
   905  	if got, want := response.FirstPage, 0; got != want {
   906  		t.Errorf("response.FirstPage: %v, want %v", got, want)
   907  	}
   908  	if got, want := response.PrevPage, 0; want != got {
   909  		t.Errorf("response.PrevPage: %v, want %v", got, want)
   910  	}
   911  	if got, want := response.NextPage, 0; want != got {
   912  		t.Errorf("response.NextPage: %v, want %v", got, want)
   913  	}
   914  	if got, want := response.LastPage, 0; want != got {
   915  		t.Errorf("response.LastPage: %v, want %v", got, want)
   916  	}
   917  	if got, want := response.NextPageToken, ""; want != got {
   918  		t.Errorf("response.NextPageToken: %v, want %v", got, want)
   919  	}
   920  }
   921  
   922  func TestResponse_populatePageValues_invalid(t *testing.T) {
   923  	r := http.Response{
   924  		Header: http.Header{
   925  			"Link": {`<https://api.github.com/?page=1>,` +
   926  				`<https://api.github.com/?page=abc>; rel="first",` +
   927  				`https://api.github.com/?page=2; rel="prev",` +
   928  				`<https://api.github.com/>; rel="next",` +
   929  				`<https://api.github.com/?page=>; rel="last"`,
   930  			},
   931  		},
   932  	}
   933  
   934  	response := newResponse(&r)
   935  	if got, want := response.FirstPage, 0; got != want {
   936  		t.Errorf("response.FirstPage: %v, want %v", got, want)
   937  	}
   938  	if got, want := response.PrevPage, 0; got != want {
   939  		t.Errorf("response.PrevPage: %v, want %v", got, want)
   940  	}
   941  	if got, want := response.NextPage, 0; got != want {
   942  		t.Errorf("response.NextPage: %v, want %v", got, want)
   943  	}
   944  	if got, want := response.LastPage, 0; got != want {
   945  		t.Errorf("response.LastPage: %v, want %v", got, want)
   946  	}
   947  
   948  	// more invalid URLs
   949  	r = http.Response{
   950  		Header: http.Header{
   951  			"Link": {`<https://api.github.com/%?page=2>; rel="first"`},
   952  		},
   953  	}
   954  
   955  	response = newResponse(&r)
   956  	if got, want := response.FirstPage, 0; got != want {
   957  		t.Errorf("response.FirstPage: %v, want %v", got, want)
   958  	}
   959  }
   960  
   961  func TestResponse_populateSinceValues_invalid(t *testing.T) {
   962  	r := http.Response{
   963  		Header: http.Header{
   964  			"Link": {`<https://api.github.com/?since=1>,` +
   965  				`<https://api.github.com/?since=abc>; rel="first",` +
   966  				`https://api.github.com/?since=2; rel="prev",` +
   967  				`<https://api.github.com/>; rel="next",` +
   968  				`<https://api.github.com/?since=>; rel="last"`,
   969  			},
   970  		},
   971  	}
   972  
   973  	response := newResponse(&r)
   974  	if got, want := response.FirstPage, 0; got != want {
   975  		t.Errorf("response.FirstPage: %v, want %v", got, want)
   976  	}
   977  	if got, want := response.PrevPage, 0; got != want {
   978  		t.Errorf("response.PrevPage: %v, want %v", got, want)
   979  	}
   980  	if got, want := response.NextPage, 0; got != want {
   981  		t.Errorf("response.NextPage: %v, want %v", got, want)
   982  	}
   983  	if got, want := response.LastPage, 0; got != want {
   984  		t.Errorf("response.LastPage: %v, want %v", got, want)
   985  	}
   986  
   987  	// more invalid URLs
   988  	r = http.Response{
   989  		Header: http.Header{
   990  			"Link": {`<https://api.github.com/%?since=2>; rel="first"`},
   991  		},
   992  	}
   993  
   994  	response = newResponse(&r)
   995  	if got, want := response.FirstPage, 0; got != want {
   996  		t.Errorf("response.FirstPage: %v, want %v", got, want)
   997  	}
   998  }
   999  
  1000  func TestDo(t *testing.T) {
  1001  	client, mux, _, teardown := setup()
  1002  	defer teardown()
  1003  
  1004  	type foo struct {
  1005  		A string
  1006  	}
  1007  
  1008  	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  1009  		testMethod(t, r, "GET")
  1010  		fmt.Fprint(w, `{"A":"a"}`)
  1011  	})
  1012  
  1013  	req, _ := client.NewRequest("GET", ".", nil)
  1014  	body := new(foo)
  1015  	ctx := context.Background()
  1016  	_, err := client.Do(ctx, req, body)
  1017  	assertNilError(t, err)
  1018  
  1019  	want := &foo{"a"}
  1020  	if !cmp.Equal(body, want) {
  1021  		t.Errorf("Response body = %v, want %v", body, want)
  1022  	}
  1023  }
  1024  
  1025  func TestDo_nilContext(t *testing.T) {
  1026  	client, _, _, teardown := setup()
  1027  	defer teardown()
  1028  
  1029  	req, _ := client.NewRequest("GET", ".", nil)
  1030  	_, err := client.Do(nil, req, nil)
  1031  
  1032  	if !errors.Is(err, errNonNilContext) {
  1033  		t.Errorf("Expected context must be non-nil error")
  1034  	}
  1035  }
  1036  
  1037  func TestDo_httpError(t *testing.T) {
  1038  	client, mux, _, teardown := setup()
  1039  	defer teardown()
  1040  
  1041  	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  1042  		http.Error(w, "Bad Request", 400)
  1043  	})
  1044  
  1045  	req, _ := client.NewRequest("GET", ".", nil)
  1046  	ctx := context.Background()
  1047  	resp, err := client.Do(ctx, req, nil)
  1048  
  1049  	if err == nil {
  1050  		t.Fatal("Expected HTTP 400 error, got no error.")
  1051  	}
  1052  	if resp.StatusCode != 400 {
  1053  		t.Errorf("Expected HTTP 400 error, got %d status code.", resp.StatusCode)
  1054  	}
  1055  }
  1056  
  1057  // Test handling of an error caused by the internal http client's Do()
  1058  // function. A redirect loop is pretty unlikely to occur within the GitHub
  1059  // API, but does allow us to exercise the right code path.
  1060  func TestDo_redirectLoop(t *testing.T) {
  1061  	client, mux, _, teardown := setup()
  1062  	defer teardown()
  1063  
  1064  	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  1065  		http.Redirect(w, r, baseURLPath, http.StatusFound)
  1066  	})
  1067  
  1068  	req, _ := client.NewRequest("GET", ".", nil)
  1069  	ctx := context.Background()
  1070  	_, err := client.Do(ctx, req, nil)
  1071  
  1072  	if err == nil {
  1073  		t.Error("Expected error to be returned.")
  1074  	}
  1075  	if err, ok := err.(*url.Error); !ok {
  1076  		t.Errorf("Expected a URL error; got %#v.", err)
  1077  	}
  1078  }
  1079  
  1080  // Test that an error caused by the internal http client's Do() function
  1081  // does not leak the client secret.
  1082  func TestDo_sanitizeURL(t *testing.T) {
  1083  	tp := &UnauthenticatedRateLimitedTransport{
  1084  		ClientID:     "id",
  1085  		ClientSecret: "secret",
  1086  	}
  1087  	unauthedClient := NewClient(tp.Client())
  1088  	unauthedClient.BaseURL = &url.URL{Scheme: "http", Host: "127.0.0.1:0", Path: "/"} // Use port 0 on purpose to trigger a dial TCP error, expect to get "dial tcp 127.0.0.1:0: connect: can't assign requested address".
  1089  	req, err := unauthedClient.NewRequest("GET", ".", nil)
  1090  	if err != nil {
  1091  		t.Fatalf("NewRequest returned unexpected error: %v", err)
  1092  	}
  1093  	ctx := context.Background()
  1094  	_, err = unauthedClient.Do(ctx, req, nil)
  1095  	if err == nil {
  1096  		t.Fatal("Expected error to be returned.")
  1097  	}
  1098  	if strings.Contains(err.Error(), "client_secret=secret") {
  1099  		t.Errorf("Do error contains secret, should be redacted:\n%q", err)
  1100  	}
  1101  }
  1102  
  1103  func TestDo_rateLimit(t *testing.T) {
  1104  	client, mux, _, teardown := setup()
  1105  	defer teardown()
  1106  
  1107  	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  1108  		w.Header().Set(headerRateLimit, "60")
  1109  		w.Header().Set(headerRateRemaining, "59")
  1110  		w.Header().Set(headerRateReset, "1372700873")
  1111  	})
  1112  
  1113  	req, _ := client.NewRequest("GET", ".", nil)
  1114  	ctx := context.Background()
  1115  	resp, err := client.Do(ctx, req, nil)
  1116  	if err != nil {
  1117  		t.Errorf("Do returned unexpected error: %v", err)
  1118  	}
  1119  	if got, want := resp.Rate.Limit, 60; got != want {
  1120  		t.Errorf("Client rate limit = %v, want %v", got, want)
  1121  	}
  1122  	if got, want := resp.Rate.Remaining, 59; got != want {
  1123  		t.Errorf("Client rate remaining = %v, want %v", got, want)
  1124  	}
  1125  	reset := time.Date(2013, time.July, 1, 17, 47, 53, 0, time.UTC)
  1126  	if resp.Rate.Reset.UTC() != reset {
  1127  		t.Errorf("Client rate reset = %v, want %v", resp.Rate.Reset, reset)
  1128  	}
  1129  }
  1130  
  1131  func TestDo_rateLimitCategory(t *testing.T) {
  1132  	tests := []struct {
  1133  		method   string
  1134  		url      string
  1135  		category RateLimitCategory
  1136  	}{
  1137  		{
  1138  			method:   http.MethodGet,
  1139  			url:      "/",
  1140  			category: CoreCategory,
  1141  		},
  1142  		{
  1143  			method:   http.MethodGet,
  1144  			url:      "/search/issues?q=rate",
  1145  			category: SearchCategory,
  1146  		},
  1147  		{
  1148  			method:   http.MethodGet,
  1149  			url:      "/graphql",
  1150  			category: GraphqlCategory,
  1151  		},
  1152  		{
  1153  			method:   http.MethodPost,
  1154  			url:      "/app-manifests/code/conversions",
  1155  			category: IntegrationManifestCategory,
  1156  		},
  1157  		{
  1158  			method:   http.MethodGet,
  1159  			url:      "/app-manifests/code/conversions",
  1160  			category: CoreCategory, // only POST requests are in the integration manifest category
  1161  		},
  1162  		{
  1163  			method:   http.MethodPut,
  1164  			url:      "/repos/google/go-github/import",
  1165  			category: SourceImportCategory,
  1166  		},
  1167  		{
  1168  			method:   http.MethodGet,
  1169  			url:      "/repos/google/go-github/import",
  1170  			category: CoreCategory, // only PUT requests are in the source import category
  1171  		},
  1172  		{
  1173  			method:   http.MethodPost,
  1174  			url:      "/repos/google/go-github/code-scanning/sarifs",
  1175  			category: CodeScanningUploadCategory,
  1176  		},
  1177  		{
  1178  			method:   http.MethodGet,
  1179  			url:      "/scim/v2/organizations/ORG/Users",
  1180  			category: ScimCategory,
  1181  		},
  1182  		{
  1183  			method:   http.MethodPost,
  1184  			url:      "/repos/google/go-github/dependency-graph/snapshots",
  1185  			category: DependencySnapshotsCategory,
  1186  		},
  1187  		{
  1188  			method:   http.MethodGet,
  1189  			url:      "/search/code?q=rate",
  1190  			category: CodeSearchCategory,
  1191  		},
  1192  		{
  1193  			method:   http.MethodGet,
  1194  			url:      "/orgs/google/audit-log",
  1195  			category: AuditLogCategory,
  1196  		},
  1197  		// missing a check for actionsRunnerRegistrationCategory: API not found
  1198  	}
  1199  
  1200  	for _, tt := range tests {
  1201  		if got, want := GetRateLimitCategory(tt.method, tt.url), tt.category; got != want {
  1202  			t.Errorf("expecting category %v, found %v", got, want)
  1203  		}
  1204  	}
  1205  }
  1206  
  1207  // ensure rate limit is still parsed, even for error responses
  1208  func TestDo_rateLimit_errorResponse(t *testing.T) {
  1209  	client, mux, _, teardown := setup()
  1210  	defer teardown()
  1211  
  1212  	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  1213  		w.Header().Set(headerRateLimit, "60")
  1214  		w.Header().Set(headerRateRemaining, "59")
  1215  		w.Header().Set(headerRateReset, "1372700873")
  1216  		http.Error(w, "Bad Request", 400)
  1217  	})
  1218  
  1219  	req, _ := client.NewRequest("GET", ".", nil)
  1220  	ctx := context.Background()
  1221  	resp, err := client.Do(ctx, req, nil)
  1222  	if err == nil {
  1223  		t.Error("Expected error to be returned.")
  1224  	}
  1225  	if _, ok := err.(*RateLimitError); ok {
  1226  		t.Errorf("Did not expect a *RateLimitError error; got %#v.", err)
  1227  	}
  1228  	if got, want := resp.Rate.Limit, 60; got != want {
  1229  		t.Errorf("Client rate limit = %v, want %v", got, want)
  1230  	}
  1231  	if got, want := resp.Rate.Remaining, 59; got != want {
  1232  		t.Errorf("Client rate remaining = %v, want %v", got, want)
  1233  	}
  1234  	reset := time.Date(2013, time.July, 1, 17, 47, 53, 0, time.UTC)
  1235  	if resp.Rate.Reset.UTC() != reset {
  1236  		t.Errorf("Client rate reset = %v, want %v", resp.Rate.Reset, reset)
  1237  	}
  1238  }
  1239  
  1240  // Ensure *RateLimitError is returned when API rate limit is exceeded.
  1241  func TestDo_rateLimit_rateLimitError(t *testing.T) {
  1242  	client, mux, _, teardown := setup()
  1243  	defer teardown()
  1244  
  1245  	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  1246  		w.Header().Set(headerRateLimit, "60")
  1247  		w.Header().Set(headerRateRemaining, "0")
  1248  		w.Header().Set(headerRateReset, "1372700873")
  1249  		w.Header().Set("Content-Type", "application/json; charset=utf-8")
  1250  		w.WriteHeader(http.StatusForbidden)
  1251  		fmt.Fprintln(w, `{
  1252     "message": "API rate limit exceeded for xxx.xxx.xxx.xxx. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)",
  1253     "documentation_url": "https://docs.github.com/en/rest/overview/resources-in-the-rest-api#abuse-rate-limits"
  1254  }`)
  1255  	})
  1256  
  1257  	req, _ := client.NewRequest("GET", ".", nil)
  1258  	ctx := context.Background()
  1259  	_, err := client.Do(ctx, req, nil)
  1260  
  1261  	if err == nil {
  1262  		t.Error("Expected error to be returned.")
  1263  	}
  1264  	rateLimitErr, ok := err.(*RateLimitError)
  1265  	if !ok {
  1266  		t.Fatalf("Expected a *RateLimitError error; got %#v.", err)
  1267  	}
  1268  	if got, want := rateLimitErr.Rate.Limit, 60; got != want {
  1269  		t.Errorf("rateLimitErr rate limit = %v, want %v", got, want)
  1270  	}
  1271  	if got, want := rateLimitErr.Rate.Remaining, 0; got != want {
  1272  		t.Errorf("rateLimitErr rate remaining = %v, want %v", got, want)
  1273  	}
  1274  	reset := time.Date(2013, time.July, 1, 17, 47, 53, 0, time.UTC)
  1275  	if rateLimitErr.Rate.Reset.UTC() != reset {
  1276  		t.Errorf("rateLimitErr rate reset = %v, want %v", rateLimitErr.Rate.Reset.UTC(), reset)
  1277  	}
  1278  }
  1279  
  1280  // Ensure a network call is not made when it's known that API rate limit is still exceeded.
  1281  func TestDo_rateLimit_noNetworkCall(t *testing.T) {
  1282  	client, mux, _, teardown := setup()
  1283  	defer teardown()
  1284  
  1285  	reset := time.Now().UTC().Add(time.Minute).Round(time.Second) // Rate reset is a minute from now, with 1 second precision.
  1286  
  1287  	mux.HandleFunc("/first", func(w http.ResponseWriter, r *http.Request) {
  1288  		w.Header().Set(headerRateLimit, "60")
  1289  		w.Header().Set(headerRateRemaining, "0")
  1290  		w.Header().Set(headerRateReset, fmt.Sprint(reset.Unix()))
  1291  		w.Header().Set("Content-Type", "application/json; charset=utf-8")
  1292  		w.WriteHeader(http.StatusForbidden)
  1293  		fmt.Fprintln(w, `{
  1294     "message": "API rate limit exceeded for xxx.xxx.xxx.xxx. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)",
  1295     "documentation_url": "https://docs.github.com/en/rest/overview/resources-in-the-rest-api#abuse-rate-limits"
  1296  }`)
  1297  	})
  1298  
  1299  	madeNetworkCall := false
  1300  	mux.HandleFunc("/second", func(w http.ResponseWriter, r *http.Request) {
  1301  		madeNetworkCall = true
  1302  	})
  1303  
  1304  	// First request is made, and it makes the client aware of rate reset time being in the future.
  1305  	req, _ := client.NewRequest("GET", "first", nil)
  1306  	ctx := context.Background()
  1307  	_, err := client.Do(ctx, req, nil)
  1308  	if err == nil {
  1309  		t.Error("Expected error to be returned.")
  1310  	}
  1311  
  1312  	// Second request should not cause a network call to be made, since client can predict a rate limit error.
  1313  	req, _ = client.NewRequest("GET", "second", nil)
  1314  	_, err = client.Do(ctx, req, nil)
  1315  
  1316  	if madeNetworkCall {
  1317  		t.Fatal("Network call was made, even though rate limit is known to still be exceeded.")
  1318  	}
  1319  
  1320  	if err == nil {
  1321  		t.Error("Expected error to be returned.")
  1322  	}
  1323  	rateLimitErr, ok := err.(*RateLimitError)
  1324  	if !ok {
  1325  		t.Fatalf("Expected a *RateLimitError error; got %#v.", err)
  1326  	}
  1327  	if got, want := rateLimitErr.Rate.Limit, 60; got != want {
  1328  		t.Errorf("rateLimitErr rate limit = %v, want %v", got, want)
  1329  	}
  1330  	if got, want := rateLimitErr.Rate.Remaining, 0; got != want {
  1331  		t.Errorf("rateLimitErr rate remaining = %v, want %v", got, want)
  1332  	}
  1333  	if rateLimitErr.Rate.Reset.UTC() != reset {
  1334  		t.Errorf("rateLimitErr rate reset = %v, want %v", rateLimitErr.Rate.Reset.UTC(), reset)
  1335  	}
  1336  }
  1337  
  1338  // Ignore rate limit headers if the response was served from cache.
  1339  func TestDo_rateLimit_ignoredFromCache(t *testing.T) {
  1340  	client, mux, _, teardown := setup()
  1341  	defer teardown()
  1342  
  1343  	reset := time.Now().UTC().Add(time.Minute).Round(time.Second) // Rate reset is a minute from now, with 1 second precision.
  1344  
  1345  	// By adding the X-From-Cache header we pretend this is served from a cache.
  1346  	mux.HandleFunc("/first", func(w http.ResponseWriter, r *http.Request) {
  1347  		w.Header().Set("X-From-Cache", "1")
  1348  		w.Header().Set(headerRateLimit, "60")
  1349  		w.Header().Set(headerRateRemaining, "0")
  1350  		w.Header().Set(headerRateReset, fmt.Sprint(reset.Unix()))
  1351  		w.Header().Set("Content-Type", "application/json; charset=utf-8")
  1352  		w.WriteHeader(http.StatusForbidden)
  1353  		fmt.Fprintln(w, `{
  1354     "message": "API rate limit exceeded for xxx.xxx.xxx.xxx. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)",
  1355     "documentation_url": "https://docs.github.com/en/rest/overview/resources-in-the-rest-api#abuse-rate-limits"
  1356  }`)
  1357  	})
  1358  
  1359  	madeNetworkCall := false
  1360  	mux.HandleFunc("/second", func(w http.ResponseWriter, r *http.Request) {
  1361  		madeNetworkCall = true
  1362  	})
  1363  
  1364  	// First request is made so afterwards we can check the returned rate limit headers were ignored.
  1365  	req, _ := client.NewRequest("GET", "first", nil)
  1366  	ctx := context.Background()
  1367  	_, err := client.Do(ctx, req, nil)
  1368  	if err == nil {
  1369  		t.Error("Expected error to be returned.")
  1370  	}
  1371  
  1372  	// Second request should not by hindered by rate limits.
  1373  	req, _ = client.NewRequest("GET", "second", nil)
  1374  	_, err = client.Do(ctx, req, nil)
  1375  
  1376  	if err != nil {
  1377  		t.Fatalf("Second request failed, even though the rate limits from the cache should've been ignored: %v", err)
  1378  	}
  1379  	if !madeNetworkCall {
  1380  		t.Fatal("Network call was not made, even though the rate limits from the cache should've been ignored")
  1381  	}
  1382  }
  1383  
  1384  // Ensure sleeps until the rate limit is reset when the client is rate limited.
  1385  func TestDo_rateLimit_sleepUntilResponseResetLimit(t *testing.T) {
  1386  	client, mux, _, teardown := setup()
  1387  	defer teardown()
  1388  
  1389  	reset := time.Now().UTC().Add(time.Second)
  1390  
  1391  	var firstRequest = true
  1392  	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  1393  		if firstRequest {
  1394  			firstRequest = false
  1395  			w.Header().Set(headerRateLimit, "60")
  1396  			w.Header().Set(headerRateRemaining, "0")
  1397  			w.Header().Set(headerRateReset, fmt.Sprint(reset.Unix()))
  1398  			w.Header().Set("Content-Type", "application/json; charset=utf-8")
  1399  			w.WriteHeader(http.StatusForbidden)
  1400  			fmt.Fprintln(w, `{
  1401     "message": "API rate limit exceeded for xxx.xxx.xxx.xxx. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)",
  1402     "documentation_url": "https://docs.github.com/en/rest/overview/resources-in-the-rest-api#abuse-rate-limits"
  1403  }`)
  1404  			return
  1405  		}
  1406  		w.Header().Set(headerRateLimit, "5000")
  1407  		w.Header().Set(headerRateRemaining, "5000")
  1408  		w.Header().Set(headerRateReset, fmt.Sprint(reset.Add(time.Hour).Unix()))
  1409  		w.Header().Set("Content-Type", "application/json; charset=utf-8")
  1410  		w.WriteHeader(http.StatusOK)
  1411  		fmt.Fprintln(w, `{}`)
  1412  	})
  1413  
  1414  	req, _ := client.NewRequest("GET", ".", nil)
  1415  	ctx := context.Background()
  1416  	resp, err := client.Do(context.WithValue(ctx, SleepUntilPrimaryRateLimitResetWhenRateLimited, true), req, nil)
  1417  	if err != nil {
  1418  		t.Errorf("Do returned unexpected error: %v", err)
  1419  	}
  1420  	if got, want := resp.StatusCode, http.StatusOK; got != want {
  1421  		t.Errorf("Response status code = %v, want %v", got, want)
  1422  	}
  1423  }
  1424  
  1425  // Ensure tries to sleep until the rate limit is reset when the client is rate limited, but only once.
  1426  func TestDo_rateLimit_sleepUntilResponseResetLimitRetryOnce(t *testing.T) {
  1427  	client, mux, _, teardown := setup()
  1428  	defer teardown()
  1429  
  1430  	reset := time.Now().UTC().Add(time.Second)
  1431  
  1432  	requestCount := 0
  1433  	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  1434  		requestCount++
  1435  		w.Header().Set(headerRateLimit, "60")
  1436  		w.Header().Set(headerRateRemaining, "0")
  1437  		w.Header().Set(headerRateReset, fmt.Sprint(reset.Unix()))
  1438  		w.Header().Set("Content-Type", "application/json; charset=utf-8")
  1439  		w.WriteHeader(http.StatusForbidden)
  1440  		fmt.Fprintln(w, `{
  1441     "message": "API rate limit exceeded for xxx.xxx.xxx.xxx. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)",
  1442     "documentation_url": "https://docs.github.com/en/rest/overview/resources-in-the-rest-api#abuse-rate-limits"
  1443  }`)
  1444  	})
  1445  
  1446  	req, _ := client.NewRequest("GET", ".", nil)
  1447  	ctx := context.Background()
  1448  	_, err := client.Do(context.WithValue(ctx, SleepUntilPrimaryRateLimitResetWhenRateLimited, true), req, nil)
  1449  	if err == nil {
  1450  		t.Error("Expected error to be returned.")
  1451  	}
  1452  	if got, want := requestCount, 2; got != want {
  1453  		t.Errorf("Expected 2 requests, got %d", got)
  1454  	}
  1455  }
  1456  
  1457  // Ensure a network call is not made when it's known that API rate limit is still exceeded.
  1458  func TestDo_rateLimit_sleepUntilClientResetLimit(t *testing.T) {
  1459  	client, mux, _, teardown := setup()
  1460  	defer teardown()
  1461  
  1462  	reset := time.Now().UTC().Add(time.Second)
  1463  	client.rateLimits[CoreCategory] = Rate{Limit: 5000, Remaining: 0, Reset: Timestamp{reset}}
  1464  	requestCount := 0
  1465  	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  1466  		requestCount++
  1467  		w.Header().Set(headerRateLimit, "5000")
  1468  		w.Header().Set(headerRateRemaining, "5000")
  1469  		w.Header().Set(headerRateReset, fmt.Sprint(reset.Add(time.Hour).Unix()))
  1470  		w.Header().Set("Content-Type", "application/json; charset=utf-8")
  1471  		w.WriteHeader(http.StatusOK)
  1472  		fmt.Fprintln(w, `{}`)
  1473  	})
  1474  	req, _ := client.NewRequest("GET", ".", nil)
  1475  	ctx := context.Background()
  1476  	resp, err := client.Do(context.WithValue(ctx, SleepUntilPrimaryRateLimitResetWhenRateLimited, true), req, nil)
  1477  	if err != nil {
  1478  		t.Errorf("Do returned unexpected error: %v", err)
  1479  	}
  1480  	if got, want := resp.StatusCode, http.StatusOK; got != want {
  1481  		t.Errorf("Response status code = %v, want %v", got, want)
  1482  	}
  1483  	if got, want := requestCount, 1; got != want {
  1484  		t.Errorf("Expected 1 request, got %d", got)
  1485  	}
  1486  }
  1487  
  1488  // Ensure sleep is aborted when the context is cancelled.
  1489  func TestDo_rateLimit_abortSleepContextCancelled(t *testing.T) {
  1490  	client, mux, _, teardown := setup()
  1491  	defer teardown()
  1492  
  1493  	// We use a 1 minute reset time to ensure the sleep is not completed.
  1494  	reset := time.Now().UTC().Add(time.Minute)
  1495  	requestCount := 0
  1496  	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  1497  		requestCount++
  1498  		w.Header().Set(headerRateLimit, "60")
  1499  		w.Header().Set(headerRateRemaining, "0")
  1500  		w.Header().Set(headerRateReset, fmt.Sprint(reset.Unix()))
  1501  		w.Header().Set("Content-Type", "application/json; charset=utf-8")
  1502  		w.WriteHeader(http.StatusForbidden)
  1503  		fmt.Fprintln(w, `{
  1504     "message": "API rate limit exceeded for xxx.xxx.xxx.xxx. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)",
  1505     "documentation_url": "https://docs.github.com/en/rest/overview/resources-in-the-rest-api#abuse-rate-limits"
  1506  }`)
  1507  	})
  1508  
  1509  	req, _ := client.NewRequest("GET", ".", nil)
  1510  	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
  1511  	defer cancel()
  1512  	_, err := client.Do(context.WithValue(ctx, SleepUntilPrimaryRateLimitResetWhenRateLimited, true), req, nil)
  1513  	if !errors.Is(err, context.DeadlineExceeded) {
  1514  		t.Error("Expected context deadline exceeded error.")
  1515  	}
  1516  	if got, want := requestCount, 1; got != want {
  1517  		t.Errorf("Expected 1 requests, got %d", got)
  1518  	}
  1519  }
  1520  
  1521  // Ensure sleep is aborted when the context is cancelled on initial request.
  1522  func TestDo_rateLimit_abortSleepContextCancelledClientLimit(t *testing.T) {
  1523  	client, mux, _, teardown := setup()
  1524  	defer teardown()
  1525  
  1526  	reset := time.Now().UTC().Add(time.Minute)
  1527  	client.rateLimits[CoreCategory] = Rate{Limit: 5000, Remaining: 0, Reset: Timestamp{reset}}
  1528  	requestCount := 0
  1529  	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  1530  		requestCount++
  1531  		w.Header().Set(headerRateLimit, "5000")
  1532  		w.Header().Set(headerRateRemaining, "5000")
  1533  		w.Header().Set(headerRateReset, fmt.Sprint(reset.Add(time.Hour).Unix()))
  1534  		w.Header().Set("Content-Type", "application/json; charset=utf-8")
  1535  		w.WriteHeader(http.StatusOK)
  1536  		fmt.Fprintln(w, `{}`)
  1537  	})
  1538  	req, _ := client.NewRequest("GET", ".", nil)
  1539  	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
  1540  	defer cancel()
  1541  	_, err := client.Do(context.WithValue(ctx, SleepUntilPrimaryRateLimitResetWhenRateLimited, true), req, nil)
  1542  	rateLimitError, ok := err.(*RateLimitError)
  1543  	if !ok {
  1544  		t.Fatalf("Expected a *rateLimitError error; got %#v.", err)
  1545  	}
  1546  	if got, wantSuffix := rateLimitError.Message, "Context cancelled while waiting for rate limit to reset until"; !strings.HasPrefix(got, wantSuffix) {
  1547  		t.Errorf("Expected request to be prevented because context cancellation, got: %v.", got)
  1548  	}
  1549  	if got, want := requestCount, 0; got != want {
  1550  		t.Errorf("Expected 1 requests, got %d", got)
  1551  	}
  1552  }
  1553  
  1554  // Ensure *AbuseRateLimitError is returned when the response indicates that
  1555  // the client has triggered an abuse detection mechanism.
  1556  func TestDo_rateLimit_abuseRateLimitError(t *testing.T) {
  1557  	client, mux, _, teardown := setup()
  1558  	defer teardown()
  1559  
  1560  	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  1561  		w.Header().Set("Content-Type", "application/json; charset=utf-8")
  1562  		w.WriteHeader(http.StatusForbidden)
  1563  		// When the abuse rate limit error is of the "temporarily blocked from content creation" type,
  1564  		// there is no "Retry-After" header.
  1565  		fmt.Fprintln(w, `{
  1566     "message": "You have triggered an abuse detection mechanism and have been temporarily blocked from content creation. Please retry your request again later.",
  1567     "documentation_url": "https://docs.github.com/en/rest/overview/resources-in-the-rest-api#abuse-rate-limits"
  1568  }`)
  1569  	})
  1570  
  1571  	req, _ := client.NewRequest("GET", ".", nil)
  1572  	ctx := context.Background()
  1573  	_, err := client.Do(ctx, req, nil)
  1574  
  1575  	if err == nil {
  1576  		t.Error("Expected error to be returned.")
  1577  	}
  1578  	abuseRateLimitErr, ok := err.(*AbuseRateLimitError)
  1579  	if !ok {
  1580  		t.Fatalf("Expected a *AbuseRateLimitError error; got %#v.", err)
  1581  	}
  1582  	if got, want := abuseRateLimitErr.RetryAfter, (*time.Duration)(nil); got != want {
  1583  		t.Errorf("abuseRateLimitErr RetryAfter = %v, want %v", got, want)
  1584  	}
  1585  }
  1586  
  1587  // Ensure *AbuseRateLimitError is returned when the response indicates that
  1588  // the client has triggered an abuse detection mechanism on GitHub Enterprise.
  1589  func TestDo_rateLimit_abuseRateLimitErrorEnterprise(t *testing.T) {
  1590  	client, mux, _, teardown := setup()
  1591  	defer teardown()
  1592  
  1593  	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  1594  		w.Header().Set("Content-Type", "application/json; charset=utf-8")
  1595  		w.WriteHeader(http.StatusForbidden)
  1596  		// When the abuse rate limit error is of the "temporarily blocked from content creation" type,
  1597  		// there is no "Retry-After" header.
  1598  		// This response returns a documentation url like the one returned for GitHub Enterprise, this
  1599  		// url changes between versions but follows roughly the same format.
  1600  		fmt.Fprintln(w, `{
  1601     "message": "You have triggered an abuse detection mechanism and have been temporarily blocked from content creation. Please retry your request again later.",
  1602     "documentation_url": "https://docs.github.com/en/rest/overview/resources-in-the-rest-api#abuse-rate-limits"
  1603  }`)
  1604  	})
  1605  
  1606  	req, _ := client.NewRequest("GET", ".", nil)
  1607  	ctx := context.Background()
  1608  	_, err := client.Do(ctx, req, nil)
  1609  
  1610  	if err == nil {
  1611  		t.Error("Expected error to be returned.")
  1612  	}
  1613  	abuseRateLimitErr, ok := err.(*AbuseRateLimitError)
  1614  	if !ok {
  1615  		t.Fatalf("Expected a *AbuseRateLimitError error; got %#v.", err)
  1616  	}
  1617  	if got, want := abuseRateLimitErr.RetryAfter, (*time.Duration)(nil); got != want {
  1618  		t.Errorf("abuseRateLimitErr RetryAfter = %v, want %v", got, want)
  1619  	}
  1620  }
  1621  
  1622  // Ensure *AbuseRateLimitError.RetryAfter is parsed correctly for the Retry-After header.
  1623  func TestDo_rateLimit_abuseRateLimitError_retryAfter(t *testing.T) {
  1624  	client, mux, _, teardown := setup()
  1625  	defer teardown()
  1626  
  1627  	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  1628  		w.Header().Set("Content-Type", "application/json; charset=utf-8")
  1629  		w.Header().Set(headerRetryAfter, "123") // Retry after value of 123 seconds.
  1630  		w.WriteHeader(http.StatusForbidden)
  1631  		fmt.Fprintln(w, `{
  1632     "message": "You have triggered an abuse detection mechanism ...",
  1633     "documentation_url": "https://docs.github.com/en/rest/overview/resources-in-the-rest-api#abuse-rate-limits"
  1634  }`)
  1635  	})
  1636  
  1637  	req, _ := client.NewRequest("GET", ".", nil)
  1638  	ctx := context.Background()
  1639  	_, err := client.Do(ctx, req, nil)
  1640  
  1641  	if err == nil {
  1642  		t.Error("Expected error to be returned.")
  1643  	}
  1644  	abuseRateLimitErr, ok := err.(*AbuseRateLimitError)
  1645  	if !ok {
  1646  		t.Fatalf("Expected a *AbuseRateLimitError error; got %#v.", err)
  1647  	}
  1648  	if abuseRateLimitErr.RetryAfter == nil {
  1649  		t.Fatalf("abuseRateLimitErr RetryAfter is nil, expected not-nil")
  1650  	}
  1651  	if got, want := *abuseRateLimitErr.RetryAfter, 123*time.Second; got != want {
  1652  		t.Errorf("abuseRateLimitErr RetryAfter = %v, want %v", got, want)
  1653  	}
  1654  
  1655  	// expect prevention of a following request
  1656  	if _, err = client.Do(ctx, req, nil); err == nil {
  1657  		t.Error("Expected error to be returned.")
  1658  	}
  1659  	abuseRateLimitErr, ok = err.(*AbuseRateLimitError)
  1660  	if !ok {
  1661  		t.Fatalf("Expected a *AbuseRateLimitError error; got %#v.", err)
  1662  	}
  1663  	if abuseRateLimitErr.RetryAfter == nil {
  1664  		t.Fatalf("abuseRateLimitErr RetryAfter is nil, expected not-nil")
  1665  	}
  1666  	// the saved duration might be a bit smaller than Retry-After because the duration is calculated from the expected end-of-cooldown time
  1667  	if got, want := *abuseRateLimitErr.RetryAfter, 123*time.Second; want-got > 1*time.Second {
  1668  		t.Errorf("abuseRateLimitErr RetryAfter = %v, want %v", got, want)
  1669  	}
  1670  	if got, wantSuffix := abuseRateLimitErr.Message, "not making remote request."; !strings.HasSuffix(got, wantSuffix) {
  1671  		t.Errorf("Expected request to be prevented because of secondary rate limit, got: %v.", got)
  1672  	}
  1673  }
  1674  
  1675  // Ensure *AbuseRateLimitError.RetryAfter is parsed correctly for the x-ratelimit-reset header.
  1676  func TestDo_rateLimit_abuseRateLimitError_xRateLimitReset(t *testing.T) {
  1677  	client, mux, _, teardown := setup()
  1678  	defer teardown()
  1679  
  1680  	// x-ratelimit-reset value of 123 seconds into the future.
  1681  	blockUntil := time.Now().Add(time.Duration(123) * time.Second).Unix()
  1682  
  1683  	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  1684  		w.Header().Set("Content-Type", "application/json; charset=utf-8")
  1685  		w.Header().Set(headerRateReset, strconv.Itoa(int(blockUntil)))
  1686  		w.Header().Set(headerRateRemaining, "1") // set remaining to a value > 0 to distinct from a primary rate limit
  1687  		w.WriteHeader(http.StatusForbidden)
  1688  		fmt.Fprintln(w, `{
  1689     "message": "You have triggered an abuse detection mechanism ...",
  1690     "documentation_url": "https://docs.github.com/en/rest/overview/resources-in-the-rest-api#abuse-rate-limits"
  1691  }`)
  1692  	})
  1693  
  1694  	req, _ := client.NewRequest("GET", ".", nil)
  1695  	ctx := context.Background()
  1696  	_, err := client.Do(ctx, req, nil)
  1697  
  1698  	if err == nil {
  1699  		t.Error("Expected error to be returned.")
  1700  	}
  1701  	abuseRateLimitErr, ok := err.(*AbuseRateLimitError)
  1702  	if !ok {
  1703  		t.Fatalf("Expected a *AbuseRateLimitError error; got %#v.", err)
  1704  	}
  1705  	if abuseRateLimitErr.RetryAfter == nil {
  1706  		t.Fatalf("abuseRateLimitErr RetryAfter is nil, expected not-nil")
  1707  	}
  1708  	// the retry after value might be a bit smaller than the original duration because the duration is calculated from the expected end-of-cooldown time
  1709  	if got, want := *abuseRateLimitErr.RetryAfter, 123*time.Second; want-got > 1*time.Second {
  1710  		t.Errorf("abuseRateLimitErr RetryAfter = %v, want %v", got, want)
  1711  	}
  1712  
  1713  	// expect prevention of a following request
  1714  	if _, err = client.Do(ctx, req, nil); err == nil {
  1715  		t.Error("Expected error to be returned.")
  1716  	}
  1717  	abuseRateLimitErr, ok = err.(*AbuseRateLimitError)
  1718  	if !ok {
  1719  		t.Fatalf("Expected a *AbuseRateLimitError error; got %#v.", err)
  1720  	}
  1721  	if abuseRateLimitErr.RetryAfter == nil {
  1722  		t.Fatalf("abuseRateLimitErr RetryAfter is nil, expected not-nil")
  1723  	}
  1724  	// the saved duration might be a bit smaller than Retry-After because the duration is calculated from the expected end-of-cooldown time
  1725  	if got, want := *abuseRateLimitErr.RetryAfter, 123*time.Second; want-got > 1*time.Second {
  1726  		t.Errorf("abuseRateLimitErr RetryAfter = %v, want %v", got, want)
  1727  	}
  1728  	if got, wantSuffix := abuseRateLimitErr.Message, "not making remote request."; !strings.HasSuffix(got, wantSuffix) {
  1729  		t.Errorf("Expected request to be prevented because of secondary rate limit, got: %v.", got)
  1730  	}
  1731  }
  1732  
  1733  func TestDo_noContent(t *testing.T) {
  1734  	client, mux, _, teardown := setup()
  1735  	defer teardown()
  1736  
  1737  	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  1738  		w.WriteHeader(http.StatusNoContent)
  1739  	})
  1740  
  1741  	var body json.RawMessage
  1742  
  1743  	req, _ := client.NewRequest("GET", ".", nil)
  1744  	ctx := context.Background()
  1745  	_, err := client.Do(ctx, req, &body)
  1746  	if err != nil {
  1747  		t.Fatalf("Do returned unexpected error: %v", err)
  1748  	}
  1749  }
  1750  
  1751  func TestSanitizeURL(t *testing.T) {
  1752  	tests := []struct {
  1753  		in, want string
  1754  	}{
  1755  		{"/?a=b", "/?a=b"},
  1756  		{"/?a=b&client_secret=secret", "/?a=b&client_secret=REDACTED"},
  1757  		{"/?a=b&client_id=id&client_secret=secret", "/?a=b&client_id=id&client_secret=REDACTED"},
  1758  	}
  1759  
  1760  	for _, tt := range tests {
  1761  		inURL, _ := url.Parse(tt.in)
  1762  		want, _ := url.Parse(tt.want)
  1763  
  1764  		if got := sanitizeURL(inURL); !cmp.Equal(got, want) {
  1765  			t.Errorf("sanitizeURL(%v) returned %v, want %v", tt.in, got, want)
  1766  		}
  1767  	}
  1768  }
  1769  
  1770  func TestCheckResponse(t *testing.T) {
  1771  	res := &http.Response{
  1772  		Request:    &http.Request{},
  1773  		StatusCode: http.StatusBadRequest,
  1774  		Body: io.NopCloser(strings.NewReader(`{"message":"m",
  1775  			"errors": [{"resource": "r", "field": "f", "code": "c"}],
  1776  			"block": {"reason": "dmca", "created_at": "2016-03-17T15:39:46Z"}}`)),
  1777  	}
  1778  	err := CheckResponse(res).(*ErrorResponse)
  1779  
  1780  	if err == nil {
  1781  		t.Errorf("Expected error response.")
  1782  	}
  1783  
  1784  	want := &ErrorResponse{
  1785  		Response: res,
  1786  		Message:  "m",
  1787  		Errors:   []Error{{Resource: "r", Field: "f", Code: "c"}},
  1788  		Block: &ErrorBlock{
  1789  			Reason:    "dmca",
  1790  			CreatedAt: &Timestamp{time.Date(2016, time.March, 17, 15, 39, 46, 0, time.UTC)},
  1791  		},
  1792  	}
  1793  	if !errors.Is(err, want) {
  1794  		t.Errorf("Error = %#v, want %#v", err, want)
  1795  	}
  1796  }
  1797  
  1798  func TestCheckResponse_RateLimit(t *testing.T) {
  1799  	res := &http.Response{
  1800  		Request:    &http.Request{},
  1801  		StatusCode: http.StatusForbidden,
  1802  		Header:     http.Header{},
  1803  		Body: io.NopCloser(strings.NewReader(`{"message":"m",
  1804  			"documentation_url": "url"}`)),
  1805  	}
  1806  	res.Header.Set(headerRateLimit, "60")
  1807  	res.Header.Set(headerRateRemaining, "0")
  1808  	res.Header.Set(headerRateReset, "243424")
  1809  
  1810  	err := CheckResponse(res).(*RateLimitError)
  1811  
  1812  	if err == nil {
  1813  		t.Errorf("Expected error response.")
  1814  	}
  1815  
  1816  	want := &RateLimitError{
  1817  		Rate:     parseRate(res),
  1818  		Response: res,
  1819  		Message:  "m",
  1820  	}
  1821  	if !errors.Is(err, want) {
  1822  		t.Errorf("Error = %#v, want %#v", err, want)
  1823  	}
  1824  }
  1825  
  1826  func TestCheckResponse_AbuseRateLimit(t *testing.T) {
  1827  	res := &http.Response{
  1828  		Request:    &http.Request{},
  1829  		StatusCode: http.StatusForbidden,
  1830  		Body: io.NopCloser(strings.NewReader(`{"message":"m",
  1831  			"documentation_url": "docs.github.com/en/rest/overview/resources-in-the-rest-api#abuse-rate-limits"}`)),
  1832  	}
  1833  	err := CheckResponse(res).(*AbuseRateLimitError)
  1834  
  1835  	if err == nil {
  1836  		t.Errorf("Expected error response.")
  1837  	}
  1838  
  1839  	want := &AbuseRateLimitError{
  1840  		Response: res,
  1841  		Message:  "m",
  1842  	}
  1843  	if !errors.Is(err, want) {
  1844  		t.Errorf("Error = %#v, want %#v", err, want)
  1845  	}
  1846  }
  1847  
  1848  func TestCompareHttpResponse(t *testing.T) {
  1849  	testcases := map[string]struct {
  1850  		h1       *http.Response
  1851  		h2       *http.Response
  1852  		expected bool
  1853  	}{
  1854  		"both are nil": {
  1855  			expected: true,
  1856  		},
  1857  		"both are non nil - same StatusCode": {
  1858  			expected: true,
  1859  			h1:       &http.Response{StatusCode: 200},
  1860  			h2:       &http.Response{StatusCode: 200},
  1861  		},
  1862  		"both are non nil - different StatusCode": {
  1863  			expected: false,
  1864  			h1:       &http.Response{StatusCode: 200},
  1865  			h2:       &http.Response{StatusCode: 404},
  1866  		},
  1867  		"one is nil, other is not": {
  1868  			expected: false,
  1869  			h2:       &http.Response{},
  1870  		},
  1871  	}
  1872  
  1873  	for name, tc := range testcases {
  1874  		t.Run(name, func(t *testing.T) {
  1875  			v := compareHTTPResponse(tc.h1, tc.h2)
  1876  			if tc.expected != v {
  1877  				t.Errorf("Expected %t, got %t for (%#v, %#v)", tc.expected, v, tc.h1, tc.h2)
  1878  			}
  1879  		})
  1880  	}
  1881  }
  1882  
  1883  func TestErrorResponse_Is(t *testing.T) {
  1884  	err := &ErrorResponse{
  1885  		Response: &http.Response{},
  1886  		Message:  "m",
  1887  		Errors:   []Error{{Resource: "r", Field: "f", Code: "c"}},
  1888  		Block: &ErrorBlock{
  1889  			Reason:    "r",
  1890  			CreatedAt: &Timestamp{time.Date(2016, time.March, 17, 15, 39, 46, 0, time.UTC)},
  1891  		},
  1892  		DocumentationURL: "https://github.com",
  1893  	}
  1894  	testcases := map[string]struct {
  1895  		wantSame   bool
  1896  		otherError error
  1897  	}{
  1898  		"errors are same": {
  1899  			wantSame: true,
  1900  			otherError: &ErrorResponse{
  1901  				Response: &http.Response{},
  1902  				Errors:   []Error{{Resource: "r", Field: "f", Code: "c"}},
  1903  				Message:  "m",
  1904  				Block: &ErrorBlock{
  1905  					Reason:    "r",
  1906  					CreatedAt: &Timestamp{time.Date(2016, time.March, 17, 15, 39, 46, 0, time.UTC)},
  1907  				},
  1908  				DocumentationURL: "https://github.com",
  1909  			},
  1910  		},
  1911  		"errors have different values - Message": {
  1912  			wantSame: false,
  1913  			otherError: &ErrorResponse{
  1914  				Response: &http.Response{},
  1915  				Errors:   []Error{{Resource: "r", Field: "f", Code: "c"}},
  1916  				Message:  "m1",
  1917  				Block: &ErrorBlock{
  1918  					Reason:    "r",
  1919  					CreatedAt: &Timestamp{time.Date(2016, time.March, 17, 15, 39, 46, 0, time.UTC)},
  1920  				},
  1921  				DocumentationURL: "https://github.com",
  1922  			},
  1923  		},
  1924  		"errors have different values - DocumentationURL": {
  1925  			wantSame: false,
  1926  			otherError: &ErrorResponse{
  1927  				Response: &http.Response{},
  1928  				Errors:   []Error{{Resource: "r", Field: "f", Code: "c"}},
  1929  				Message:  "m",
  1930  				Block: &ErrorBlock{
  1931  					Reason:    "r",
  1932  					CreatedAt: &Timestamp{time.Date(2016, time.March, 17, 15, 39, 46, 0, time.UTC)},
  1933  				},
  1934  				DocumentationURL: "https://google.com",
  1935  			},
  1936  		},
  1937  		"errors have different values - Response is nil": {
  1938  			wantSame: false,
  1939  			otherError: &ErrorResponse{
  1940  				Errors:  []Error{{Resource: "r", Field: "f", Code: "c"}},
  1941  				Message: "m",
  1942  				Block: &ErrorBlock{
  1943  					Reason:    "r",
  1944  					CreatedAt: &Timestamp{time.Date(2016, time.March, 17, 15, 39, 46, 0, time.UTC)},
  1945  				},
  1946  				DocumentationURL: "https://github.com",
  1947  			},
  1948  		},
  1949  		"errors have different values - Errors": {
  1950  			wantSame: false,
  1951  			otherError: &ErrorResponse{
  1952  				Response: &http.Response{},
  1953  				Errors:   []Error{{Resource: "r1", Field: "f1", Code: "c1"}},
  1954  				Message:  "m",
  1955  				Block: &ErrorBlock{
  1956  					Reason:    "r",
  1957  					CreatedAt: &Timestamp{time.Date(2016, time.March, 17, 15, 39, 46, 0, time.UTC)},
  1958  				},
  1959  				DocumentationURL: "https://github.com",
  1960  			},
  1961  		},
  1962  		"errors have different values - Errors have different length": {
  1963  			wantSame: false,
  1964  			otherError: &ErrorResponse{
  1965  				Response: &http.Response{},
  1966  				Errors:   []Error{},
  1967  				Message:  "m",
  1968  				Block: &ErrorBlock{
  1969  					Reason:    "r",
  1970  					CreatedAt: &Timestamp{time.Date(2016, time.March, 17, 15, 39, 46, 0, time.UTC)},
  1971  				},
  1972  				DocumentationURL: "https://github.com",
  1973  			},
  1974  		},
  1975  		"errors have different values - Block - one is nil, other is not": {
  1976  			wantSame: false,
  1977  			otherError: &ErrorResponse{
  1978  				Response:         &http.Response{},
  1979  				Errors:           []Error{{Resource: "r", Field: "f", Code: "c"}},
  1980  				Message:          "m",
  1981  				DocumentationURL: "https://github.com",
  1982  			},
  1983  		},
  1984  		"errors have different values - Block - different Reason": {
  1985  			wantSame: false,
  1986  			otherError: &ErrorResponse{
  1987  				Response: &http.Response{},
  1988  				Errors:   []Error{{Resource: "r", Field: "f", Code: "c"}},
  1989  				Message:  "m",
  1990  				Block: &ErrorBlock{
  1991  					Reason:    "r1",
  1992  					CreatedAt: &Timestamp{time.Date(2016, time.March, 17, 15, 39, 46, 0, time.UTC)},
  1993  				},
  1994  				DocumentationURL: "https://github.com",
  1995  			},
  1996  		},
  1997  		"errors have different values - Block - different CreatedAt #1": {
  1998  			wantSame: false,
  1999  			otherError: &ErrorResponse{
  2000  				Response: &http.Response{},
  2001  				Errors:   []Error{{Resource: "r", Field: "f", Code: "c"}},
  2002  				Message:  "m",
  2003  				Block: &ErrorBlock{
  2004  					Reason:    "r",
  2005  					CreatedAt: nil,
  2006  				},
  2007  				DocumentationURL: "https://github.com",
  2008  			},
  2009  		},
  2010  		"errors have different values - Block - different CreatedAt #2": {
  2011  			wantSame: false,
  2012  			otherError: &ErrorResponse{
  2013  				Response: &http.Response{},
  2014  				Errors:   []Error{{Resource: "r", Field: "f", Code: "c"}},
  2015  				Message:  "m",
  2016  				Block: &ErrorBlock{
  2017  					Reason:    "r",
  2018  					CreatedAt: &Timestamp{time.Date(2017, time.March, 17, 15, 39, 46, 0, time.UTC)},
  2019  				},
  2020  				DocumentationURL: "https://github.com",
  2021  			},
  2022  		},
  2023  		"errors have different types": {
  2024  			wantSame:   false,
  2025  			otherError: errors.New("Github"),
  2026  		},
  2027  	}
  2028  
  2029  	for name, tc := range testcases {
  2030  		t.Run(name, func(t *testing.T) {
  2031  			if tc.wantSame != err.Is(tc.otherError) {
  2032  				t.Errorf("Error = %#v, want %#v", err, tc.otherError)
  2033  			}
  2034  		})
  2035  	}
  2036  }
  2037  
  2038  func TestRateLimitError_Is(t *testing.T) {
  2039  	err := &RateLimitError{
  2040  		Response: &http.Response{},
  2041  		Message:  "Github",
  2042  	}
  2043  	testcases := map[string]struct {
  2044  		wantSame   bool
  2045  		err        *RateLimitError
  2046  		otherError error
  2047  	}{
  2048  		"errors are same": {
  2049  			wantSame: true,
  2050  			err:      err,
  2051  			otherError: &RateLimitError{
  2052  				Response: &http.Response{},
  2053  				Message:  "Github",
  2054  			},
  2055  		},
  2056  		"errors are same - Response is nil": {
  2057  			wantSame: true,
  2058  			err: &RateLimitError{
  2059  				Message: "Github",
  2060  			},
  2061  			otherError: &RateLimitError{
  2062  				Message: "Github",
  2063  			},
  2064  		},
  2065  		"errors have different values - Rate": {
  2066  			wantSame: false,
  2067  			err:      err,
  2068  			otherError: &RateLimitError{
  2069  				Rate:     Rate{Limit: 10},
  2070  				Response: &http.Response{},
  2071  				Message:  "Gitlab",
  2072  			},
  2073  		},
  2074  		"errors have different values - Response is nil": {
  2075  			wantSame: false,
  2076  			err:      err,
  2077  			otherError: &RateLimitError{
  2078  				Message: "Github",
  2079  			},
  2080  		},
  2081  		"errors have different values - StatusCode": {
  2082  			wantSame: false,
  2083  			err:      err,
  2084  			otherError: &RateLimitError{
  2085  				Response: &http.Response{StatusCode: 200},
  2086  				Message:  "Github",
  2087  			},
  2088  		},
  2089  		"errors have different types": {
  2090  			wantSame:   false,
  2091  			err:        err,
  2092  			otherError: errors.New("Github"),
  2093  		},
  2094  	}
  2095  
  2096  	for name, tc := range testcases {
  2097  		t.Run(name, func(t *testing.T) {
  2098  			if tc.wantSame != tc.err.Is(tc.otherError) {
  2099  				t.Errorf("Error = %#v, want %#v", tc.err, tc.otherError)
  2100  			}
  2101  		})
  2102  	}
  2103  }
  2104  
  2105  func TestAbuseRateLimitError_Is(t *testing.T) {
  2106  	t1 := 1 * time.Second
  2107  	t2 := 2 * time.Second
  2108  	err := &AbuseRateLimitError{
  2109  		Response:   &http.Response{},
  2110  		Message:    "Github",
  2111  		RetryAfter: &t1,
  2112  	}
  2113  	testcases := map[string]struct {
  2114  		wantSame   bool
  2115  		err        *AbuseRateLimitError
  2116  		otherError error
  2117  	}{
  2118  		"errors are same": {
  2119  			wantSame: true,
  2120  			err:      err,
  2121  			otherError: &AbuseRateLimitError{
  2122  				Response:   &http.Response{},
  2123  				Message:    "Github",
  2124  				RetryAfter: &t1,
  2125  			},
  2126  		},
  2127  		"errors are same - Response is nil": {
  2128  			wantSame: true,
  2129  			err: &AbuseRateLimitError{
  2130  				Message:    "Github",
  2131  				RetryAfter: &t1,
  2132  			},
  2133  			otherError: &AbuseRateLimitError{
  2134  				Message:    "Github",
  2135  				RetryAfter: &t1,
  2136  			},
  2137  		},
  2138  		"errors have different values - Message": {
  2139  			wantSame: false,
  2140  			err:      err,
  2141  			otherError: &AbuseRateLimitError{
  2142  				Response:   &http.Response{},
  2143  				Message:    "Gitlab",
  2144  				RetryAfter: nil,
  2145  			},
  2146  		},
  2147  		"errors have different values - RetryAfter": {
  2148  			wantSame: false,
  2149  			err:      err,
  2150  			otherError: &AbuseRateLimitError{
  2151  				Response:   &http.Response{},
  2152  				Message:    "Github",
  2153  				RetryAfter: &t2,
  2154  			},
  2155  		},
  2156  		"errors have different values - Response is nil": {
  2157  			wantSame: false,
  2158  			err:      err,
  2159  			otherError: &AbuseRateLimitError{
  2160  				Message:    "Github",
  2161  				RetryAfter: &t1,
  2162  			},
  2163  		},
  2164  		"errors have different values - StatusCode": {
  2165  			wantSame: false,
  2166  			err:      err,
  2167  			otherError: &AbuseRateLimitError{
  2168  				Response:   &http.Response{StatusCode: 200},
  2169  				Message:    "Github",
  2170  				RetryAfter: &t1,
  2171  			},
  2172  		},
  2173  		"errors have different types": {
  2174  			wantSame:   false,
  2175  			err:        err,
  2176  			otherError: errors.New("Github"),
  2177  		},
  2178  	}
  2179  
  2180  	for name, tc := range testcases {
  2181  		t.Run(name, func(t *testing.T) {
  2182  			if tc.wantSame != tc.err.Is(tc.otherError) {
  2183  				t.Errorf("Error = %#v, want %#v", tc.err, tc.otherError)
  2184  			}
  2185  		})
  2186  	}
  2187  }
  2188  
  2189  func TestAcceptedError_Is(t *testing.T) {
  2190  	err := &AcceptedError{Raw: []byte("Github")}
  2191  	testcases := map[string]struct {
  2192  		wantSame   bool
  2193  		otherError error
  2194  	}{
  2195  		"errors are same": {
  2196  			wantSame:   true,
  2197  			otherError: &AcceptedError{Raw: []byte("Github")},
  2198  		},
  2199  		"errors have different values": {
  2200  			wantSame:   false,
  2201  			otherError: &AcceptedError{Raw: []byte("Gitlab")},
  2202  		},
  2203  		"errors have different types": {
  2204  			wantSame:   false,
  2205  			otherError: errors.New("Github"),
  2206  		},
  2207  	}
  2208  
  2209  	for name, tc := range testcases {
  2210  		t.Run(name, func(t *testing.T) {
  2211  			if tc.wantSame != err.Is(tc.otherError) {
  2212  				t.Errorf("Error = %#v, want %#v", err, tc.otherError)
  2213  			}
  2214  		})
  2215  	}
  2216  }
  2217  
  2218  // ensure that we properly handle API errors that do not contain a response body
  2219  func TestCheckResponse_noBody(t *testing.T) {
  2220  	res := &http.Response{
  2221  		Request:    &http.Request{},
  2222  		StatusCode: http.StatusBadRequest,
  2223  		Body:       io.NopCloser(strings.NewReader("")),
  2224  	}
  2225  	err := CheckResponse(res).(*ErrorResponse)
  2226  
  2227  	if err == nil {
  2228  		t.Errorf("Expected error response.")
  2229  	}
  2230  
  2231  	want := &ErrorResponse{
  2232  		Response: res,
  2233  	}
  2234  	if !errors.Is(err, want) {
  2235  		t.Errorf("Error = %#v, want %#v", err, want)
  2236  	}
  2237  }
  2238  
  2239  func TestCheckResponse_unexpectedErrorStructure(t *testing.T) {
  2240  	httpBody := `{"message":"m", "errors": ["error 1"]}`
  2241  	res := &http.Response{
  2242  		Request:    &http.Request{},
  2243  		StatusCode: http.StatusBadRequest,
  2244  		Body:       io.NopCloser(strings.NewReader(httpBody)),
  2245  	}
  2246  	err := CheckResponse(res).(*ErrorResponse)
  2247  
  2248  	if err == nil {
  2249  		t.Errorf("Expected error response.")
  2250  	}
  2251  
  2252  	want := &ErrorResponse{
  2253  		Response: res,
  2254  		Message:  "m",
  2255  		Errors:   []Error{{Message: "error 1"}},
  2256  	}
  2257  	if !errors.Is(err, want) {
  2258  		t.Errorf("Error = %#v, want %#v", err, want)
  2259  	}
  2260  	data, err2 := io.ReadAll(err.Response.Body)
  2261  	if err2 != nil {
  2262  		t.Fatalf("failed to read response body: %v", err)
  2263  	}
  2264  	if got := string(data); got != httpBody {
  2265  		t.Errorf("ErrorResponse.Response.Body = %q, want %q", got, httpBody)
  2266  	}
  2267  }
  2268  
  2269  func TestParseBooleanResponse_true(t *testing.T) {
  2270  	result, err := parseBoolResponse(nil)
  2271  	if err != nil {
  2272  		t.Errorf("parseBoolResponse returned error: %+v", err)
  2273  	}
  2274  
  2275  	if want := true; result != want {
  2276  		t.Errorf("parseBoolResponse returned %+v, want: %+v", result, want)
  2277  	}
  2278  }
  2279  
  2280  func TestParseBooleanResponse_false(t *testing.T) {
  2281  	v := &ErrorResponse{Response: &http.Response{StatusCode: http.StatusNotFound}}
  2282  	result, err := parseBoolResponse(v)
  2283  	if err != nil {
  2284  		t.Errorf("parseBoolResponse returned error: %+v", err)
  2285  	}
  2286  
  2287  	if want := false; result != want {
  2288  		t.Errorf("parseBoolResponse returned %+v, want: %+v", result, want)
  2289  	}
  2290  }
  2291  
  2292  func TestParseBooleanResponse_error(t *testing.T) {
  2293  	v := &ErrorResponse{Response: &http.Response{StatusCode: http.StatusBadRequest}}
  2294  	result, err := parseBoolResponse(v)
  2295  
  2296  	if err == nil {
  2297  		t.Errorf("Expected error to be returned.")
  2298  	}
  2299  
  2300  	if want := false; result != want {
  2301  		t.Errorf("parseBoolResponse returned %+v, want: %+v", result, want)
  2302  	}
  2303  }
  2304  
  2305  func TestErrorResponse_Error(t *testing.T) {
  2306  	res := &http.Response{Request: &http.Request{}}
  2307  	err := ErrorResponse{Message: "m", Response: res}
  2308  	if err.Error() == "" {
  2309  		t.Errorf("Expected non-empty ErrorResponse.Error()")
  2310  	}
  2311  
  2312  	//dont panic if request is nil
  2313  	res = &http.Response{}
  2314  	err = ErrorResponse{Message: "m", Response: res}
  2315  	if err.Error() == "" {
  2316  		t.Errorf("Expected non-empty ErrorResponse.Error()")
  2317  	}
  2318  
  2319  	//dont panic if response is nil
  2320  	err = ErrorResponse{Message: "m"}
  2321  	if err.Error() == "" {
  2322  		t.Errorf("Expected non-empty ErrorResponse.Error()")
  2323  	}
  2324  }
  2325  
  2326  func TestError_Error(t *testing.T) {
  2327  	err := Error{}
  2328  	if err.Error() == "" {
  2329  		t.Errorf("Expected non-empty Error.Error()")
  2330  	}
  2331  }
  2332  
  2333  func TestSetCredentialsAsHeaders(t *testing.T) {
  2334  	req := new(http.Request)
  2335  	id, secret := "id", "secret"
  2336  	modifiedRequest := setCredentialsAsHeaders(req, id, secret)
  2337  
  2338  	actualID, actualSecret, ok := modifiedRequest.BasicAuth()
  2339  	if !ok {
  2340  		t.Errorf("request does not contain basic credentials")
  2341  	}
  2342  
  2343  	if actualID != id {
  2344  		t.Errorf("id is %s, want %s", actualID, id)
  2345  	}
  2346  
  2347  	if actualSecret != secret {
  2348  		t.Errorf("secret is %s, want %s", actualSecret, secret)
  2349  	}
  2350  }
  2351  
  2352  func TestUnauthenticatedRateLimitedTransport(t *testing.T) {
  2353  	client, mux, _, teardown := setup()
  2354  	defer teardown()
  2355  
  2356  	clientID, clientSecret := "id", "secret"
  2357  	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  2358  		id, secret, ok := r.BasicAuth()
  2359  		if !ok {
  2360  			t.Errorf("request does not contain basic auth credentials")
  2361  		}
  2362  		if id != clientID {
  2363  			t.Errorf("request contained basic auth username %q, want %q", id, clientID)
  2364  		}
  2365  		if secret != clientSecret {
  2366  			t.Errorf("request contained basic auth password %q, want %q", secret, clientSecret)
  2367  		}
  2368  	})
  2369  
  2370  	tp := &UnauthenticatedRateLimitedTransport{
  2371  		ClientID:     clientID,
  2372  		ClientSecret: clientSecret,
  2373  	}
  2374  	unauthedClient := NewClient(tp.Client())
  2375  	unauthedClient.BaseURL = client.BaseURL
  2376  	req, _ := unauthedClient.NewRequest("GET", ".", nil)
  2377  	ctx := context.Background()
  2378  	_, err := unauthedClient.Do(ctx, req, nil)
  2379  	assertNilError(t, err)
  2380  }
  2381  
  2382  func TestUnauthenticatedRateLimitedTransport_missingFields(t *testing.T) {
  2383  	// missing ClientID
  2384  	tp := &UnauthenticatedRateLimitedTransport{
  2385  		ClientSecret: "secret",
  2386  	}
  2387  	_, err := tp.RoundTrip(nil)
  2388  	if err == nil {
  2389  		t.Errorf("Expected error to be returned")
  2390  	}
  2391  
  2392  	// missing ClientSecret
  2393  	tp = &UnauthenticatedRateLimitedTransport{
  2394  		ClientID: "id",
  2395  	}
  2396  	_, err = tp.RoundTrip(nil)
  2397  	if err == nil {
  2398  		t.Errorf("Expected error to be returned")
  2399  	}
  2400  }
  2401  
  2402  func TestUnauthenticatedRateLimitedTransport_transport(t *testing.T) {
  2403  	// default transport
  2404  	tp := &UnauthenticatedRateLimitedTransport{
  2405  		ClientID:     "id",
  2406  		ClientSecret: "secret",
  2407  	}
  2408  	if tp.transport() != http.DefaultTransport {
  2409  		t.Errorf("Expected http.DefaultTransport to be used.")
  2410  	}
  2411  
  2412  	// custom transport
  2413  	tp = &UnauthenticatedRateLimitedTransport{
  2414  		ClientID:     "id",
  2415  		ClientSecret: "secret",
  2416  		Transport:    &http.Transport{},
  2417  	}
  2418  	if tp.transport() == http.DefaultTransport {
  2419  		t.Errorf("Expected custom transport to be used.")
  2420  	}
  2421  }
  2422  
  2423  func TestBasicAuthTransport(t *testing.T) {
  2424  	client, mux, _, teardown := setup()
  2425  	defer teardown()
  2426  
  2427  	username, password, otp := "u", "p", "123456"
  2428  
  2429  	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  2430  		u, p, ok := r.BasicAuth()
  2431  		if !ok {
  2432  			t.Errorf("request does not contain basic auth credentials")
  2433  		}
  2434  		if u != username {
  2435  			t.Errorf("request contained basic auth username %q, want %q", u, username)
  2436  		}
  2437  		if p != password {
  2438  			t.Errorf("request contained basic auth password %q, want %q", p, password)
  2439  		}
  2440  		if got, want := r.Header.Get(headerOTP), otp; got != want {
  2441  			t.Errorf("request contained OTP %q, want %q", got, want)
  2442  		}
  2443  	})
  2444  
  2445  	tp := &BasicAuthTransport{
  2446  		Username: username,
  2447  		Password: password,
  2448  		OTP:      otp,
  2449  	}
  2450  	basicAuthClient := NewClient(tp.Client())
  2451  	basicAuthClient.BaseURL = client.BaseURL
  2452  	req, _ := basicAuthClient.NewRequest("GET", ".", nil)
  2453  	ctx := context.Background()
  2454  	_, err := basicAuthClient.Do(ctx, req, nil)
  2455  	assertNilError(t, err)
  2456  }
  2457  
  2458  func TestBasicAuthTransport_transport(t *testing.T) {
  2459  	// default transport
  2460  	tp := &BasicAuthTransport{}
  2461  	if tp.transport() != http.DefaultTransport {
  2462  		t.Errorf("Expected http.DefaultTransport to be used.")
  2463  	}
  2464  
  2465  	// custom transport
  2466  	tp = &BasicAuthTransport{
  2467  		Transport: &http.Transport{},
  2468  	}
  2469  	if tp.transport() == http.DefaultTransport {
  2470  		t.Errorf("Expected custom transport to be used.")
  2471  	}
  2472  }
  2473  
  2474  func TestFormatRateReset(t *testing.T) {
  2475  	d := 120*time.Minute + 12*time.Second
  2476  	got := formatRateReset(d)
  2477  	want := "[rate reset in 120m12s]"
  2478  	if got != want {
  2479  		t.Errorf("Format is wrong. got: %v, want: %v", got, want)
  2480  	}
  2481  
  2482  	d = 14*time.Minute + 2*time.Second
  2483  	got = formatRateReset(d)
  2484  	want = "[rate reset in 14m02s]"
  2485  	if got != want {
  2486  		t.Errorf("Format is wrong. got: %v, want: %v", got, want)
  2487  	}
  2488  
  2489  	d = 2*time.Minute + 2*time.Second
  2490  	got = formatRateReset(d)
  2491  	want = "[rate reset in 2m02s]"
  2492  	if got != want {
  2493  		t.Errorf("Format is wrong. got: %v, want: %v", got, want)
  2494  	}
  2495  
  2496  	d = 12 * time.Second
  2497  	got = formatRateReset(d)
  2498  	want = "[rate reset in 12s]"
  2499  	if got != want {
  2500  		t.Errorf("Format is wrong. got: %v, want: %v", got, want)
  2501  	}
  2502  
  2503  	d = -1 * (2*time.Hour + 2*time.Second)
  2504  	got = formatRateReset(d)
  2505  	want = "[rate limit was reset 120m02s ago]"
  2506  	if got != want {
  2507  		t.Errorf("Format is wrong. got: %v, want: %v", got, want)
  2508  	}
  2509  }
  2510  
  2511  func TestNestedStructAccessorNoPanic(t *testing.T) {
  2512  	issue := &Issue{User: nil}
  2513  	got := issue.GetUser().GetPlan().GetName()
  2514  	want := ""
  2515  	if got != want {
  2516  		t.Errorf("Issues.Get.GetUser().GetPlan().GetName() returned %+v, want %+v", got, want)
  2517  	}
  2518  }
  2519  
  2520  func TestTwoFactorAuthError(t *testing.T) {
  2521  	u, err := url.Parse("https://example.com")
  2522  	if err != nil {
  2523  		t.Fatal(err)
  2524  	}
  2525  
  2526  	e := &TwoFactorAuthError{
  2527  		Response: &http.Response{
  2528  			Request:    &http.Request{Method: "PUT", URL: u},
  2529  			StatusCode: http.StatusTooManyRequests,
  2530  		},
  2531  		Message: "<msg>",
  2532  	}
  2533  	if got, want := e.Error(), "PUT https://example.com: 429 <msg> []"; got != want {
  2534  		t.Errorf("TwoFactorAuthError = %q, want %q", got, want)
  2535  	}
  2536  }
  2537  
  2538  func TestRateLimitError(t *testing.T) {
  2539  	u, err := url.Parse("https://example.com")
  2540  	if err != nil {
  2541  		t.Fatal(err)
  2542  	}
  2543  
  2544  	r := &RateLimitError{
  2545  		Response: &http.Response{
  2546  			Request:    &http.Request{Method: "PUT", URL: u},
  2547  			StatusCode: http.StatusTooManyRequests,
  2548  		},
  2549  		Message: "<msg>",
  2550  	}
  2551  	if got, want := r.Error(), "PUT https://example.com: 429 <msg> [rate limit was reset"; !strings.Contains(got, want) {
  2552  		t.Errorf("RateLimitError = %q, want %q", got, want)
  2553  	}
  2554  }
  2555  
  2556  func TestAcceptedError(t *testing.T) {
  2557  	a := &AcceptedError{}
  2558  	if got, want := a.Error(), "try again later"; !strings.Contains(got, want) {
  2559  		t.Errorf("AcceptedError = %q, want %q", got, want)
  2560  	}
  2561  }
  2562  
  2563  func TestAbuseRateLimitError(t *testing.T) {
  2564  	u, err := url.Parse("https://example.com")
  2565  	if err != nil {
  2566  		t.Fatal(err)
  2567  	}
  2568  
  2569  	r := &AbuseRateLimitError{
  2570  		Response: &http.Response{
  2571  			Request:    &http.Request{Method: "PUT", URL: u},
  2572  			StatusCode: http.StatusTooManyRequests,
  2573  		},
  2574  		Message: "<msg>",
  2575  	}
  2576  	if got, want := r.Error(), "PUT https://example.com: 429 <msg>"; got != want {
  2577  		t.Errorf("AbuseRateLimitError = %q, want %q", got, want)
  2578  	}
  2579  }
  2580  
  2581  func TestAddOptions_QueryValues(t *testing.T) {
  2582  	if _, err := addOptions("yo", ""); err == nil {
  2583  		t.Error("addOptions err = nil, want error")
  2584  	}
  2585  }
  2586  
  2587  func TestBareDo_returnsOpenBody(t *testing.T) {
  2588  	client, mux, _, teardown := setup()
  2589  	defer teardown()
  2590  
  2591  	expectedBody := "Hello from the other side !"
  2592  
  2593  	mux.HandleFunc("/test-url", func(w http.ResponseWriter, r *http.Request) {
  2594  		testMethod(t, r, "GET")
  2595  		fmt.Fprint(w, expectedBody)
  2596  	})
  2597  
  2598  	ctx := context.Background()
  2599  	req, err := client.NewRequest("GET", "test-url", nil)
  2600  	if err != nil {
  2601  		t.Fatalf("client.NewRequest returned error: %v", err)
  2602  	}
  2603  
  2604  	resp, err := client.BareDo(ctx, req)
  2605  	if err != nil {
  2606  		t.Fatalf("client.BareDo returned error: %v", err)
  2607  	}
  2608  
  2609  	got, err := io.ReadAll(resp.Body)
  2610  	if err != nil {
  2611  		t.Fatalf("io.ReadAll returned error: %v", err)
  2612  	}
  2613  	if string(got) != expectedBody {
  2614  		t.Fatalf("Expected %q, got %q", expectedBody, string(got))
  2615  	}
  2616  	if err := resp.Body.Close(); err != nil {
  2617  		t.Fatalf("resp.Body.Close() returned error: %v", err)
  2618  	}
  2619  }
  2620  
  2621  func TestErrorResponse_Marshal(t *testing.T) {
  2622  	testJSONMarshal(t, &ErrorResponse{}, "{}")
  2623  
  2624  	u := &ErrorResponse{
  2625  		Message: "msg",
  2626  		Errors: []Error{
  2627  			{
  2628  				Resource: "res",
  2629  				Field:    "f",
  2630  				Code:     "c",
  2631  				Message:  "msg",
  2632  			},
  2633  		},
  2634  		Block: &ErrorBlock{
  2635  			Reason:    "reason",
  2636  			CreatedAt: &Timestamp{referenceTime},
  2637  		},
  2638  		DocumentationURL: "doc",
  2639  	}
  2640  
  2641  	want := `{
  2642  		"message": "msg",
  2643  		"errors": [
  2644  			{
  2645  				"resource": "res",
  2646  				"field": "f",
  2647  				"code": "c",
  2648  				"message": "msg"
  2649  			}
  2650  		],
  2651  		"block": {
  2652  			"reason": "reason",
  2653  			"created_at": ` + referenceTimeStr + `
  2654  		},
  2655  		"documentation_url": "doc"
  2656  	}`
  2657  
  2658  	testJSONMarshal(t, u, want)
  2659  }
  2660  
  2661  func TestErrorBlock_Marshal(t *testing.T) {
  2662  	testJSONMarshal(t, &ErrorBlock{}, "{}")
  2663  
  2664  	u := &ErrorBlock{
  2665  		Reason:    "reason",
  2666  		CreatedAt: &Timestamp{referenceTime},
  2667  	}
  2668  
  2669  	want := `{
  2670  		"reason": "reason",
  2671  		"created_at": ` + referenceTimeStr + `
  2672  	}`
  2673  
  2674  	testJSONMarshal(t, u, want)
  2675  }
  2676  
  2677  func TestRateLimitError_Marshal(t *testing.T) {
  2678  	testJSONMarshal(t, &RateLimitError{}, "{}")
  2679  
  2680  	u := &RateLimitError{
  2681  		Rate: Rate{
  2682  			Limit:     1,
  2683  			Remaining: 1,
  2684  			Reset:     Timestamp{referenceTime},
  2685  		},
  2686  		Message: "msg",
  2687  	}
  2688  
  2689  	want := `{
  2690  		"Rate": {
  2691  			"limit": 1,
  2692  			"remaining": 1,
  2693  			"reset": ` + referenceTimeStr + `
  2694  		},
  2695  		"message": "msg"
  2696  	}`
  2697  
  2698  	testJSONMarshal(t, u, want)
  2699  }
  2700  
  2701  func TestAbuseRateLimitError_Marshal(t *testing.T) {
  2702  	testJSONMarshal(t, &AbuseRateLimitError{}, "{}")
  2703  
  2704  	u := &AbuseRateLimitError{
  2705  		Message: "msg",
  2706  	}
  2707  
  2708  	want := `{
  2709  		"message": "msg"
  2710  	}`
  2711  
  2712  	testJSONMarshal(t, u, want)
  2713  }
  2714  
  2715  func TestError_Marshal(t *testing.T) {
  2716  	testJSONMarshal(t, &Error{}, "{}")
  2717  
  2718  	u := &Error{
  2719  		Resource: "res",
  2720  		Field:    "field",
  2721  		Code:     "code",
  2722  		Message:  "msg",
  2723  	}
  2724  
  2725  	want := `{
  2726  		"resource": "res",
  2727  		"field": "field",
  2728  		"code": "code",
  2729  		"message": "msg"
  2730  	}`
  2731  
  2732  	testJSONMarshal(t, u, want)
  2733  }
  2734  
  2735  func TestParseTokenExpiration(t *testing.T) {
  2736  	tests := []struct {
  2737  		header string
  2738  		want   Timestamp
  2739  	}{
  2740  		{
  2741  			header: "",
  2742  			want:   Timestamp{},
  2743  		},
  2744  		{
  2745  			header: "this is a garbage",
  2746  			want:   Timestamp{},
  2747  		},
  2748  		{
  2749  			header: "2021-09-03 02:34:04 UTC",
  2750  			want:   Timestamp{time.Date(2021, time.September, 3, 2, 34, 4, 0, time.UTC)},
  2751  		},
  2752  		{
  2753  			header: "2021-09-03 14:34:04 UTC",
  2754  			want:   Timestamp{time.Date(2021, time.September, 3, 14, 34, 4, 0, time.UTC)},
  2755  		},
  2756  		// Some tokens include the timezone offset instead of the timezone.
  2757  		// https://github.com/google/go-github/issues/2649
  2758  		{
  2759  			header: "2023-04-26 20:23:26 +0200",
  2760  			want:   Timestamp{time.Date(2023, time.April, 26, 18, 23, 26, 0, time.UTC)},
  2761  		},
  2762  	}
  2763  
  2764  	for _, tt := range tests {
  2765  		res := &http.Response{
  2766  			Request: &http.Request{},
  2767  			Header:  http.Header{},
  2768  		}
  2769  
  2770  		res.Header.Set(headerTokenExpiration, tt.header)
  2771  		exp := parseTokenExpiration(res)
  2772  		if !exp.Equal(tt.want) {
  2773  			t.Errorf("parseTokenExpiration of %q\nreturned %#v\n    want %#v", tt.header, exp, tt.want)
  2774  		}
  2775  	}
  2776  }
  2777  
  2778  func TestClientCopy_leak_transport(t *testing.T) {
  2779  	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  2780  		w.Header().Set("Content-Type", "application/json")
  2781  		accessToken := r.Header.Get("Authorization")
  2782  		_, _ = fmt.Fprintf(w, `{"login": "%s"}`, accessToken)
  2783  	}))
  2784  	clientPreconfiguredWithURLs, err := NewClient(nil).WithEnterpriseURLs(srv.URL, srv.URL)
  2785  	if err != nil {
  2786  		t.Fatal(err)
  2787  	}
  2788  
  2789  	aliceClient := clientPreconfiguredWithURLs.WithAuthToken("alice")
  2790  	bobClient := clientPreconfiguredWithURLs.WithAuthToken("bob")
  2791  
  2792  	alice, _, err := aliceClient.Users.Get(context.Background(), "")
  2793  	if err != nil {
  2794  		t.Fatal(err)
  2795  	}
  2796  
  2797  	assertNoDiff(t, "Bearer alice", alice.GetLogin())
  2798  
  2799  	bob, _, err := bobClient.Users.Get(context.Background(), "")
  2800  	if err != nil {
  2801  		t.Fatal(err)
  2802  	}
  2803  
  2804  	assertNoDiff(t, "Bearer bob", bob.GetLogin())
  2805  }