diff --git a/src/main/java/com/auth0/exception/RateLimitException.java b/src/main/java/com/auth0/exception/RateLimitException.java index c5e79e81..738a941f 100644 --- a/src/main/java/com/auth0/exception/RateLimitException.java +++ b/src/main/java/com/auth0/exception/RateLimitException.java @@ -1,5 +1,7 @@ package com.auth0.exception; +import java.util.Map; + /** * Represents a server error when a rate limit has been exceeded. *
@@ -16,6 +18,13 @@ public class RateLimitException extends APIException {
private static final int STATUS_CODE_TOO_MANY_REQUEST = 429;
+ public RateLimitException(long limit, long remaining, long reset, Map request = new BaseRequest<>(client, tokenProvider, server.getBaseUrl(), HttpMethod.GET, listType);
- server.rateLimitReachedResponse(100, 10, 5);
+ server.rateLimitReachedResponse(100, 10, 5, RATE_LIMIT_ERROR);
Exception exception = null;
try {
request.execute().getBody();
@@ -414,10 +414,10 @@ public void shouldParseRateLimitsHeaders() {
assertThat(exception, Matchers.is(notNullValue()));
assertThat(exception, Matchers.is(Matchers.instanceOf(RateLimitException.class)));
assertThat(exception.getCause(), Matchers.is(nullValue()));
- assertThat(exception.getMessage(), Matchers.is("Request failed with status code 429: Rate limit reached"));
+ assertThat(exception.getMessage(), Matchers.is("Request failed with status code 429: Global limit has been reached"));
RateLimitException rateLimitException = (RateLimitException) exception;
- assertThat(rateLimitException.getDescription(), Matchers.is("Rate limit reached"));
- assertThat(rateLimitException.getError(), Matchers.is(nullValue()));
+ assertThat(rateLimitException.getDescription(), Matchers.is("Global limit has been reached"));
+ assertThat(rateLimitException.getError(), Matchers.is("too_many_requests"));
assertThat(rateLimitException.getValue("non_existing_key"), Matchers.is(nullValue()));
assertThat(rateLimitException.getStatusCode(), Matchers.is(429));
assertThat(rateLimitException.getLimit(), Matchers.is(100L));
@@ -426,7 +426,7 @@ public void shouldParseRateLimitsHeaders() {
}
@Test
- public void shouldDefaultRateLimitsHeadersWhenMissing() {
+ public void shouldDefaultRateLimitsHeadersWhenMissing() throws Exception {
BaseRequest
request = new BaseRequest<>(client, tokenProvider, server.getBaseUrl(), HttpMethod.GET, listType);
server.rateLimitReachedResponse(-1, -1, -1);
Exception exception = null;
diff --git a/src/test/java/com/auth0/net/MultipartRequestTest.java b/src/test/java/com/auth0/net/MultipartRequestTest.java
index 670a704c..9eb1f61f 100644
--- a/src/test/java/com/auth0/net/MultipartRequestTest.java
+++ b/src/test/java/com/auth0/net/MultipartRequestTest.java
@@ -335,7 +335,7 @@ public void shouldParsePlainTextErrorResponse() throws Exception {
}
@Test
- public void shouldParseRateLimitsHeaders() {
+ public void shouldParseRateLimitsHeaders() throws Exception {
MultipartRequest
request = new MultipartRequest<>(client, tokenProvider, server.getBaseUrl(), HttpMethod.POST, listType);
request.addPart("non_empty", "body");
server.rateLimitReachedResponse(100, 10, 5);
@@ -361,7 +361,7 @@ public void shouldParseRateLimitsHeaders() {
}
@Test
- public void shouldDefaultRateLimitsHeadersWhenMissing() {
+ public void shouldDefaultRateLimitsHeadersWhenMissing() throws Exception {
MultipartRequest
request = new MultipartRequest<>(client, tokenProvider, server.getBaseUrl(), HttpMethod.POST, listType);
request.addPart("non_empty", "body");
server.rateLimitReachedResponse(-1, -1, -1);
diff --git a/src/test/java/com/auth0/net/RateLimitInterceptorTest.java b/src/test/java/com/auth0/net/RateLimitInterceptorTest.java
index f737578c..f05ea72b 100644
--- a/src/test/java/com/auth0/net/RateLimitInterceptorTest.java
+++ b/src/test/java/com/auth0/net/RateLimitInterceptorTest.java
@@ -108,9 +108,7 @@ public void shouldReturnResponseWhenMaxRetriesHit() throws Exception {
int index = 0;
for (int i = 0; i < maxRetries + 1; i++) {
- System.out.println("about to take request " + i);
index = server.takeRequest().getSequenceNumber();
- System.out.println("took request " + i);
}
assertThat(response.code(), is(429));
diff --git a/src/test/java/com/auth0/net/client/DefaultHttpClientTest.java b/src/test/java/com/auth0/net/client/DefaultHttpClientTest.java
index 0066a581..a9f1e7e7 100644
--- a/src/test/java/com/auth0/net/client/DefaultHttpClientTest.java
+++ b/src/test/java/com/auth0/net/client/DefaultHttpClientTest.java
@@ -462,6 +462,31 @@ public void alwaysCloseResponseOnSuccessfulRequest() throws IOException {
verify(response, times(1)).close();
}
+ @Test
+ public void alwaysCloseResponseOnSuccessfulRequestAsync() throws Exception {
+ OkHttpClient okClient = Mockito.mock(OkHttpClient.class);
+ okhttp3.Response response = Mockito.mock(okhttp3.Response.class);
+ Call call = Mockito.mock(Call.class);
+
+ doReturn(call).when(okClient).newCall(any());
+
+ doAnswer(invocation -> {
+ ((Callback) invocation.getArgument(0)).onResponse(call, response);
+ return null;
+ }).when(call).enqueue(any(Callback.class));
+
+ Headers headers = Mockito.mock(Headers.class);
+ when(response.headers()).thenReturn(headers);
+
+ DefaultHttpClient client = new DefaultHttpClient(okClient);
+ Auth0HttpRequest request = Auth0HttpRequest.newBuilder(server.url("/users/").toString(), HttpMethod.POST)
+ .withBody(HttpRequestBody.create("application/json", "{}".getBytes()))
+ .build();
+
+ client.sendRequestAsync(request).get();
+ verify(response, times(1)).close();
+ }
+
@Test
public void closesResponseOnAPIError() throws Exception {
okhttp3.Response response = Mockito.mock(okhttp3.Response.class);
@@ -490,6 +515,36 @@ public void closesResponseOnAPIError() throws Exception {
verify(response, times(1)).close();
}
+ @Test
+ public void closesResponseOnAPIErrorAsync() throws Exception {
+ OkHttpClient okClient = Mockito.mock(OkHttpClient.class);
+ okhttp3.Response response = Mockito.mock(okhttp3.Response.class);
+ Call call = Mockito.mock(Call.class);
+
+ doReturn(call).when(okClient).newCall(any());
+
+ doAnswer(invocation -> {
+ ((Callback) invocation.getArgument(0)).onResponse(call, response);
+ return null;
+ }).when(call).enqueue(any(Callback.class));
+
+ Headers headers = Mockito.mock(Headers.class);
+ when(response.headers()).thenReturn(headers);
+
+ DefaultHttpClient client = new DefaultHttpClient(okClient);
+ Auth0HttpRequest request = Auth0HttpRequest.newBuilder(server.url("/users/").toString(), HttpMethod.POST)
+ .withBody(HttpRequestBody.create("application/json", "{}".getBytes()))
+ .build();
+
+ MockResponse mockResponse = new MockResponse()
+ .setResponseCode(500)
+ .setBody("server error");
+
+ server.enqueue(mockResponse);
+ client.sendRequestAsync(request).get();
+ verify(response, times(1)).close();
+ }
+
@Test
public void makesFormBodyPostRequest() throws Exception {
Map