Revert "add new Options for feeding custom OkHttpClient"

This reverts commit 464b7302d4.
This commit is contained in:
Yu-Hsuan Lin
2015-05-02 00:59:34 +08:00
parent ae1c521645
commit 822aaebc35
5 changed files with 38 additions and 34 deletions

View File

@@ -1,31 +1,37 @@
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 HostnameVerifier hostnameVerifier = new javax.net.ssl.HostnameVerifier(){
public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) {
return true;
}
};
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");
}
});
}
private Socket socket;
@@ -46,13 +52,28 @@ 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 = SslContextBuilder.localhost();
opts.okHttpClient = new OkHttpClient().setHostnameVerifier(hostnameVerifier);
opts.sslContext = createSSLContext();
socket = new Socket(opts);
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@Override
@@ -76,8 +97,7 @@ public class SSLConnectionTest extends Connection {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
Socket.Options opts = createOptions();
opts.sslContext = SslContextBuilder.localhost();
opts.okHttpClient = new OkHttpClient().setHostnameVerifier(hostnameVerifier);
opts.sslContext = createSSLContext();
socket = new Socket(opts);
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@Override
@@ -106,10 +126,8 @@ public class SSLConnectionTest extends Connection {
public void defaultSSLContext() throws Exception {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
Socket.Options opts = createOptions();
Socket.setDefaultSSLContext(SslContextBuilder.localhost());
opts.okHttpClient = new OkHttpClient().setHostnameVerifier(hostnameVerifier);
socket = new Socket(opts);
Socket.setDefaultSSLContext(createSSLContext());
socket = new Socket(createOptions());
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@Override
public void call(Object... args) {