use of okhttp3.mockwebserver.internal.tls.SslClient in project okhttp by square.
the class ConnectionCoalescingTest method setUp.
@Before
public void setUp() throws Exception {
rootCa = new HeldCertificate.Builder().serialNumber("1").ca(3).commonName("root").build();
certificate = new HeldCertificate.Builder().issuedBy(rootCa).serialNumber("2").commonName(server.getHostName()).subjectAlternativeName(server.getHostName()).subjectAlternativeName("san.com").subjectAlternativeName("*.wildcard.com").subjectAlternativeName("differentdns.com").build();
serverIps = Dns.SYSTEM.lookup(server.getHostName());
dns.set(server.getHostName(), serverIps);
dns.set("san.com", serverIps);
dns.set("nonsan.com", serverIps);
dns.set("www.wildcard.com", serverIps);
dns.set("differentdns.com", Collections.<InetAddress>emptyList());
SslClient sslClient = new SslClient.Builder().addTrustedCertificate(rootCa.certificate).build();
client = new OkHttpClient.Builder().dns(dns).sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).build();
SslClient serverSslClient = new SslClient.Builder().certificateChain(certificate, rootCa).build();
server.useHttps(serverSslClient.socketFactory, false);
url = server.url("/robots.txt");
}
use of okhttp3.mockwebserver.internal.tls.SslClient in project okhttp by square.
the class ConnectionReuseTest method connectionsAreNotReusedIfSslSocketFactoryChanges.
@Test
public void connectionsAreNotReusedIfSslSocketFactoryChanges() throws Exception {
enableHttps();
server.enqueue(new MockResponse());
server.enqueue(new MockResponse());
Request request = new Request.Builder().url(server.url("/")).build();
Response response = client.newCall(request).execute();
response.body().close();
// This client shares a connection pool but has a different SSL socket factory.
SslClient sslClient2 = new SslClient.Builder().build();
OkHttpClient anotherClient = client.newBuilder().sslSocketFactory(sslClient2.socketFactory, sslClient2.trustManager).build();
// This client fails to connect because the new SSL socket factory refuses.
try {
anotherClient.newCall(request).execute();
fail();
} catch (SSLException expected) {
}
}
use of okhttp3.mockwebserver.internal.tls.SslClient in project okhttp by square.
the class OkHttpAsync method prepare.
@Override
public void prepare(final Benchmark benchmark) {
concurrencyLevel = benchmark.concurrencyLevel;
targetBacklog = benchmark.targetBacklog;
client = new OkHttpClient.Builder().protocols(benchmark.protocols).dispatcher(new Dispatcher(new ThreadPoolExecutor(benchmark.concurrencyLevel, benchmark.concurrencyLevel, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()))).build();
if (benchmark.tls) {
SslClient sslClient = SslClient.localhost();
SSLSocketFactory socketFactory = sslClient.socketFactory;
HostnameVerifier hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String s, SSLSession session) {
return true;
}
};
client = client.newBuilder().sslSocketFactory(socketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build();
}
callback = new Callback() {
@Override
public void onFailure(Call call, IOException e) {
System.out.println("Failed: " + e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
ResponseBody body = response.body();
long total = SynchronousHttpClient.readAllAndClose(body.byteStream());
long finish = System.nanoTime();
if (VERBOSE) {
long start = (Long) response.request().tag();
System.out.printf("Transferred % 8d bytes in %4d ms%n", total, TimeUnit.NANOSECONDS.toMillis(finish - start));
}
requestsInFlight.decrementAndGet();
}
};
}
use of okhttp3.mockwebserver.internal.tls.SslClient in project okhttp by square.
the class CallTest method httpsWithIpAddress.
@Test
public void httpsWithIpAddress() throws Exception {
String localIpAddress = InetAddress.getLoopbackAddress().getHostAddress();
// Create a certificate with an IP address in the subject alt name.
HeldCertificate heldCertificate = new HeldCertificate.Builder().commonName("example.com").subjectAlternativeName(localIpAddress).build();
SslClient sslClient = new SslClient.Builder().certificateChain(heldCertificate.keyPair, heldCertificate.certificate).addTrustedCertificate(heldCertificate.certificate).build();
// Use that certificate on the server and trust it on the client.
server.useHttps(sslClient.socketFactory, false);
client = client.newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(new RecordingHostnameVerifier()).protocols(Collections.singletonList(Protocol.HTTP_1_1)).build();
// Make a request.
server.enqueue(new MockResponse());
HttpUrl url = server.url("/").newBuilder().host(localIpAddress).build();
Request request = new Request.Builder().url(url).build();
executeSynchronously(request).assertCode(200);
// Confirm that the IP address was used in the host header.
RecordedRequest recordedRequest = server.takeRequest();
assertEquals(localIpAddress + ":" + server.getPort(), recordedRequest.getHeader("Host"));
}
use of okhttp3.mockwebserver.internal.tls.SslClient in project okhttp by square.
the class OkUrlFactoryTest method testURLFilterRedirect.
@Test
public void testURLFilterRedirect() throws Exception {
MockWebServer cleartextServer = new MockWebServer();
cleartextServer.enqueue(new MockResponse().setBody("Blocked!"));
final URL blockedURL = cleartextServer.url("/").url();
SslClient contextBuilder = SslClient.localhost();
server.useHttps(contextBuilder.socketFactory, false);
factory.setClient(factory.client().newBuilder().sslSocketFactory(contextBuilder.socketFactory, contextBuilder.trustManager).followSslRedirects(true).build());
factory.setUrlFilter(new URLFilter() {
@Override
public void checkURLPermitted(URL url) throws IOException {
if (blockedURL.equals(url)) {
throw new IOException("Blocked");
}
}
});
server.enqueue(new MockResponse().setResponseCode(302).addHeader("Location: " + blockedURL).setBody("This page has moved"));
URL destination = server.url("/").url();
try {
HttpsURLConnection httpsConnection = (HttpsURLConnection) factory.open(destination);
httpsConnection.getInputStream();
fail("Connection was successful");
} catch (IOException expected) {
}
}
Aggregations