add setDefaultSSLContext #3

This commit is contained in:
Naoyuki Kanezawa
2014-07-08 01:17:01 +09:00
parent fe2fd4413a
commit 3ffe7b326b
2 changed files with 36 additions and 7 deletions

View File

@@ -96,6 +96,8 @@ public class Socket extends Emitter {
public static boolean priorWebsocketSuccess = false; public static boolean priorWebsocketSuccess = false;
private static SSLContext defaultSSLContext;
private boolean secure; private boolean secure;
private boolean upgrade; private boolean upgrade;
private boolean timestampRequests; private boolean timestampRequests;
@@ -123,6 +125,9 @@ public class Socket extends Emitter {
private ReadyState readyState; private ReadyState readyState;
private ScheduledExecutorService heartbeatScheduler = Executors.newSingleThreadScheduledExecutor(); private ScheduledExecutorService heartbeatScheduler = Executors.newSingleThreadScheduledExecutor();
public static void setDefaultSSLContext(SSLContext sslContext) {
defaultSSLContext = sslContext;
}
public Socket() { public Socket() {
this(new Options()); this(new Options());
@@ -167,7 +172,7 @@ public class Socket extends Emitter {
} }
this.secure = opts.secure; this.secure = opts.secure;
this.sslContext = opts.sslContext; this.sslContext = opts.sslContext != null ? opts.sslContext : defaultSSLContext;
this.hostname = opts.hostname != null ? opts.hostname : "localhost"; this.hostname = opts.hostname != null ? opts.hostname : "localhost";
this.port = opts.port != 0 ? opts.port : (this.secure ? 443 : 80); this.port = opts.port != 0 ? opts.port : (this.secure ? 443 : 80);
this.query = opts.query != null ? this.query = opts.query != null ?

View File

@@ -1,6 +1,7 @@
package com.github.nkzawa.engineio.client; package com.github.nkzawa.engineio.client;
import com.github.nkzawa.emitter.Emitter; import com.github.nkzawa.emitter.Emitter;
import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.JUnit4; import org.junit.runners.JUnit4;
@@ -33,6 +34,11 @@ public class SSLConnectionTest extends Connection {
private Socket socket; private Socket socket;
@After
public void tearDown() {
Socket.setDefaultSSLContext(null);
}
@Override @Override
Socket.Options createOptions() { Socket.Options createOptions() {
Socket.Options opts = super.createOptions(); Socket.Options opts = super.createOptions();
@@ -80,11 +86,6 @@ public class SSLConnectionTest extends Connection {
} }
}); });
} }
}).on("error", new Emitter.Listener() {
@Override
public void call(Object... args) {
((Exception)args[0]).printStackTrace();
}
}); });
socket.open(); socket.open();
latch.await(); latch.await();
@@ -119,4 +120,27 @@ public class SSLConnectionTest extends Connection {
socket.open(); socket.open();
latch.await(); latch.await();
} }
@Test(timeout = TIMEOUT)
public void defaultSSLContext() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
Socket.setDefaultSSLContext(createSSLContext());
socket = new Socket(createOptions());
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.on(Socket.EVENT_MESSAGE, new Emitter.Listener() {
@Override
public void call(Object... args) {
assertThat((String) args[0], is("hi"));
socket.close();
latch.countDown();
}
});
}
});
socket.open();
latch.await();
}
} }