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);
}
}
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();
}
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>() {
};
}
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>() {
};
}
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));
}
Aggregations