Skip to content

Commit 2ec4167

Browse files
committed
add tests
1 parent 3d98ed9 commit 2ec4167

File tree

4 files changed

+70
-14
lines changed

4 files changed

+70
-14
lines changed

src/main/java/io/socket/client/Socket.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ public class Socket extends Emitter {
7979
}};
8080

8181
/*package*/ String id;
82-
/*package*/ String query;
8382

8483
private volatile boolean connected;
8584
private int ids;
8685
private String nsp;
8786
private Manager io;
87+
private String query;
8888
private Map<Integer, Ack> acks = new HashMap<Integer, Ack>();
8989
private Queue<On.Handle> subs;
9090
private final Queue<List<Object>> receiveBuffer = new LinkedList<List<Object>>();

src/test/java/io/socket/client/Connection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Socket client() throws URISyntaxException {
8282
}
8383

8484
Socket client(String path) throws URISyntaxException {
85-
return IO.socket(path, createOptions());
85+
return client(path, createOptions());
8686
}
8787

8888
Socket client(IO.Options opts) throws URISyntaxException {

src/test/java/io/socket/client/SocketTest.java

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import io.socket.emitter.Emitter;
44
import io.socket.util.Optional;
5+
import org.json.JSONException;
6+
import org.json.JSONObject;
57
import org.junit.Test;
68
import org.junit.runner.RunWith;
79
import org.junit.runners.JUnit4;
@@ -23,7 +25,7 @@ public class SocketTest extends Connection {
2325
private Socket socket;
2426

2527
@Test(timeout = TIMEOUT)
26-
public void shouldHaveAnAccessibleSocketIdEqualToTheEngineIOSocketId() throws URISyntaxException, InterruptedException {
28+
public void shouldHaveAnAccessibleSocketIdEqualToServerSideSocketId() throws URISyntaxException, InterruptedException {
2729
final BlockingQueue<Optional> values = new LinkedBlockingQueue<Optional>();
2830
socket = client();
2931
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@@ -41,6 +43,25 @@ public void call(Object... objects) {
4143
socket.disconnect();
4244
}
4345

46+
@Test(timeout = TIMEOUT)
47+
public void shouldHaveAnAccessibleSocketIdEqualToServerSideSocketIdOnCustomNamespace() throws URISyntaxException, InterruptedException {
48+
final BlockingQueue<Optional> values = new LinkedBlockingQueue<Optional>();
49+
socket = client("/foo");
50+
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
51+
@Override
52+
public void call(Object... objects) {
53+
values.offer(Optional.ofNullable(socket.id()));
54+
}
55+
});
56+
socket.connect();
57+
58+
@SuppressWarnings("unchecked")
59+
Optional<String> id = values.take();
60+
assertThat(id.isPresent(), is(true));
61+
assertThat(id.get(), is("/foo#" + socket.io().engine.id()));
62+
socket.disconnect();
63+
}
64+
4465
@Test(timeout = TIMEOUT)
4566
public void clearsSocketIdUponDisconnection() throws URISyntaxException, InterruptedException {
4667
final BlockingQueue<Optional> values = new LinkedBlockingQueue<Optional>();
@@ -170,19 +191,46 @@ public void call(Object... objects) {
170191
}
171192

172193
@Test(timeout = TIMEOUT)
173-
public void shouldStoreQueryStringAsAProperty() throws URISyntaxException, InterruptedException {
174-
IO.Options opts = new IO.Options();
175-
opts.query = "a=b";
176-
Socket socket = IO.socket(this.uri() + "/abc", opts);
194+
public void shouldAcceptAQueryStringOnDefaultNamespace() throws URISyntaxException, InterruptedException, JSONException {
195+
final BlockingQueue<Optional> values = new LinkedBlockingQueue<Optional>();
177196

178-
Socket socket2 = IO.socket(this.uri() + "/abc?b=c&d=e");
197+
socket = client("/?c=d");
198+
socket.emit("getHandshake", new Ack() {
199+
@Override
200+
public void call(Object... args) {
201+
JSONObject handshake = (JSONObject)args[0];
202+
values.offer(Optional.ofNullable(handshake));
203+
}
204+
});
205+
socket.connect();
206+
207+
@SuppressWarnings("unchecked")
208+
Optional<JSONObject> handshake = values.take();
209+
assertThat(handshake.get().getJSONObject("query").getString("c"), is("d"));
179210

180-
IO.Options opts3 = new IO.Options();
181-
opts.query = "%26a=%26%3D%3Fa";
182-
Socket socket3 = IO.socket(this.uri() + "/abc", opts);
211+
socket.disconnect();
212+
}
213+
214+
@Test(timeout = TIMEOUT)
215+
public void shouldAcceptAQueryString() throws URISyntaxException, InterruptedException, JSONException {
216+
final BlockingQueue<Optional> values = new LinkedBlockingQueue<Optional>();
183217

184-
assertThat(socket.query, is("a=b"));
185-
assertThat(socket2.query, is("b=c&d=e"));
186-
assertThat(socket3.query, is("%26a=%26%3D%3Fa"));
218+
socket = client("/abc?b=c&d=e");
219+
socket.on("handshake", new Emitter.Listener() {
220+
@Override
221+
public void call(Object... args) {
222+
JSONObject handshake = (JSONObject)args[0];
223+
values.offer(Optional.ofNullable(handshake));
224+
}
225+
});
226+
socket.connect();
227+
228+
@SuppressWarnings("unchecked")
229+
Optional<JSONObject> handshake = values.take();
230+
JSONObject query = handshake.get().getJSONObject("query");
231+
assertThat(query.getString("b"), is("c"));
232+
assertThat(query.getString("d"), is("e"));
233+
234+
socket.disconnect();
187235
}
188236
}

src/test/resources/server.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ io.of('/asd').on('connection', function() {
3434
// register namespace
3535
});
3636

37+
io.of('/abc').on('connection', function(socket) {
38+
socket.emit('handshake', socket.handshake);
39+
});
40+
3741
io.of(nsp).on('connection', function(socket) {
3842
socket.send('hello client');
3943

@@ -96,6 +100,10 @@ io.of(nsp).on('connection', function(socket) {
96100
socket.on('error', function() {
97101
console.log('error: ', arguments);
98102
});
103+
104+
socket.on('getHandshake', function(cb) {
105+
cb(socket.handshake);
106+
});
99107
});
100108

101109

0 commit comments

Comments
 (0)