use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project recurly-client-java by recurly.
the class BaseClientTest method testNetworkError.
@Test
public void testNetworkError() throws IOException {
final Call mCall = mock(Call.class);
Answer answer = (i) -> {
return mCall;
};
when(mCall.execute()).thenThrow(new IOException());
OkHttpClient mockOkHttpClient = MockClient.getMockOkHttpClient(answer);
final MockClient client = new MockClient("apiKey", mockOkHttpClient);
assertThrows(NetworkException.class, () -> {
client.getResource("code-aaron");
});
}
use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project recurly-client-java by recurly.
the class BaseClientTest method testBadMethodError.
@Test
public void testBadMethodError() throws IOException {
final Call mCall = mock(Call.class);
Answer answer = (i) -> {
return mCall;
};
when(mCall.execute()).thenThrow(new IOException());
OkHttpClient mockOkHttpClient = MockClient.getMockOkHttpClient(answer);
final MockClient client = new MockClient("apiKey", mockOkHttpClient);
assertThrows(IllegalArgumentException.class, () -> {
client.badRequestMethod();
});
}
use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project recurly-client-java by recurly.
the class BaseClientTest method testTransactionError.
@Test
public void testTransactionError() throws IOException {
final Call mCall = mock(Call.class);
Answer answer = (i) -> {
return mCall;
};
when(mCall.execute()).thenReturn(MockClient.buildResponse(422, "Unprocessable Entity", getErrorResponse("transaction")));
OkHttpClient mockOkHttpClient = MockClient.getMockOkHttpClient(answer);
final MockClient client = new MockClient("apiKey", mockOkHttpClient);
TransactionException t = assertThrows(TransactionException.class, () -> {
client.removeResource("code-aaron");
});
assertEquals("mbca9aaao6xr", t.getError().getTransactionError().getTransactionId());
}
use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project concord-plugins by walmartlabs.
the class PuppetTaskTest method testWrongHostName.
/**
* Tests {@link PuppetHostnameVerifier} by executing a request with loopback
* address 'localhost' but configuring a hostname verifier to only trust
* '127.0.0.1'
*/
@Test
public void testWrongHostName() throws Exception {
stubForOk();
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectTimeout(10000L, TimeUnit.SECONDS).readTimeout(10000L, TimeUnit.SECONDS).writeTimeout(10000L, TimeUnit.SECONDS);
// no certificate validation for wiremock
final TrustManager[] tms = new TrustManager[] { new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[0];
}
} };
final SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
sslContext.init(null, tms, new java.security.SecureRandom());
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
// Request to execute
Request.Builder rBuilder = new Request.Builder().url(httpsRule.baseUrl() + "/ok").post(RequestBody.create(MediaType.parse("application/json"), "{}"));
Request request = rBuilder.build();
// Create a hostname mismatch
clientBuilder.sslSocketFactory(sslSocketFactory, (X509TrustManager) tms[0]).hostnameVerifier(new PuppetHostnameVerifier("https://127.0.0.1"));
OkHttpClient client = clientBuilder.build();
try {
client.newCall(request).execute();
fail("Hostname mismatch should result in SSLPeerUnverifiedException");
} catch (SSLPeerUnverifiedException expected) {
// that's a good thing
} catch (Exception e) {
log.info("Hostname mismatch should result in SSLPeerUnverifiedException");
fail("Hostname mismatch should result in SSLPeerUnverifiedException");
}
// Do it again, but with the right hostname
clientBuilder.sslSocketFactory(sslSocketFactory, (X509TrustManager) tms[0]).hostnameVerifier(new PuppetHostnameVerifier("https://localhost"));
client = clientBuilder.build();
try {
client.newCall(request).execute();
} catch (Exception e) {
fail("Hostnames match but test still failed: " + e.getMessage());
}
}
use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project devspaces-images by redhat-developer.
the class KubernetesClientFactory method create.
/**
* Creates instance of {@link KubernetesClient} that uses an {@link OkHttpClient} instance derived
* from the shared {@code httpClient} instance in which interceptors are overridden to
* authenticate with the credentials (user/password or Oauth token) contained in the {@code
* config} parameter.
*/
protected BaseKubernetesClient<?> create(Config config) {
OkHttpClient clientHttpClient = httpClient.newBuilder().authenticator(Authenticator.NONE).build();
OkHttpClient.Builder builder = clientHttpClient.newBuilder();
builder.interceptors().clear();
builder.addInterceptor(buildKubernetesInterceptor(config)).addInterceptor(new ImpersonatorInterceptor(config));
initializeRequestTracing(builder);
clientHttpClient = builder.build();
return new UnclosableKubernetesClient(clientHttpClient, config);
}
Aggregations