Skip to content

Commit 6146660

Browse files
committed
more test cases
1 parent ef380a5 commit 6146660

2 files changed

Lines changed: 38 additions & 9 deletions

File tree

src/test/java/com/auth0/client/MockServer.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ public class MockServer {
7676
public static final String MGMT_EMPTY_LIST = "src/test/resources/mgmt/empty_list.json";
7777
public static final String MGMT_JOB_POST_VERIFICATION_EMAIL = "src/test/resources/mgmt/post_verification_email.json";
7878

79-
8079
private final MockWebServer server;
8180

8281
public MockServer() throws Exception {
@@ -107,13 +106,18 @@ public void jsonResponse(String path, int statusCode) throws IOException {
107106
.setBody(readTextFile(path));
108107
server.enqueue(response);
109108
}
110-
109+
111110
public void rateLimitReachedResponse(long limit, long remaining, long reset) throws IOException {
112-
MockResponse response = new MockResponse()
113-
.setResponseCode(429)
114-
.addHeader("X-RateLimit-Limit", String.valueOf(limit))
115-
.addHeader("X-RateLimit-Remaining", String.valueOf(remaining))
116-
.addHeader("X-RateLimit-Reset", String.valueOf(reset));
111+
MockResponse response = new MockResponse().setResponseCode(429);
112+
if (limit != -1) {
113+
response.addHeader("X-RateLimit-Limit", String.valueOf(limit));
114+
}
115+
if (remaining != -1) {
116+
response.addHeader("X-RateLimit-Remaining", String.valueOf(remaining));
117+
}
118+
if (reset != -1) {
119+
response.addHeader("X-RateLimit-Reset", String.valueOf(reset));
120+
}
117121
server.enqueue(response);
118122
}
119123

src/test/java/com/auth0/net/CustomRequestTest.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ public void shouldParsePlainTextErrorResponse() throws Exception {
306306
@Test
307307
public void shouldReceiveRateLimitsResponse() throws Exception {
308308
CustomRequest<List> request = new CustomRequest<>(client, server.getBaseUrl(), "GET", listType);
309-
server.rateLimitReachedResponse(100, 10, 5);
309+
server.rateLimitReachedResponse(100, -1, 5);
310310
Exception exception = null;
311311
try {
312312
request.execute();
@@ -324,8 +324,33 @@ public void shouldReceiveRateLimitsResponse() throws Exception {
324324
assertThat(rateLimitException.getValue("non_existing_key"), is(nullValue()));
325325
assertThat(rateLimitException.getStatusCode(), is(429));
326326
assertThat(rateLimitException.getLimit(), is(100L));
327-
assertThat(rateLimitException.getRemaining(), is(10L));
327+
assertThat(rateLimitException.getRemaining(), is(-1L));
328328
assertThat(rateLimitException.getReset(), is(5L));
329329
}
330+
331+
@Test
332+
public void shouldReceiveDefaultsRateLimitsResponse() 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+
}
330355

331356
}

0 commit comments

Comments
 (0)