add new Options for feeding custom OkHttpClient
This commit is contained in:
@@ -1,37 +1,31 @@
|
||||
package com.github.nkzawa.engineio.client;
|
||||
|
||||
import com.github.nkzawa.emitter.Emitter;
|
||||
import com.squareup.okhttp.OkHttpClient;
|
||||
import com.squareup.okhttp.internal.SslContextBuilder;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
import javax.net.ssl.KeyManagerFactory;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.TrustManagerFactory;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.KeyStore;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
@RunWith(JUnit4.class)
|
||||
public class SSLConnectionTest extends Connection {
|
||||
|
||||
static {
|
||||
// for test on localhost
|
||||
javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
|
||||
new javax.net.ssl.HostnameVerifier(){
|
||||
public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) {
|
||||
return hostname.equals("localhost");
|
||||
}
|
||||
});
|
||||
}
|
||||
static HostnameVerifier hostnameVerifier = new javax.net.ssl.HostnameVerifier(){
|
||||
public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
private Socket socket;
|
||||
|
||||
@@ -52,28 +46,13 @@ public class SSLConnectionTest extends Connection {
|
||||
return new String[] {"DEBUG=engine*", "PORT=" + PORT, "SSL=1"};
|
||||
}
|
||||
|
||||
SSLContext createSSLContext() throws GeneralSecurityException, IOException {
|
||||
KeyStore ks = KeyStore.getInstance("JKS");
|
||||
File file = new File("src/test/resources/keystore.jks");
|
||||
ks.load(new FileInputStream(file), "password".toCharArray());
|
||||
|
||||
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
|
||||
kmf.init(ks, "password".toCharArray());
|
||||
|
||||
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
|
||||
tmf.init(ks);
|
||||
|
||||
SSLContext sslContext = SSLContext.getInstance("TLS");
|
||||
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
|
||||
return sslContext;
|
||||
}
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
public void connect() throws Exception {
|
||||
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
|
||||
|
||||
Socket.Options opts = createOptions();
|
||||
opts.sslContext = createSSLContext();
|
||||
opts.sslContext = SslContextBuilder.localhost();
|
||||
opts.okHttpClient = new OkHttpClient().setHostnameVerifier(hostnameVerifier);
|
||||
socket = new Socket(opts);
|
||||
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
|
||||
@Override
|
||||
@@ -97,7 +76,8 @@ public class SSLConnectionTest extends Connection {
|
||||
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
|
||||
|
||||
Socket.Options opts = createOptions();
|
||||
opts.sslContext = createSSLContext();
|
||||
opts.sslContext = SslContextBuilder.localhost();
|
||||
opts.okHttpClient = new OkHttpClient().setHostnameVerifier(hostnameVerifier);
|
||||
socket = new Socket(opts);
|
||||
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
|
||||
@Override
|
||||
@@ -126,8 +106,10 @@ public class SSLConnectionTest extends Connection {
|
||||
public void defaultSSLContext() throws Exception {
|
||||
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
|
||||
|
||||
Socket.setDefaultSSLContext(createSSLContext());
|
||||
socket = new Socket(createOptions());
|
||||
Socket.Options opts = createOptions();
|
||||
Socket.setDefaultSSLContext(SslContextBuilder.localhost());
|
||||
opts.okHttpClient = new OkHttpClient().setHostnameVerifier(hostnameVerifier);
|
||||
socket = new Socket(opts);
|
||||
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
|
||||
Reference in New Issue
Block a user