Ensure *RateLimitError is returned when API rate limit is exceeded.
(t *testing.T)
| 2486 | |
| 2487 | // Ensure *RateLimitError is returned when API rate limit is exceeded. |
| 2488 | func TestDo_rateLimit_rateLimitError(t *testing.T) { |
| 2489 | t.Parallel() |
| 2490 | client, mux, _ := setup(t) |
| 2491 | |
| 2492 | mux.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) { |
| 2493 | w.Header().Set(HeaderRateLimit, "60") |
| 2494 | w.Header().Set(HeaderRateRemaining, "0") |
| 2495 | w.Header().Set(HeaderRateUsed, "60") |
| 2496 | w.Header().Set(HeaderRateReset, "1372700873") |
| 2497 | w.Header().Set(HeaderRateResource, "core") |
| 2498 | w.Header().Set("Content-Type", "application/json; charset=utf-8") |
| 2499 | w.WriteHeader(http.StatusForbidden) |
| 2500 | fmt.Fprintln(w, `{ |
| 2501 | "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.)", |
| 2502 | "documentation_url": "https://docs.github.com/en/rest/overview/resources-in-the-rest-api#abuse-rate-limits" |
| 2503 | }`) |
| 2504 | }) |
| 2505 | |
| 2506 | req, _ := client.NewRequest(t.Context(), "GET", ".", nil) |
| 2507 | _, err := client.Do(req, nil) |
| 2508 | |
| 2509 | if err == nil { |
| 2510 | t.Error("Expected error to be returned.") |
| 2511 | } |
| 2512 | var rateLimitErr *RateLimitError |
| 2513 | if !errors.As(err, &rateLimitErr) { |
| 2514 | t.Fatalf("Expected a *RateLimitError error; got %#v.", err) |
| 2515 | } |
| 2516 | if got, want := rateLimitErr.Rate.Limit, 60; got != want { |
| 2517 | t.Errorf("rateLimitErr rate limit = %v, want %v", got, want) |
| 2518 | } |
| 2519 | if got, want := rateLimitErr.Rate.Remaining, 0; got != want { |
| 2520 | t.Errorf("rateLimitErr rate remaining = %v, want %v", got, want) |
| 2521 | } |
| 2522 | if got, want := rateLimitErr.Rate.Used, 60; got != want { |
| 2523 | t.Errorf("rateLimitErr rate used = %v, want %v", got, want) |
| 2524 | } |
| 2525 | reset := time.Date(2013, time.July, 1, 17, 47, 53, 0, time.UTC) |
| 2526 | if !rateLimitErr.Rate.Reset.UTC().Equal(reset) { |
| 2527 | t.Errorf("rateLimitErr rate reset = %v, want %v", rateLimitErr.Rate.Reset.UTC(), reset) |
| 2528 | } |
| 2529 | if got, want := rateLimitErr.Rate.Resource, "core"; got != want { |
| 2530 | t.Errorf("rateLimitErr rate resource = %v, want %v", got, want) |
| 2531 | } |
| 2532 | } |
| 2533 | |
| 2534 | // Ensure a network call is not made when it's known that API rate limit is still exceeded. |
| 2535 | func TestDo_rateLimit_noNetworkCall(t *testing.T) { |
nothing calls this directly
no test coverage detected
searching dependent graphs…