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