Ignore rate limit headers if the response was served from cache.
(t *testing.T)
| 2599 | |
| 2600 | // Ignore rate limit headers if the response was served from cache. |
| 2601 | func TestDo_rateLimit_ignoredFromCache(t *testing.T) { |
| 2602 | t.Parallel() |
| 2603 | client, mux, _ := setup(t) |
| 2604 | |
| 2605 | reset := time.Now().UTC().Add(time.Minute).Round(time.Second) // Rate reset is a minute from now, with 1 second precision. |
| 2606 | |
| 2607 | // By adding the X-From-Cache header we pretend this is served from a cache. |
| 2608 | mux.HandleFunc("/first", func(w http.ResponseWriter, _ *http.Request) { |
| 2609 | w.Header().Set("X-From-Cache", "1") |
| 2610 | w.Header().Set(HeaderRateLimit, "60") |
| 2611 | w.Header().Set(HeaderRateRemaining, "0") |
| 2612 | w.Header().Set(HeaderRateUsed, "60") |
| 2613 | w.Header().Set(HeaderRateReset, fmt.Sprint(reset.Unix())) |
| 2614 | w.Header().Set(HeaderRateResource, "core") |
| 2615 | w.Header().Set("Content-Type", "application/json; charset=utf-8") |
| 2616 | w.WriteHeader(http.StatusForbidden) |
| 2617 | fmt.Fprintln(w, `{ |
| 2618 | "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.)", |
| 2619 | "documentation_url": "https://docs.github.com/en/rest/overview/resources-in-the-rest-api#abuse-rate-limits" |
| 2620 | }`) |
| 2621 | }) |
| 2622 | |
| 2623 | madeNetworkCall := false |
| 2624 | mux.HandleFunc("/second", func(http.ResponseWriter, *http.Request) { |
| 2625 | madeNetworkCall = true |
| 2626 | }) |
| 2627 | |
| 2628 | // First request is made so afterwards we can check the returned rate limit headers were ignored. |
| 2629 | req, _ := client.NewRequest(t.Context(), "GET", "first", nil) |
| 2630 | _, err := client.Do(req, nil) |
| 2631 | if err == nil { |
| 2632 | t.Error("Expected error to be returned.") |
| 2633 | } |
| 2634 | |
| 2635 | // Second request should not be hindered by rate limits. |
| 2636 | req, _ = client.NewRequest(t.Context(), "GET", "second", nil) |
| 2637 | _, err = client.Do(req, nil) |
| 2638 | if err != nil { |
| 2639 | t.Fatalf("Second request failed, even though the rate limits from the cache should've been ignored: %v", err) |
| 2640 | } |
| 2641 | if !madeNetworkCall { |
| 2642 | t.Fatal("Network call was not made, even though the rate limits from the cache should've been ignored") |
| 2643 | } |
| 2644 | } |
| 2645 | |
| 2646 | // Ensure sleeps until the rate limit is reset when the client is rate limited. |
| 2647 | func TestDo_rateLimit_sleepUntilResponseResetLimit(t *testing.T) { |
nothing calls this directly
no test coverage detected
searching dependent graphs…