Search in sources :

Example 91 with OkHttpClient

use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project spring-cloud-contract by spring-cloud.

the class OkHttpHttpVerifier method exchange.

@Override
public Response exchange(Request request) {
    String requestContentType = request.contentType();
    OkHttpClient client = new OkHttpClient.Builder().protocols(toProtocol(request.protocol().toString())).build();
    Map<String, String> headers = stringTyped(request.headers());
    if (!request.cookies().isEmpty()) {
        headers.put("Set-Cookie", request.cookies().entrySet().stream().map(e -> e.getKey() + "=" + e.getValue().toString()).collect(Collectors.joining(";")));
    }
    okhttp3.Request req = new okhttp3.Request.Builder().url(url(request)).method(request.method().name(), requestBody(request, requestContentType)).headers(Headers.of(headers)).build();
    try (okhttp3.Response res = client.newCall(req).execute()) {
        return response(res);
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) IOException(java.io.IOException)

Example 92 with OkHttpClient

use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project auth0-java by auth0.

the class CustomRequestTest method shouldThrowOnExecuteFailure.

@Test
public void shouldThrowOnExecuteFailure() throws Exception {
    exception.expect(Auth0Exception.class);
    exception.expectCause(Matchers.<Throwable>instanceOf(IOException.class));
    exception.expectMessage("Failed to execute request");
    OkHttpClient client = mock(OkHttpClient.class);
    Call call = mock(Call.class);
    when(client.newCall(any(okhttp3.Request.class))).thenReturn(call);
    when(call.execute()).thenThrow(IOException.class);
    CustomRequest<Void> request = new CustomRequest<>(client, server.getBaseUrl(), "GET", voidType);
    request.execute();
}
Also used : Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) IOException(java.io.IOException)

Example 93 with OkHttpClient

use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project auth0-java by auth0.

the class EmptyBodyRequestTest method setUp.

@Before
public void setUp() throws Exception {
    server = new MockServer();
    client = new OkHttpClient();
    tokenHolderType = new TypeReference<TokenHolder>() {
    };
}
Also used : OkHttpClient(okhttp3.OkHttpClient) MockServer(com.auth0.client.MockServer) TokenHolder(com.auth0.json.auth.TokenHolder) Before(org.junit.Before)

Example 94 with OkHttpClient

use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project auth0-java by auth0.

the class MultipartRequestTest method setUp.

@Before
public void setUp() throws Exception {
    server = new MockServer();
    client = new OkHttpClient();
    tokenHolderType = new TypeReference<TokenHolder>() {
    };
    listType = new TypeReference<List>() {
    };
    voidType = new TypeReference<Void>() {
    };
}
Also used : OkHttpClient(okhttp3.OkHttpClient) MockServer(com.auth0.client.MockServer) TokenHolder(com.auth0.json.auth.TokenHolder) List(java.util.List)

Example 95 with OkHttpClient

use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project auth0-java by auth0.

the class RateLimitInterceptorTest method shouldRetryRateLimitResponses.

@Test
public void shouldRetryRateLimitResponses() throws Exception {
    OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new RateLimitInterceptor(3)).build();
    server.enqueue(new MockResponse().setResponseCode(429));
    server.enqueue(new MockResponse().setResponseCode(429));
    server.enqueue(new MockResponse().setResponseCode(429));
    server.enqueue(new MockResponse().setResponseCode(200));
    okhttp3.Request request = new Request.Builder().get().url(server.url("/")).build();
    Response resp = client.newCall(request).execute();
    server.takeRequest();
    server.takeRequest();
    server.takeRequest();
    RecordedRequest finalRequest = server.takeRequest();
    assertThat(resp.code(), is(200));
    // verify that a total of 4 requests were made (zero index; 3 failures and one successful retry)
    assertThat(finalRequest.getSequenceNumber(), is(3));
}
Also used : Response(okhttp3.Response) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Request(okhttp3.Request) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

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