2121import java .util .Map ;
2222
2323import static com .auth0 .client .MockServer .*;
24+ import com .auth0 .exception .RateLimitException ;
2425import static org .hamcrest .MatcherAssert .assertThat ;
2526import static org .hamcrest .Matchers .*;
2627import static org .mockito .ArgumentMatchers .any ;
@@ -301,5 +302,55 @@ public void shouldParsePlainTextErrorResponse() throws Exception {
301302 assertThat (authException .getValue ("non_existing_key" ), is (nullValue ()));
302303 assertThat (authException .getStatusCode (), is (400 ));
303304 }
305+
306+ @ Test
307+ public void shouldParseRateLimitsHeaders () throws Exception {
308+ CustomRequest <List > request = new CustomRequest <>(client , server .getBaseUrl (), "GET" , listType );
309+ server .rateLimitReachedResponse (100 , 10 , 5 );
310+ Exception exception = null ;
311+ try {
312+ request .execute ();
313+ server .takeRequest ();
314+ } catch (Exception e ) {
315+ exception = e ;
316+ }
317+ assertThat (exception , is (notNullValue ()));
318+ assertThat (exception , is (instanceOf (RateLimitException .class )));
319+ assertThat (exception .getCause (), is (nullValue ()));
320+ assertThat (exception .getMessage (), is ("Request failed with status code 429: Rate limit reached" ));
321+ RateLimitException rateLimitException = (RateLimitException ) exception ;
322+ assertThat (rateLimitException .getDescription (), is ("Rate limit reached" ));
323+ assertThat (rateLimitException .getError (), is (nullValue ()));
324+ assertThat (rateLimitException .getValue ("non_existing_key" ), is (nullValue ()));
325+ assertThat (rateLimitException .getStatusCode (), is (429 ));
326+ assertThat (rateLimitException .getLimit (), is (100L ));
327+ assertThat (rateLimitException .getRemaining (), is (10L ));
328+ assertThat (rateLimitException .getReset (), is (5L ));
329+ }
330+
331+ @ Test
332+ public void shouldDefaultRateLimitsHeadersWhenMissing () throws Exception {
333+ CustomRequest <List > request = new CustomRequest <>(client , server .getBaseUrl (), "GET" , listType );
334+ server .rateLimitReachedResponse (-1 , -1 , -1 );
335+ Exception exception = null ;
336+ try {
337+ request .execute ();
338+ server .takeRequest ();
339+ } catch (Exception e ) {
340+ exception = e ;
341+ }
342+ assertThat (exception , is (notNullValue ()));
343+ assertThat (exception , is (instanceOf (RateLimitException .class )));
344+ assertThat (exception .getCause (), is (nullValue ()));
345+ assertThat (exception .getMessage (), is ("Request failed with status code 429: Rate limit reached" ));
346+ RateLimitException rateLimitException = (RateLimitException ) exception ;
347+ assertThat (rateLimitException .getDescription (), is ("Rate limit reached" ));
348+ assertThat (rateLimitException .getError (), is (nullValue ()));
349+ assertThat (rateLimitException .getValue ("non_existing_key" ), is (nullValue ()));
350+ assertThat (rateLimitException .getStatusCode (), is (429 ));
351+ assertThat (rateLimitException .getLimit (), is (-1L ));
352+ assertThat (rateLimitException .getRemaining (), is (-1L ));
353+ assertThat (rateLimitException .getReset (), is (-1L ));
354+ }
304355
305- }
356+ }
0 commit comments