Options for inject okhttp Call/WebSocket factory
This commit is contained in:
@@ -17,7 +17,7 @@ public class ExecutionTest extends Connection {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(Socket.class.getName());
|
||||
|
||||
final static int TIMEOUT = 30 * 1000;
|
||||
final static int TIMEOUT = 100 * 1000;
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
public void execConnection() throws InterruptedException, IOException {
|
||||
|
||||
@@ -1,15 +1,10 @@
|
||||
package io.socket.engineio.client;
|
||||
|
||||
import io.socket.emitter.Emitter;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
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;
|
||||
@@ -18,23 +13,63 @@ import java.security.KeyStore;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
import javax.net.ssl.KeyManagerFactory;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.TrustManagerFactory;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
|
||||
import io.socket.emitter.Emitter;
|
||||
import okhttp3.OkHttpClient;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
@RunWith(JUnit4.class)
|
||||
public class SSLConnectionTest extends Connection {
|
||||
|
||||
static HostnameVerifier hostnameVerifier = new javax.net.ssl.HostnameVerifier(){
|
||||
public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) {
|
||||
return hostname.equals("localhost");
|
||||
}
|
||||
};
|
||||
private static OkHttpClient sOkHttpClient;
|
||||
|
||||
private Socket socket;
|
||||
|
||||
static {
|
||||
try {
|
||||
prepareOkHttpClient();
|
||||
} catch (GeneralSecurityException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static void prepareOkHttpClient() 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("TLSv1");
|
||||
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
|
||||
|
||||
sOkHttpClient = new OkHttpClient.Builder()
|
||||
.hostnameVerifier(new javax.net.ssl.HostnameVerifier(){
|
||||
public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) {
|
||||
return hostname.equals("localhost");
|
||||
}
|
||||
})
|
||||
.sslSocketFactory(sslContext.getSocketFactory(),
|
||||
(X509TrustManager) tmf.getTrustManagers()[0])
|
||||
.build();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
Socket.setDefaultSSLContext(null);
|
||||
Socket.setDefaultOkHttpCallFactory(null);
|
||||
Socket.setDefaultOkHttpWebSocketFactory(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -49,29 +84,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("TLSv1");
|
||||
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.hostnameVerifier = SSLConnectionTest.hostnameVerifier;
|
||||
opts.callFactory = sOkHttpClient;
|
||||
opts.webSocketFactory = sOkHttpClient;
|
||||
socket = new Socket(opts);
|
||||
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
|
||||
@Override
|
||||
@@ -95,8 +114,8 @@ public class SSLConnectionTest extends Connection {
|
||||
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
|
||||
|
||||
Socket.Options opts = createOptions();
|
||||
opts.sslContext = createSSLContext();
|
||||
opts.hostnameVerifier = SSLConnectionTest.hostnameVerifier;
|
||||
opts.callFactory = sOkHttpClient;
|
||||
opts.webSocketFactory = sOkHttpClient;
|
||||
socket = new Socket(opts);
|
||||
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
|
||||
@Override
|
||||
@@ -125,8 +144,8 @@ public class SSLConnectionTest extends Connection {
|
||||
public void defaultSSLContext() throws Exception {
|
||||
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
|
||||
|
||||
Socket.setDefaultSSLContext(createSSLContext());
|
||||
Socket.setDefaultHostnameVerifier(SSLConnectionTest.hostnameVerifier);
|
||||
Socket.setDefaultOkHttpWebSocketFactory(sOkHttpClient);
|
||||
Socket.setDefaultOkHttpCallFactory(sOkHttpClient);
|
||||
socket = new Socket(createOptions());
|
||||
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
|
||||
@Override
|
||||
|
||||
@@ -163,8 +163,8 @@ public class ServerConnectionTest extends Connection {
|
||||
});
|
||||
socket.open();
|
||||
|
||||
assertThat(messages.take(), is("foo"));
|
||||
assertThat(messages.take(), is("hi"));
|
||||
assertThat(messages.take(), is("foo"));
|
||||
socket.close();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user