move package name

This commit is contained in:
nkzawa
2015-08-31 02:34:28 +09:00
parent 411001a04f
commit acddf2f69c
35 changed files with 110 additions and 108 deletions

View File

@@ -0,0 +1,89 @@
package io.socket.engineio.client;
import io.socket.emitter.Emitter;
import io.socket.engineio.client.transports.Polling;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@RunWith(JUnit4.class)
public class BinaryPollingTest extends Connection {
private Socket socket;
@Test(timeout = TIMEOUT)
public void receiveBinaryData() throws InterruptedException {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
final byte[] binaryData = new byte[5];
for (int i = 0; i < binaryData.length; i++) {
binaryData[i] = (byte)i;
}
Socket.Options opts = new Socket.Options();
opts.port = PORT;
opts.transports = new String[] {Polling.NAME};
socket = new Socket(opts);
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.send(binaryData);
socket.on(Socket.EVENT_MESSAGE, new Emitter.Listener() {
@Override
public void call(Object... args) {
if ("hi".equals(args[0])) return;
values.offer(args[0]);
}
});
}
});
socket.open();
assertThat((byte[])values.take(), is(binaryData));
socket.close();
}
@Test(timeout = TIMEOUT)
public void receiveBinaryDataAndMultibyteUTF8String() throws InterruptedException {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
final byte[] binaryData = new byte[5];
for (int i = 0; i < binaryData.length; i++) {
binaryData[i] = (byte)i;
}
final int[] msg = new int[] {0};
Socket.Options opts = new Socket.Options();
opts.port = PORT;
opts.transports = new String[] {Polling.NAME};
socket = new Socket(opts);
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.send(binaryData);
socket.send("cash money €€€");
socket.on(Socket.EVENT_MESSAGE, new Emitter.Listener() {
@Override
public void call(Object... args) {
if ("hi".equals(args[0])) return;
values.offer(args[0]);
msg[0]++;
}
});
}
});
socket.open();
assertThat((byte[])values.take(), is(binaryData));
assertThat((String)values.take(), is("cash money €€€"));
socket.close();
}
}

View File

@@ -0,0 +1,94 @@
package io.socket.engineio.client;
import io.socket.emitter.Emitter;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@RunWith(JUnit4.class)
public class BinaryWSTest extends Connection {
private Socket socket;
@Test(timeout = TIMEOUT)
public void receiveBinaryData() throws InterruptedException {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
final byte[] binaryData = new byte[5];
for (int i = 0; i < binaryData.length; i++) {
binaryData[i] = (byte)i;
}
Socket.Options opts = new Socket.Options();
opts.port = PORT;
socket = new Socket(opts);
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.on(Socket.EVENT_UPGRADE, new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.send(binaryData);
socket.on(Socket.EVENT_MESSAGE, new Emitter.Listener() {
@Override
public void call(Object... args) {
if (args[0] instanceof String) return;
values.offer(args[0]);
}
});
}
});
}
});
socket.open();
assertThat((byte[])values.take(), is(binaryData));
socket.close();
}
@Test(timeout = TIMEOUT)
public void receiveBinaryDataAndMultibyteUTF8String() throws InterruptedException {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
final byte[] binaryData = new byte[5];
for (int i = 0; i < binaryData.length; i++) {
binaryData[i] = (byte)i;
}
final int[] msg = new int[] {0};
Socket.Options opts = new Socket.Options();
opts.port = PORT;
socket = new Socket(opts);
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.on(Socket.EVENT_UPGRADE, new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.send(binaryData);
socket.send("cash money €€€");
socket.on(Socket.EVENT_MESSAGE, new Emitter.Listener() {
@Override
public void call(Object... args) {
if ("hi".equals(args[0])) return;
values.offer(args[0]);
msg[0]++;
}
});
}
});
}
});
socket.open();
assertThat((byte[])values.take(), is(binaryData));
assertThat((String)values.take(), is("cash money €€€"));
socket.close();
}
}

View File

@@ -0,0 +1,97 @@
package io.socket.engineio.client;
import org.junit.After;
import org.junit.Before;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;
import java.util.logging.Logger;
public abstract class Connection {
private static final Logger logger = Logger.getLogger(Socket.class.getName());
final static int TIMEOUT = 10000;
final static int PORT = 3000;
private Process serverProcess;
private ExecutorService serverService;
private Future serverOutout;
private Future serverError;
@Before
public void startServer() throws IOException, InterruptedException {
logger.fine("Starting server ...");
final CountDownLatch latch = new CountDownLatch(1);
serverProcess = Runtime.getRuntime().exec(
"node src/test/resources/server.js", createEnv());
serverService = Executors.newCachedThreadPool();
serverOutout = serverService.submit(new Runnable() {
@Override
public void run() {
BufferedReader reader = new BufferedReader(
new InputStreamReader(serverProcess.getInputStream()));
String line;
try {
line = reader.readLine();
latch.countDown();
do {
logger.fine("SERVER OUT: " + line);
} while ((line = reader.readLine()) != null);
} catch (IOException e) {
logger.warning(e.getMessage());
}
}
});
serverError = serverService.submit(new Runnable() {
@Override
public void run() {
BufferedReader reader = new BufferedReader(
new InputStreamReader(serverProcess.getErrorStream()));
String line;
try {
while ((line = reader.readLine()) != null) {
logger.fine("SERVER ERR: " + line);
}
} catch (IOException e) {
logger.warning(e.getMessage());
}
}
});
latch.await(3000, TimeUnit.MILLISECONDS);
}
@After
public void stopServer() throws InterruptedException {
logger.fine("Stopping server ...");
serverProcess.destroy();
serverOutout.cancel(false);
serverError.cancel(false);
serverService.shutdown();
serverService.awaitTermination(3000, TimeUnit.MILLISECONDS);
}
Socket.Options createOptions() {
Socket.Options opts = new Socket.Options();
opts.port = PORT;
return opts;
}
String[] createEnv() {
Map<String, String> env = new HashMap<String, String>(System.getenv());
env.put("DEBUG", "engine*");
env.put("PORT", String.valueOf(PORT));
String[] _env = new String[env.size()];
int i = 0;
for (String key : env.keySet()) {
_env[i] = key + "=" + env.get(key);
i++;
}
return _env;
}
}

View File

@@ -0,0 +1,245 @@
package io.socket.engineio.client;
import io.socket.emitter.Emitter;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@RunWith(JUnit4.class)
public class ConnectionTest extends Connection {
private Socket socket;
@Test(timeout = TIMEOUT)
public void connectToLocalhost() throws InterruptedException {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
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) {
values.offer(args[0]);
socket.close();
}
});
}
});
socket.open();
assertThat((String)values.take(), is("hi"));
}
@Test(timeout = TIMEOUT)
public void receiveMultibyteUTF8StringsWithPolling() throws InterruptedException {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
socket = new Socket(createOptions());
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.send("cash money €€€");
socket.on(Socket.EVENT_MESSAGE, new Emitter.Listener() {
@Override
public void call(Object... args) {
if ("hi".equals(args[0])) return;
values.offer(args[0]);
socket.close();
}
});
}
});
socket.open();
assertThat((String)values.take(), is("cash money €€€"));
}
@Test(timeout = TIMEOUT)
public void receiveEmoji() throws InterruptedException {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
socket = new Socket(createOptions());
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.send("\uD800-\uDB7F\uDB80-\uDBFF\uDC00-\uDFFF\uE000-\uF8FF");
socket.on(Socket.EVENT_MESSAGE, new Emitter.Listener() {
@Override
public void call(Object... args) {
if ("hi".equals(args[0])) return;
values.offer(args[0]);
socket.close();
}
});
}
});
socket.open();
assertThat((String)values.take(), is("\uD800-\uDB7F\uDB80-\uDBFF\uDC00-\uDFFF\uE000-\uF8FF"));
}
@Test(timeout = TIMEOUT)
public void notSendPacketsIfSocketCloses() throws InterruptedException {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
socket = new Socket(createOptions());
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@Override
public void call(Object... args) {
final boolean[] noPacket = new boolean[] {true};
socket.on(Socket.EVENT_PACKET_CREATE, new Emitter.Listener() {
@Override
public void call(Object... args) {
noPacket[0] = false;
}
});
socket.close();
socket.send("hi");
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
values.offer(noPacket[0]);
}
}, 1200);
}
});
socket.open();
assertThat((Boolean)values.take(), is(true));
}
@Test(timeout = TIMEOUT)
public void deferCloseWhenUpgrading() throws InterruptedException {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
socket = new Socket(createOptions());
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@Override
public void call(Object... args) {
final boolean[] upgraded = new boolean[] {false};
socket.on(Socket.EVENT_UPGRADE, new Emitter.Listener() {
@Override
public void call(Object... args) {
upgraded[0] = true;
}
}).on(Socket.EVENT_UPGRADING, new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.on(Socket.EVENT_CLOSE, new Emitter.Listener() {
@Override
public void call(Object... args) {
values.offer(upgraded[0]);
}
});
socket.close();
}
});
}
});
socket.open();
assertThat((Boolean)values.take(), is(true));
}
@Test(timeout = TIMEOUT)
public void closeOnUpgradeErrorIfClosingIsDeferred() throws InterruptedException {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
socket = new Socket(createOptions());
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@Override
public void call(Object... args) {
final boolean[] upgradError = new boolean[] {false};
socket.on(Socket.EVENT_UPGRADE_ERROR, new Emitter.Listener() {
@Override
public void call(Object... args) {
upgradError[0] = true;
}
}).on(Socket.EVENT_UPGRADING, new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.on(Socket.EVENT_CLOSE, new Emitter.Listener() {
@Override
public void call(Object... args) {
values.offer(upgradError[0]);
}
});
socket.close();
socket.transport.onError("upgrade error", new Exception());
}
});
}
});
socket.open();
assertThat((Boolean) values.take(), is(true));
}
public void notSendPacketsIfClosingIsDeferred() throws InterruptedException {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
socket = new Socket(createOptions());
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@Override
public void call(Object... args) {
final boolean[] noPacket = new boolean[] {true};
socket.on(Socket.EVENT_UPGRADING, new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.on(Socket.EVENT_PACKET_CREATE, new Emitter.Listener() {
@Override
public void call(Object... args) {
noPacket[0] = false;
}
});
socket.close();
socket.send("hi");
}
});
new Timer().schedule(new TimerTask() {
@Override
public void run() {
values.offer(noPacket[0]);
}
}, 1200);
}
});
socket.open();
assertThat((Boolean) values.take(), is(true));
}
@Test(timeout = TIMEOUT)
public void sendAllBufferedPacketsIfClosingIsDeferred() throws InterruptedException {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
socket = new Socket(createOptions());
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.on(Socket.EVENT_UPGRADING, new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.send("hi");
socket.close();
}
}).on(Socket.EVENT_CLOSE, new Emitter.Listener() {
@Override
public void call(Object... args) {
values.offer(socket.writeBuffer.size());
}
});
}
});
socket.open();
assertThat((Integer) values.take(), is(0));
}
}

View File

@@ -0,0 +1,49 @@
package io.socket.engineio.client;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Logger;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@RunWith(JUnit4.class)
public class ExecutionTest extends Connection {
private static final Logger logger = Logger.getLogger(Socket.class.getName());
final static int TIMEOUT = 30 * 1000;
@Test(timeout = TIMEOUT)
public void execConnection() throws InterruptedException, IOException {
exec("io.socket.engineio.client.executions.Connection");
}
@Test(timeout = TIMEOUT)
public void execConnectionFailure() throws InterruptedException, IOException {
exec("io.socket.engineio.client.executions.ConnectionFailure");
}
@Test(timeout = TIMEOUT)
public void execImmediateClose() throws InterruptedException, IOException {
exec("io.socket.engineio.client.executions.ImmediateClose");
}
private void exec(String mainClass) throws InterruptedException, IOException {
Process process = Runtime.getRuntime().exec(String.format("mvn --quiet exec:java" +
" -Dexec.mainClass=%s -Dexec.classpathScope=test", mainClass), createEnv());
BufferedReader input = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
logger.fine("EXEC OUT: " + line);
}
process.waitFor();
assertThat(process.exitValue(), is(0));
}
}

View File

@@ -0,0 +1,147 @@
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;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
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 Socket socket;
@After
public void tearDown() {
Socket.setDefaultSSLContext(null);
}
@Override
Socket.Options createOptions() {
Socket.Options opts = super.createOptions();
opts.secure = true;
return opts;
}
@Override
String[] createEnv() {
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.hostnameVerifier = SSLConnectionTest.hostnameVerifier;
socket = new Socket(opts);
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) {
values.offer(args[0]);
}
});
}
});
socket.open();
assertThat((String)values.take(), is("hi"));
socket.close();
}
@Test(timeout = TIMEOUT)
public void upgrade() throws Exception {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
Socket.Options opts = createOptions();
opts.sslContext = createSSLContext();
opts.hostnameVerifier = SSLConnectionTest.hostnameVerifier;
socket = new Socket(opts);
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.on(Socket.EVENT_UPGRADE, new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.send("hi");
socket.on(Socket.EVENT_MESSAGE, new Emitter.Listener() {
@Override
public void call(Object... args) {
values.offer(args[0]);
}
});
}
});
}
});
socket.open();
assertThat((String)values.take(), is("hi"));
socket.close();
}
@Test(timeout = TIMEOUT)
public void defaultSSLContext() throws Exception {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
Socket.setDefaultSSLContext(createSSLContext());
Socket.setDefaultHostnameVerifier(SSLConnectionTest.hostnameVerifier);
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) {
values.offer(args[0]);
}
});
}
});
socket.open();
assertThat((String)values.take(), is("hi"));
socket.close();
}
}

View File

@@ -0,0 +1,278 @@
package io.socket.engineio.client;
import io.socket.emitter.Emitter;
import io.socket.engineio.client.transports.Polling;
import io.socket.engineio.client.transports.WebSocket;
import io.socket.thread.EventThread;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertThat;
@RunWith(JUnit4.class)
public class ServerConnectionTest extends Connection {
private Socket socket;
@Test(timeout = TIMEOUT)
public void openAndClose() throws URISyntaxException, InterruptedException {
final BlockingQueue<String> events = new LinkedBlockingQueue<String>();
socket = new Socket(createOptions());
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@Override
public void call(Object... args) {
events.offer("onopen");
}
}).on(Socket.EVENT_CLOSE, new Emitter.Listener() {
@Override
public void call(Object... args) {
events.offer("onclose");
}
});
socket.open();
assertThat(events.take(), is("onopen"));
socket.close();
assertThat(events.take(), is("onclose"));
}
@Test(timeout = TIMEOUT)
public void messages() throws URISyntaxException, InterruptedException {
final BlockingQueue<String> events = new LinkedBlockingQueue<String>();
socket = new Socket(createOptions());
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.send("hello");
}
}).on(Socket.EVENT_MESSAGE, new Emitter.Listener() {
@Override
public void call(Object... args) {
events.offer((String) args[0]);
}
});
socket.open();
assertThat(events.take(), is("hi"));
assertThat(events.take(), is("hello"));
socket.close();
}
@Test(timeout = TIMEOUT)
public void handshake() throws URISyntaxException, InterruptedException {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
socket = new Socket(createOptions());
socket.on(Socket.EVENT_HANDSHAKE, new Emitter.Listener() {
@Override
public void call(Object... args) {
values.offer(args);
}
});
socket.open();
Object[] args = (Object[])values.take();
assertThat(args.length, is(1));
HandshakeData data = (HandshakeData)args[0];
assertThat(data.sid, is(notNullValue()));
assertThat(data.upgrades, is(not(emptyArray())));
assertThat(data.pingTimeout, is(greaterThan((long) 0)));
assertThat(data.pingInterval, is(greaterThan((long) 0)));
socket.close();
}
@Test(timeout = TIMEOUT)
public void upgrade() throws URISyntaxException, InterruptedException {
final BlockingQueue<Object[]> events = new LinkedBlockingQueue<Object[]>();
socket = new Socket(createOptions());
socket.on(Socket.EVENT_UPGRADING, new Emitter.Listener() {
@Override
public void call(Object... args) {
events.offer(args);
}
});
socket.on(Socket.EVENT_UPGRADE, new Emitter.Listener() {
@Override
public void call(Object... args) {
events.offer(args);
}
});
socket.open();
Object[] args1 = events.take();
assertThat(args1.length, is(1));
assertThat(args1[0], is(instanceOf(Transport.class)));
Transport transport1 = (Transport)args1[0];
assertThat(transport1, is(notNullValue()));
Object[] args2 = events.take();
assertThat(args2.length, is(1));
assertThat(args2[0], is(instanceOf(Transport.class)));
Transport transport2 = (Transport)args2[0];
assertThat(transport2, is(notNullValue()));
socket.close();
}
@Test(timeout = TIMEOUT)
public void pollingHeaders() throws URISyntaxException, InterruptedException {
final BlockingQueue<String> messages = new LinkedBlockingQueue<String>();
Socket.Options opts = createOptions();
opts.transports = new String[] {Polling.NAME};
socket = new Socket(opts);
socket.on(Socket.EVENT_TRANSPORT, new Emitter.Listener() {
@Override
public void call(Object... args) {
Transport transport = (Transport)args[0];
transport.on(Transport.EVENT_REQUEST_HEADERS, new Emitter.Listener() {
@Override
public void call(Object... args) {
@SuppressWarnings("unchecked")
Map<String, List<String>> headers = (Map<String, List<String>>)args[0];
headers.put("X-EngineIO", Arrays.asList("foo"));
}
}).on(Transport.EVENT_RESPONSE_HEADERS, new Emitter.Listener() {
@Override
public void call(Object... args) {
@SuppressWarnings("unchecked")
Map<String, List<String>> headers = (Map<String, List<String>>)args[0];
List<String> values = headers.get("X-EngineIO");
messages.offer(values.get(0));
messages.offer(values.get(1));
}
});
}
});
socket.open();
assertThat(messages.take(), is("foo"));
assertThat(messages.take(), is("hi"));
socket.close();
}
@Test(timeout = TIMEOUT)
public void websocketHandshakeHeaders() throws URISyntaxException, InterruptedException {
final BlockingQueue<String> messages = new LinkedBlockingQueue<String>();
Socket.Options opts = createOptions();
opts.transports = new String[] {WebSocket.NAME};
socket = new Socket(opts);
socket.on(Socket.EVENT_TRANSPORT, new Emitter.Listener() {
@Override
public void call(Object... args) {
Transport transport = (Transport)args[0];
transport.on(Transport.EVENT_REQUEST_HEADERS, new Emitter.Listener() {
@Override
public void call(Object... args) {
@SuppressWarnings("unchecked")
Map<String, List<String>> headers = (Map<String, List<String>>)args[0];
headers.put("X-EngineIO", Arrays.asList("foo"));
}
}).on(Transport.EVENT_RESPONSE_HEADERS, new Emitter.Listener() {
@Override
public void call(Object... args) {
@SuppressWarnings("unchecked")
Map<String, List<String>> headers = (Map<String, List<String>>)args[0];
List<String> values = headers.get("X-EngineIO");
messages.offer(values.get(0));
messages.offer(values.get(1));
}
});
}
});
socket.open();
assertThat(messages.take(), is("hi"));
assertThat(messages.take(), is("foo"));
socket.close();
}
@Test(timeout = TIMEOUT)
public void rememberWebsocket() throws InterruptedException {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
EventThread.exec(new Runnable() {
@Override
public void run() {
final Socket socket = new Socket(createOptions());
socket.on(Socket.EVENT_UPGRADE, new Emitter.Listener() {
@Override
public void call(Object... args) {
Transport transport = (Transport) args[0];
socket.close();
if (WebSocket.NAME.equals(transport.name)) {
Socket.Options opts = new Socket.Options();
opts.port = PORT;
opts.rememberUpgrade = true;
Socket socket2 = new Socket(opts);
socket2.open();
values.offer(socket2.transport.name);
socket2.close();
}
}
});
socket.open();
values.offer(socket.transport.name);
}
});
assertThat((String)values.take(), is(Polling.NAME));
assertThat((String)values.take(), is(WebSocket.NAME));
}
@Test(timeout = TIMEOUT)
public void notRememberWebsocket() throws InterruptedException {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
EventThread.exec(new Runnable() {
@Override
public void run() {
final Socket socket = new Socket(createOptions());
socket.on(Socket.EVENT_UPGRADE, new Emitter.Listener() {
@Override
public void call(Object... args) {
Transport transport = (Transport)args[0];
socket.close();
if (WebSocket.NAME.equals(transport.name)) {
Socket.Options opts = new Socket.Options();
opts.port = PORT;
opts.rememberUpgrade = false;
final Socket socket2 = new Socket(opts);
socket2.open();
values.offer(socket2.transport.name);
socket2.close();
}
}
});
socket.open();
values.offer(socket.transport.name);
}
});
assertThat((String) values.take(), is(Polling.NAME));
assertThat((String)values.take(), is(not(WebSocket.NAME)));
}
}

View File

@@ -0,0 +1,51 @@
package io.socket.engineio.client;
import io.socket.engineio.client.transports.Polling;
import io.socket.engineio.client.transports.WebSocket;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@RunWith(JUnit4.class)
public class SocketTest {
@Test
public void filterUpgrades() {
Socket.Options opts = new Socket.Options();
opts.transports = new String[] {Polling.NAME};
Socket socket = new Socket(opts);
List<String> upgrades = new ArrayList<String>() {{
add(Polling.NAME);
add(WebSocket.NAME);
}};
List<String> expected = new ArrayList<String>() {{add(Polling.NAME);}};
assertThat(socket.filterUpgrades(upgrades), is(expected));
}
public void properlyParseHttpUriWithoutPort() throws URISyntaxException {
Socket client = new Socket("http://localhost");
assertThat(client.port, is(80));
}
public void properlyParseHttpsUriWithoutPort() throws URISyntaxException {
Socket client = new Socket("http://localhost");
assertThat(client.port, is(443));
}
public void properlyParseWssUriWithoutPort() throws URISyntaxException {
Socket client = new Socket("http://localhost");
assertThat(client.port, is(443));
}
public void properlyParseWssUriWithPort() throws URISyntaxException {
Socket client = new Socket("http://localhost:2020");
assertThat(client.port, is(2020));
}
}

View File

@@ -0,0 +1,147 @@
package io.socket.engineio.client;
import io.socket.engineio.client.transports.PollingXHR;
import io.socket.engineio.client.transports.WebSocket;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.util.HashMap;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@RunWith(JUnit4.class)
public class TransportTest {
// NOTE: tests for the rememberUpgrade option are on ServerConnectionTest.
@Test
public void uri() {
Transport.Options opt = new Transport.Options();
opt.path = "/engine.io";
opt.hostname = "localhost";
opt.secure = false;
opt.query = new HashMap<String, String>() {{
put("sid", "test");
}};
opt.timestampRequests = false;
Polling polling = new Polling(opt);
assertThat(polling.uri(), containsString("http://localhost/engine.io?sid=test"));
}
@Test
public void uriWithDefaultPort() {
Transport.Options opt = new Transport.Options();
opt.path ="/engine.io";
opt.hostname = "localhost";
opt.secure = false;
opt.query = new HashMap<String, String>() {{
put("sid", "test");
}};
opt.port = 80;
opt.timestampRequests = false;
Polling polling = new Polling(opt);
assertThat(polling.uri(), containsString("http://localhost/engine.io?sid=test"));
}
@Test
public void uriWithPort() {
Transport.Options opt = new Transport.Options();
opt.path ="/engine.io";
opt.hostname = "localhost";
opt.secure = false;
opt.query = new HashMap<String, String>() {{
put("sid", "test");
}};
opt.port = 3000;
opt.timestampRequests = false;
Polling polling = new Polling(opt);
assertThat(polling.uri(), containsString("http://localhost:3000/engine.io?sid=test"));
}
@Test
public void httpsUriWithDefaultPort() {
Transport.Options opt = new Transport.Options();
opt.path ="/engine.io";
opt.hostname = "localhost";
opt.secure = true;
opt.query = new HashMap<String, String>() {{
put("sid", "test");
}};
opt.port = 443;
opt.timestampRequests = false;
Polling polling = new Polling(opt);
assertThat(polling.uri(), containsString("https://localhost/engine.io?sid=test"));
}
@Test
public void timestampedUri() {
Transport.Options opt = new Transport.Options();
opt.path ="/engine.io";
opt.hostname = "localhost";
opt.timestampParam = "t";
opt.timestampRequests = true;
Polling polling = new Polling(opt);
assertThat(polling.uri().matches("http://localhost/engine.io\\?(j=[0-9]+&)?t=[0-9]+-[0-9]+"), is(true));
}
@Test
public void wsUri() {
Transport.Options opt = new Transport.Options();
opt.path ="/engine.io";
opt.hostname = "test";
opt.secure = false;
opt.query = new HashMap<String, String>() {{
put("transport", "websocket");
}};
opt.timestampRequests = false;
WS ws = new WS(opt);
assertThat(ws.uri(), is("ws://test/engine.io?transport=websocket"));
}
@Test
public void wssUri() {
Transport.Options opt = new Transport.Options();
opt.path = "/engine.io";
opt.hostname = "test";
opt.secure = true;
opt.timestampRequests = false;
WS ws = new WS(opt);
assertThat(ws.uri(), is("wss://test/engine.io"));
}
@Test
public void wsTimestampedUri() {
Transport.Options opt = new Transport.Options();
opt.path = "/engine.io";
opt.hostname = "localhost";
opt.timestampParam = "woot";
opt.timestampRequests = true;
WS ws = new WS(opt);
assertThat(ws.uri().matches("ws://localhost/engine.io\\?woot=[0-9]+"), is(true));
}
class Polling extends PollingXHR {
public Polling(Options opts) {
super(opts);
}
public String uri() {
return super.uri();
}
}
class WS extends WebSocket {
public WS(Transport.Options opts) {
super(opts);
}
public String uri() {
return super.uri();
}
}
}

View File

@@ -0,0 +1,21 @@
package io.socket.engineio.client.executions;
import io.socket.emitter.Emitter;
import io.socket.engineio.client.Socket;
import java.net.URISyntaxException;
public class Connection {
public static void main(String[] args) throws URISyntaxException {
final Socket socket = new Socket("http://localhost:" + System.getenv("PORT"));
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("open");
socket.close();
}
});
socket.open();
}
}

View File

@@ -0,0 +1,27 @@
package io.socket.engineio.client.executions;
import io.socket.emitter.Emitter;
import io.socket.engineio.client.Socket;
import java.net.URISyntaxException;
public class ConnectionFailure {
public static void main(String[] args) throws URISyntaxException {
int port = Integer.parseInt(System.getenv("PORT"));
port++;
final Socket socket = new Socket("http://localhost:" + port);
socket.on(Socket.EVENT_CLOSE, new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("close");
}
}).on(Socket.EVENT_ERROR, new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("error");
}
});
socket.open();
}
}

View File

@@ -0,0 +1,26 @@
package io.socket.engineio.client.executions;
import io.socket.emitter.Emitter;
import io.socket.engineio.client.Socket;
import java.net.URISyntaxException;
public class ImmediateClose {
public static void main(String[] args) throws URISyntaxException {
final Socket socket = new Socket("http://localhost:" + System.getenv("PORT"));
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("open");
}
}).on(Socket.EVENT_CLOSE, new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("close");
}
});
socket.open();
socket.close();
}
}