@@ -538,6 +538,71 @@ func TestDo_rateLimit_noNetworkCall(t *testing.T) {
538
538
}
539
539
}
540
540
541
+ // Ensure *AbuseRateLimitError is returned when the response indicates that
542
+ // the client has triggered an abuse detection mechanism.
543
+ func TestDo_rateLimit_abuseRateLimitError (t * testing.T ) {
544
+ setup ()
545
+ defer teardown ()
546
+
547
+ mux .HandleFunc ("/" , func (w http.ResponseWriter , r * http.Request ) {
548
+ w .Header ().Set ("Content-Type" , "application/json; charset=utf-8" )
549
+ w .WriteHeader (http .StatusForbidden )
550
+ // When the abuse rate limit error is of the "temporarily blocked from content creation" type,
551
+ // there is no "Retry-After" header.
552
+ fmt .Fprintln (w , `{
553
+ "message": "You have triggered an abuse detection mechanism and have been temporarily blocked from content creation. Please retry your request again later.",
554
+ "documentation_url": "https://developer.github.com/v3#abuse-rate-limits"
555
+ }` )
556
+ })
557
+
558
+ req , _ := client .NewRequest ("GET" , "/" , nil )
559
+ _ , err := client .Do (req , nil )
560
+
561
+ if err == nil {
562
+ t .Error ("Expected error to be returned." )
563
+ }
564
+ abuseRateLimitErr , ok := err .(* AbuseRateLimitError )
565
+ if ! ok {
566
+ t .Fatalf ("Expected a *AbuseRateLimitError error; got %#v." , err )
567
+ }
568
+ if got , want := abuseRateLimitErr .RetryAfter , (* time .Duration )(nil ); got != want {
569
+ t .Errorf ("abuseRateLimitErr RetryAfter = %v, want %v" , got , want )
570
+ }
571
+ }
572
+
573
+ // Ensure *AbuseRateLimitError.RetryAfter is parsed correctly.
574
+ func TestDo_rateLimit_abuseRateLimitError_retryAfter (t * testing.T ) {
575
+ setup ()
576
+ defer teardown ()
577
+
578
+ mux .HandleFunc ("/" , func (w http.ResponseWriter , r * http.Request ) {
579
+ w .Header ().Set ("Content-Type" , "application/json; charset=utf-8" )
580
+ w .Header ().Set ("Retry-After" , "123" ) // Retry after value of 123 seconds.
581
+ w .WriteHeader (http .StatusForbidden )
582
+ fmt .Fprintln (w , `{
583
+ "message": "You have triggered an abuse detection mechanism ...",
584
+ "documentation_url": "https://developer.github.com/v3#abuse-rate-limits"
585
+ }` )
586
+ })
587
+
588
+ req , _ := client .NewRequest ("GET" , "/" , nil )
589
+ _ , err := client .Do (req , nil )
590
+
591
+ if err == nil {
592
+ t .Error ("Expected error to be returned." )
593
+ }
594
+ abuseRateLimitErr , ok := err .(* AbuseRateLimitError )
595
+ if ! ok {
596
+ t .Fatalf ("Expected a *AbuseRateLimitError error; got %#v." , err )
597
+ }
598
+ if abuseRateLimitErr .RetryAfter == nil {
599
+ t .Fatalf ("abuseRateLimitErr RetryAfter is nil, expected not-nil" )
600
+ }
601
+ if got , want := * abuseRateLimitErr .RetryAfter , 123 * time .Second ; got != want {
602
+ t .Errorf ("abuseRateLimitErr RetryAfter = %v, want %v" , got , want )
603
+ }
604
+ }
605
+
541
606
func TestDo_noContent (t * testing.T ) {
542
607
setup ()
543
608
defer teardown ()
0 commit comments