Search in sources :

Example 96 with OkHttpClient

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));
}
Also used : Response(okhttp3.Response) MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) Request(okhttp3.Request) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 97 with OkHttpClient

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)));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Request(okhttp3.Request) OkHttpClient(okhttp3.OkHttpClient) ArrayList(java.util.ArrayList) Request(okhttp3.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 98 with OkHttpClient

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();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) MockServer(com.auth0.client.MockServer) Before(org.junit.Before)

Example 99 with OkHttpClient

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);
}
Also used : DefaultSerializer(com.microsoft.graph.serializer.DefaultSerializer) Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) GsonBuilder(com.google.gson.GsonBuilder) GsonBuilder(com.google.gson.GsonBuilder) Request(okhttp3.Request) ILogger(com.microsoft.graph.logger.ILogger)

Example 100 with OkHttpClient

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);
}
Also used : Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) IHttpRequest(com.microsoft.graph.http.IHttpRequest) Request(okhttp3.Request) CoreHttpProvider(com.microsoft.graph.http.CoreHttpProvider) URL(java.net.URL) DefaultSerializer(com.microsoft.graph.serializer.DefaultSerializer) Response(okhttp3.Response) IHttpRequest(com.microsoft.graph.http.IHttpRequest) IAuthenticationProvider(com.microsoft.graph.authentication.IAuthenticationProvider) ILogger(com.microsoft.graph.logger.ILogger) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

OkHttpClient (okhttp3.OkHttpClient)1944 Request (okhttp3.Request)1024 Response (okhttp3.Response)880 IOException (java.io.IOException)567 Test (org.junit.Test)365 Call (okhttp3.Call)290 RequestBody (okhttp3.RequestBody)222 Test (org.junit.jupiter.api.Test)145 Retrofit (retrofit2.Retrofit)138 File (java.io.File)132 HttpUrl (okhttp3.HttpUrl)131 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)128 Callback (okhttp3.Callback)117 JSONObject (org.json.JSONObject)110 ArrayList (java.util.ArrayList)106 ResponseBody (okhttp3.ResponseBody)105 Gson (com.google.gson.Gson)98 MediaType (okhttp3.MediaType)98 List (java.util.List)92 Map (java.util.Map)85