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