use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project auth0-java by auth0.
the class RateLimitInterceptorTest method shouldNotRetryIfMaxRetriesIsZero.
@Test
public void shouldNotRetryIfMaxRetriesIsZero() throws Exception {
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new RateLimitInterceptor(0)).build();
server.enqueue(new MockResponse().setResponseCode(429));
okhttp3.Request request = new Request.Builder().get().url(server.url("/")).build();
Response resp = client.newCall(request).execute();
assertThat(resp.code(), is(429));
// verify that only one request was made (no retries)
assertThat(server.takeRequest().getSequenceNumber(), is(0));
}
use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project auth0-java by auth0.
the class RateLimitInterceptorTest method shouldBackOffOnRetries.
@Test
public void shouldBackOffOnRetries() throws Exception {
int maxRetries = 10;
List<Long> retryTimings = new ArrayList<>();
RateLimitInterceptor interceptor = new RateLimitInterceptor(maxRetries, c -> {
// keep a sequential list of the elapsed time since last execution request (retry delay)
retryTimings.add(c.getElapsedAttemptTime().toMillis());
});
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
MockResponse mockResponse = new MockResponse().setResponseCode(429);
for (int i = 0; i < maxRetries + 1; i++) {
server.enqueue(mockResponse);
}
okhttp3.Request request = new Request.Builder().get().url(server.url("/")).build();
client.newCall(request).execute();
// Verify that the last retry attempt is at least 3x greater than the first
assertThat(retryTimings.get(maxRetries - 1), greaterThan(retryTimings.get(0) * 3L));
// Verify that the final retry is close to the maximum delay of 1000ms, within variance from jitter
assertThat(retryTimings.get(maxRetries - 1).doubleValue(), closeTo(1000, 220));
// Basic checks that retry retryTimings are increasing as retry count increases
// Different retry attempts are compared to account for backoff growth as well as random jitter
assertThat(retryTimings.get(2), greaterThan(retryTimings.get(0)));
assertThat(retryTimings.get(5), greaterThan(retryTimings.get(2)));
}
use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project auth0-java by auth0.
the class TokenRequestTest method setUp.
@Before
public void setUp() throws Exception {
client = new OkHttpClient();
server = new MockServer();
}
use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project msgraph-sdk-java-core by microsoftgraph.
the class CoreHttpProviderTests method setCoreHttpProvider.
/**
* Configures the http provider for test cases
* @param toSerialize The object to serialize
*/
private void setCoreHttpProvider(final Object toSerialize) throws IOException {
final OkHttpClient mClient = mock(OkHttpClient.class);
final Call mCall = mock(Call.class);
when(mClient.newCall(any(Request.class))).thenReturn(mCall);
when(mCall.execute()).thenReturn(new Response.Builder().code(503).message("Service Unavailable").protocol(Protocol.HTTP_1_1).request(new Request.Builder().url("https://graph.microsoft.com/v1.0/me").build()).addHeader("Content-type", "application/json").body(ResponseBody.create(new GsonBuilder().setPrettyPrinting().create().toJson(toSerialize), MediaType.parse("application/json"))).build());
mProvider = new CoreHttpProvider(new DefaultSerializer(mock(ILogger.class)), mock(ILogger.class), mClient);
}
use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project msgraph-sdk-java-core by microsoftgraph.
the class BatchRequestContentTest method executeBatchTest.
@Test
void executeBatchTest() throws Throwable {
final BatchRequestContent content = new BatchRequestContent();
IHttpRequest requestStep = mock(IHttpRequest.class);
when(requestStep.getRequestUrl()).thenReturn(new URL(testurl));
final String stepId = content.addBatchRequestStep(requestStep);
final OkHttpClient mHttpClient = mock(OkHttpClient.class);
final Call mCall = mock(Call.class);
when(mHttpClient.newCall(any(Request.class))).thenReturn(mCall);
final CoreHttpProvider mHttpProvider = new CoreHttpProvider(new DefaultSerializer(mock(ILogger.class)), mock(ILogger.class), mHttpClient);
final IBaseClient<?> mClient = BaseClient.builder().authenticationProvider(mock(IAuthenticationProvider.class)).httpProvider(mHttpProvider).buildClient();
final Response mResponse = new Response.Builder().request(new Request.Builder().url("https://graph.microsoft.com/v1.0/$batch").build()).code(200).protocol(Protocol.HTTP_1_1).message("OK").addHeader("Content-type", "application/json").body(ResponseBody.create("{\"responses\": [{\"id\": \"" + stepId + "\",\"status\": 200,\"body\": null}]}", MediaType.parse("application/json"))).build();
when(mCall.execute()).thenReturn(mResponse);
final BatchResponseContent batchResponse = mClient.batch().buildRequest().post(content);
assertNotNull(mClient.batch().buildRequest().postAsync(content));
final BatchResponseStep<?> response = batchResponse.getResponseById(stepId);
assertNotNull(response);
}
Aggregations