@@ -1852,6 +1852,47 @@ func TestDo_rateLimit_abuseRateLimitError_xRateLimitReset(t *testing.T) {
1852
1852
}
1853
1853
}
1854
1854
1855
+ // Ensure *AbuseRateLimitError.RetryAfter respect a max duration if specified.
1856
+ func TestDo_rateLimit_abuseRateLimitError_maxDuration (t * testing.T ) {
1857
+ t .Parallel ()
1858
+ client , mux , _ := setup (t )
1859
+ // specify a max retry after duration of 1 min
1860
+ client .MaxSecondaryRateLimitRetryAfterDuration = 60 * time .Second
1861
+
1862
+ // x-ratelimit-reset value of 1h into the future, to make sure we are way over the max wait time duration.
1863
+ blockUntil := time .Now ().Add (1 * time .Hour ).Unix ()
1864
+
1865
+ mux .HandleFunc ("/" , func (w http.ResponseWriter , r * http.Request ) {
1866
+ w .Header ().Set ("Content-Type" , "application/json; charset=utf-8" )
1867
+ w .Header ().Set (headerRateReset , strconv .Itoa (int (blockUntil )))
1868
+ w .Header ().Set (headerRateRemaining , "1" ) // set remaining to a value > 0 to distinct from a primary rate limit
1869
+ w .WriteHeader (http .StatusForbidden )
1870
+ fmt .Fprintln (w , `{
1871
+ "message": "You have triggered an abuse detection mechanism ...",
1872
+ "documentation_url": "https://docs.github.com/en/rest/overview/resources-in-the-rest-api#abuse-rate-limits"
1873
+ }` )
1874
+ })
1875
+
1876
+ req , _ := client .NewRequest ("GET" , "." , nil )
1877
+ ctx := context .Background ()
1878
+ _ , err := client .Do (ctx , req , nil )
1879
+
1880
+ if err == nil {
1881
+ t .Error ("Expected error to be returned." )
1882
+ }
1883
+ abuseRateLimitErr , ok := err .(* AbuseRateLimitError )
1884
+ if ! ok {
1885
+ t .Fatalf ("Expected a *AbuseRateLimitError error; got %#v." , err )
1886
+ }
1887
+ if abuseRateLimitErr .RetryAfter == nil {
1888
+ t .Fatalf ("abuseRateLimitErr RetryAfter is nil, expected not-nil" )
1889
+ }
1890
+ // check that the retry after is set to be the max allowed duration
1891
+ if got , want := * abuseRateLimitErr .RetryAfter , client .MaxSecondaryRateLimitRetryAfterDuration ; got != want {
1892
+ t .Errorf ("abuseRateLimitErr RetryAfter = %v, want %v" , got , want )
1893
+ }
1894
+ }
1895
+
1855
1896
func TestDo_noContent (t * testing.T ) {
1856
1897
t .Parallel ()
1857
1898
client , mux , _ := setup (t )
0 commit comments