support binary
This commit is contained in:
@@ -6,6 +6,8 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
@@ -16,21 +18,21 @@ public class ParserTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void connect() {
|
||||
public void encodeConnection() {
|
||||
Packet packet = new Packet(Parser.CONNECT);
|
||||
packet.nsp = "/woot";
|
||||
test(packet);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void disconnect() {
|
||||
public void encodeDisconnect() {
|
||||
Packet packet = new Packet(Parser.DISCONNECT);
|
||||
packet.nsp = "/woot";
|
||||
test(packet);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void event() {
|
||||
public void encodeEvent() {
|
||||
Packet packet1 = new Packet(Parser.EVENT);
|
||||
packet1.data = new JSONTokener("[\"a\", 1, {}]").nextValue();
|
||||
packet1.nsp = "/";
|
||||
@@ -43,7 +45,7 @@ public class ParserTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ack() {
|
||||
public void encodeAck() {
|
||||
Packet packet = new Packet(Parser.ACK);
|
||||
packet.data = new JSONTokener("[\"a\", 1, {}]").nextValue();
|
||||
packet.id = 123;
|
||||
@@ -51,10 +53,19 @@ public class ParserTest {
|
||||
test(packet);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeBytes() {
|
||||
Packet packet = new Packet(Parser.BINARY_EVENT);
|
||||
packet.data = "abc".getBytes(Charset.forName("UTF-8"));
|
||||
packet.id = 23;
|
||||
packet.nsp = "/cool";
|
||||
testBin(packet);
|
||||
}
|
||||
|
||||
private void test(final Packet obj) {
|
||||
encoder.encode(obj, new Parser.Encoder.Callback() {
|
||||
@Override
|
||||
public void call(String[] encodedPackets) {
|
||||
public void call(Object[] encodedPackets) {
|
||||
Parser.Decoder decoder = new Parser.Decoder();
|
||||
decoder.on(Parser.Decoder.EVENT_DECODED, new Emitter.Listener() {
|
||||
@Override
|
||||
@@ -71,7 +82,39 @@ public class ParserTest {
|
||||
assertThat(packet.attachments, is(obj.attachments));
|
||||
}
|
||||
});
|
||||
decoder.add(encodedPackets[0]);
|
||||
decoder.add((String)encodedPackets[0]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void testBin(final Packet obj) {
|
||||
final Object originalData = obj.data;
|
||||
encoder.encode(obj, new Parser.Encoder.Callback() {
|
||||
@Override
|
||||
public void call(Object[] encodedPackets) {
|
||||
Parser.Decoder decoder = new Parser.Decoder();
|
||||
decoder.on(Parser.Decoder.EVENT_DECODED, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
Packet packet = (Packet)args[0];
|
||||
obj.data = originalData;
|
||||
obj.attachments = -1;
|
||||
|
||||
assertThat(packet.type, is(obj.type));
|
||||
assertThat(packet.id, is(obj.id));
|
||||
assertThat(packet.data, is(obj.data));
|
||||
assertThat(packet.nsp, is(obj.nsp));
|
||||
assertThat(packet.attachments, is(obj.attachments));
|
||||
}
|
||||
});
|
||||
|
||||
for (Object packet : encodedPackets) {
|
||||
if (packet instanceof String) {
|
||||
decoder.add((String)packet);
|
||||
} else if (packet instanceof byte[]) {
|
||||
decoder.add((byte[])packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user