77import com .auth0 .exception .Auth0Exception ;
88import com .auth0 .exception .RateLimitException ;
99import com .auth0 .json .auth .TokenHolder ;
10+ import com .auth0 .json .auth .TokenQuotaLimit ;
1011import com .auth0 .net .client .*;
1112import com .fasterxml .jackson .core .JsonParseException ;
1213import com .fasterxml .jackson .core .JsonProcessingException ;
@@ -197,6 +198,8 @@ public void shouldAddHeaders() throws Exception {
197198 request .addParameter ("non_empty" , "body" );
198199 request .addHeader ("Extra-Info" , "this is a test" );
199200 request .addHeader ("Authorization" , "Bearer my_access_token" );
201+ request .addHeader ("X-Client-Quota" , getTokenQuotaString ());
202+ request .addHeader ("X-Organization-Quota" , getTokenQuotaString ());
200203
201204 server .jsonResponse (AUTH_TOKENS , 200 );
202205 request .execute ().getBody ();
@@ -425,6 +428,33 @@ public void shouldParseRateLimitException() throws Exception {
425428 assertThat (rateLimitException .getReset (), Matchers .is (5L ));
426429 }
427430
431+ @ Test
432+ public void shouldParseRateLimitExceptionWithZeroRemaining () throws Exception {
433+ BaseRequest <List > request = new BaseRequest <>(client , tokenProvider , server .getBaseUrl (), HttpMethod .GET , listType );
434+ server .rateLimitReachedResponse (100 , 0 , 5 , RATE_LIMIT_ERROR , getTokenQuotaString (), getTokenQuotaString ());
435+ Exception exception = null ;
436+ try {
437+ request .execute ().getBody ();
438+ server .takeRequest ();
439+ } catch (Exception e ) {
440+ exception = e ;
441+ }
442+ assertThat (exception , Matchers .is (notNullValue ()));
443+ assertThat (exception , Matchers .is (Matchers .instanceOf (RateLimitException .class )));
444+ assertThat (exception .getCause (), Matchers .is (nullValue ()));
445+ assertThat (exception .getMessage (), Matchers .is ("Request failed with status code 429: Global limit has been reached" ));
446+ RateLimitException rateLimitException = (RateLimitException ) exception ;
447+ assertThat (rateLimitException .getDescription (), Matchers .is ("Global limit has been reached" ));
448+ assertThat (rateLimitException .getError (), Matchers .is ("too_many_requests" ));
449+ assertThat (rateLimitException .getValue ("non_existing_key" ), Matchers .is (nullValue ()));
450+ assertThat (rateLimitException .getStatusCode (), Matchers .is (429 ));
451+ assertThat (rateLimitException .getLimit (), Matchers .is (100L ));
452+ assertThat (rateLimitException .getRemaining (), Matchers .is (0L ));
453+ assertThat (rateLimitException .getReset (), Matchers .is (5L ));
454+ assertThat (rateLimitException .getClientQuotaLimit ().getPerDay ().getQuota (), Matchers .is (getTokenQuota ().getPerDay ().getQuota ()));
455+ assertThat (rateLimitException .getOrganizationQuotaLimit ().getPerDay ().getQuota (), Matchers .is (getTokenQuota ().getPerDay ().getQuota ()));
456+ }
457+
428458 @ Test
429459 public void shouldDefaultRateLimitsHeadersWhenMissing () throws Exception {
430460 BaseRequest <List > request = new BaseRequest <>(client , tokenProvider , server .getBaseUrl (), HttpMethod .GET , listType );
@@ -448,5 +478,31 @@ public void shouldDefaultRateLimitsHeadersWhenMissing() throws Exception {
448478 assertThat (rateLimitException .getLimit (), Matchers .is (-1L ));
449479 assertThat (rateLimitException .getRemaining (), Matchers .is (-1L ));
450480 assertThat (rateLimitException .getReset (), Matchers .is (-1L ));
481+ assertThat (rateLimitException .getClientQuotaLimit (), Matchers .is (nullValue ()));
482+ assertThat (rateLimitException .getOrganizationQuotaLimit (), Matchers .is (nullValue ()));
483+ }
484+
485+ private TokenQuotaBucket getTokenQuota () {
486+ TokenQuotaLimit perHourLimit = new TokenQuotaLimit (100 , 80 , 3600 );
487+ TokenQuotaLimit perDayLimit = new TokenQuotaLimit (100 , 90 , 86400 );
488+ return new TokenQuotaBucket (perHourLimit , perDayLimit );
489+ }
490+
491+ public String getTokenQuotaString () {
492+ TokenQuotaLimit perHourLimit = new TokenQuotaLimit (100 , 80 , 3600 );
493+ TokenQuotaLimit perDayLimit = new TokenQuotaLimit (100 , 90 , 86400 );
494+
495+ StringBuilder builder = new StringBuilder ();
496+
497+ builder .append (String .format ("b=per_hour;q=%d;r=%d;t=%d" ,
498+ perHourLimit .getQuota (), perHourLimit .getRemaining (), perHourLimit .getTime ()));
499+
500+ if (builder .length () > 0 ) {
501+ builder .append ("," );
502+ }
503+ builder .append (String .format ("b=per_day;q=%d;r=%d;t=%d" ,
504+ perDayLimit .getQuota (), perDayLimit .getRemaining (), perDayLimit .getTime ()));
505+
506+ return builder .toString ();
451507 }
452508}
0 commit comments