add tests

This commit is contained in:
nkzawa
2017-07-14 12:40:22 +09:00
parent 3d98ed9fe9
commit 2ec4167eb8
4 changed files with 70 additions and 14 deletions

View File

@@ -79,12 +79,12 @@ public class Socket extends Emitter {
}};
/*package*/ String id;
/*package*/ String query;
private volatile boolean connected;
private int ids;
private String nsp;
private Manager io;
private String query;
private Map<Integer, Ack> acks = new HashMap<Integer, Ack>();
private Queue<On.Handle> subs;
private final Queue<List<Object>> receiveBuffer = new LinkedList<List<Object>>();

View File

@@ -82,7 +82,7 @@ public abstract class Connection {
}
Socket client(String path) throws URISyntaxException {
return IO.socket(path, createOptions());
return client(path, createOptions());
}
Socket client(IO.Options opts) throws URISyntaxException {

View File

@@ -2,6 +2,8 @@ package io.socket.client;
import io.socket.emitter.Emitter;
import io.socket.util.Optional;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@@ -23,7 +25,7 @@ public class SocketTest extends Connection {
private Socket socket;
@Test(timeout = TIMEOUT)
public void shouldHaveAnAccessibleSocketIdEqualToTheEngineIOSocketId() throws URISyntaxException, InterruptedException {
public void shouldHaveAnAccessibleSocketIdEqualToServerSideSocketId() throws URISyntaxException, InterruptedException {
final BlockingQueue<Optional> values = new LinkedBlockingQueue<Optional>();
socket = client();
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@@ -41,6 +43,25 @@ public class SocketTest extends Connection {
socket.disconnect();
}
@Test(timeout = TIMEOUT)
public void shouldHaveAnAccessibleSocketIdEqualToServerSideSocketIdOnCustomNamespace() throws URISyntaxException, InterruptedException {
final BlockingQueue<Optional> values = new LinkedBlockingQueue<Optional>();
socket = client("/foo");
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... objects) {
values.offer(Optional.ofNullable(socket.id()));
}
});
socket.connect();
@SuppressWarnings("unchecked")
Optional<String> id = values.take();
assertThat(id.isPresent(), is(true));
assertThat(id.get(), is("/foo#" + socket.io().engine.id()));
socket.disconnect();
}
@Test(timeout = TIMEOUT)
public void clearsSocketIdUponDisconnection() throws URISyntaxException, InterruptedException {
final BlockingQueue<Optional> values = new LinkedBlockingQueue<Optional>();
@@ -170,19 +191,46 @@ public class SocketTest extends Connection {
}
@Test(timeout = TIMEOUT)
public void shouldStoreQueryStringAsAProperty() throws URISyntaxException, InterruptedException {
IO.Options opts = new IO.Options();
opts.query = "a=b";
Socket socket = IO.socket(this.uri() + "/abc", opts);
public void shouldAcceptAQueryStringOnDefaultNamespace() throws URISyntaxException, InterruptedException, JSONException {
final BlockingQueue<Optional> values = new LinkedBlockingQueue<Optional>();
Socket socket2 = IO.socket(this.uri() + "/abc?b=c&d=e");
socket = client("/?c=d");
socket.emit("getHandshake", new Ack() {
@Override
public void call(Object... args) {
JSONObject handshake = (JSONObject)args[0];
values.offer(Optional.ofNullable(handshake));
}
});
socket.connect();
IO.Options opts3 = new IO.Options();
opts.query = "%26a=%26%3D%3Fa";
Socket socket3 = IO.socket(this.uri() + "/abc", opts);
@SuppressWarnings("unchecked")
Optional<JSONObject> handshake = values.take();
assertThat(handshake.get().getJSONObject("query").getString("c"), is("d"));
assertThat(socket.query, is("a=b"));
assertThat(socket2.query, is("b=c&d=e"));
assertThat(socket3.query, is("%26a=%26%3D%3Fa"));
socket.disconnect();
}
@Test(timeout = TIMEOUT)
public void shouldAcceptAQueryString() throws URISyntaxException, InterruptedException, JSONException {
final BlockingQueue<Optional> values = new LinkedBlockingQueue<Optional>();
socket = client("/abc?b=c&d=e");
socket.on("handshake", new Emitter.Listener() {
@Override
public void call(Object... args) {
JSONObject handshake = (JSONObject)args[0];
values.offer(Optional.ofNullable(handshake));
}
});
socket.connect();
@SuppressWarnings("unchecked")
Optional<JSONObject> handshake = values.take();
JSONObject query = handshake.get().getJSONObject("query");
assertThat(query.getString("b"), is("c"));
assertThat(query.getString("d"), is("e"));
socket.disconnect();
}
}

View File

@@ -34,6 +34,10 @@ io.of('/asd').on('connection', function() {
// register namespace
});
io.of('/abc').on('connection', function(socket) {
socket.emit('handshake', socket.handshake);
});
io.of(nsp).on('connection', function(socket) {
socket.send('hello client');
@@ -96,6 +100,10 @@ io.of(nsp).on('connection', function(socket) {
socket.on('error', function() {
console.log('error: ', arguments);
});
socket.on('getHandshake', function(cb) {
cb(socket.handshake);
});
});