support protocol ver 3 and sending byte data
This commit is contained in:
@@ -15,91 +15,152 @@ public class ParserTest {
|
||||
|
||||
@Test
|
||||
public void encodeAsString() {
|
||||
assertThat(encodePacket(new Packet(Packet.MESSAGE, "test")), isA(String.class));
|
||||
encodePacket(new Packet<String>(Packet.MESSAGE, "test"), new EncodeCallback<String>() {
|
||||
@Override
|
||||
public void call(String data) {
|
||||
assertThat(data, isA(String.class));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decodeAsPacket() {
|
||||
assertThat(decodePacket(encodePacket(new Packet(Packet.MESSAGE, "test"))), isA(Packet.class));
|
||||
encodePacket(new Packet<String>(Packet.MESSAGE, "test"), new EncodeCallback<String>() {
|
||||
@Override
|
||||
public void call(String data) {
|
||||
assertThat(decodePacket(data), isA(Packet.class));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noData() {
|
||||
Packet p = decodePacket(encodePacket(new Packet(Packet.MESSAGE)));
|
||||
assertThat(p.type, is(Packet.MESSAGE));
|
||||
assertThat(p.data, is(nullValue()));
|
||||
encodePacket(new Packet(Packet.MESSAGE), new EncodeCallback<String>() {
|
||||
@Override
|
||||
public void call(String data) {
|
||||
Packet p = decodePacket(data);
|
||||
assertThat(p.type, is(Packet.MESSAGE));
|
||||
assertThat(p.data, is(nullValue()));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeOpenPacket() {
|
||||
Packet p = decodePacket(encodePacket(new Packet(Packet.OPEN, "{\"some\":\"json\"}")));
|
||||
assertThat(p.type, is(Packet.OPEN));
|
||||
assertThat(p.data, is("{\"some\":\"json\"}"));
|
||||
encodePacket(new Packet<String>(Packet.OPEN, "{\"some\":\"json\"}"), new EncodeCallback<String>() {
|
||||
@Override
|
||||
public void call(String data) {
|
||||
Packet<String> p = decodePacket(data);
|
||||
assertThat(p.type, is(Packet.OPEN));
|
||||
assertThat(p.data, is("{\"some\":\"json\"}"));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeClosePacket() {
|
||||
Packet p = decodePacket(encodePacket(new Packet(Packet.CLOSE)));
|
||||
assertThat(p.type, is(Packet.CLOSE));
|
||||
encodePacket(new Packet<String>(Packet.CLOSE), new EncodeCallback<String>() {
|
||||
@Override
|
||||
public void call(String data) {
|
||||
Packet p = decodePacket(data);
|
||||
assertThat(p.type, is(Packet.CLOSE));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodePingPacket() {
|
||||
Packet p = decodePacket(encodePacket(new Packet(Packet.PING, "1")));
|
||||
assertThat(p.type, is(Packet.PING));
|
||||
assertThat(p.data, is("1"));
|
||||
encodePacket(new Packet<String>(Packet.PING, "1"), new EncodeCallback<String>() {
|
||||
@Override
|
||||
public void call(String data) {
|
||||
Packet<String> p = decodePacket(data);
|
||||
assertThat(p.type, is(Packet.PING));
|
||||
assertThat(p.data, is("1"));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodePongPacket() {
|
||||
Packet p = decodePacket(encodePacket(new Packet(Packet.PONG, "1")));
|
||||
assertThat(p.type, is(Packet.PONG));
|
||||
assertThat(p.data, is("1"));
|
||||
encodePacket(new Packet<String>(Packet.PONG, "1"), new EncodeCallback<String>() {
|
||||
@Override
|
||||
public void call(String data) {
|
||||
Packet<String> p = decodePacket(data);
|
||||
assertThat(p.type, is(Packet.PONG));
|
||||
assertThat(p.data, is("1"));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeMessagePacket() {
|
||||
Packet p = decodePacket(encodePacket(new Packet(Packet.MESSAGE, "aaa")));
|
||||
assertThat(p.type, is(Packet.MESSAGE));
|
||||
assertThat(p.data, is("aaa"));
|
||||
encodePacket(new Packet<String>(Packet.MESSAGE, "aaa"), new EncodeCallback<String>() {
|
||||
@Override
|
||||
public void call(String data) {
|
||||
Packet<String> p = decodePacket(data);
|
||||
assertThat(p.type, is(Packet.MESSAGE));
|
||||
assertThat(p.data, is("aaa"));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeUpgradePacket() {
|
||||
Packet p = decodePacket(encodePacket(new Packet(Packet.UPGRADE)));
|
||||
assertThat(p.type, is(Packet.UPGRADE));
|
||||
encodePacket(new Packet<String>(Packet.UPGRADE), new EncodeCallback<String>() {
|
||||
@Override
|
||||
public void call(String data) {
|
||||
Packet p = decodePacket(data);
|
||||
assertThat(p.type, is(Packet.UPGRADE));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodingFormat() {
|
||||
assertThat(encodePacket(new Packet(Packet.MESSAGE, "test")).matches("[0-9].*"), is(true));
|
||||
assertThat(encodePacket(new Packet(Packet.MESSAGE)).matches("[0-9]"), is(true));
|
||||
encodePacket(new Packet<String>(Packet.MESSAGE, "test"), new EncodeCallback<String>() {
|
||||
@Override
|
||||
public void call(String data) {
|
||||
assertThat(data.matches("[0-9].*"), is(true));
|
||||
}
|
||||
});
|
||||
encodePacket(new Packet<String>(Packet.MESSAGE), new EncodeCallback<String>() {
|
||||
@Override
|
||||
public void call(String data) {
|
||||
assertThat(data.matches("[0-9]"), is(true));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decodeBadFormat() {
|
||||
Packet p = decodePacket(":::");
|
||||
Packet<String> p = decodePacket(":::");
|
||||
assertThat(p.type, is(Packet.ERROR));
|
||||
assertThat(p.data, is(ERROR_DATA));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decodeInexistentTypes() {
|
||||
Packet p = decodePacket("94103");
|
||||
Packet<String> p = decodePacket("94103");
|
||||
assertThat(p.type, is(Packet.ERROR));
|
||||
assertThat(p.data, is(ERROR_DATA));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodePayloadsAsString() {
|
||||
assertThat(encodePayload(new Packet[] {
|
||||
new Packet(Packet.PING), new Packet(Packet.PONG)}), isA(String.class));
|
||||
public void encodePayloads() {
|
||||
encodePayload(new Packet[]{new Packet(Packet.PING), new Packet(Packet.PONG)}, new EncodeCallback<byte[]>() {
|
||||
@Override
|
||||
public void call(byte[] data) {
|
||||
assertThat(data, isA(byte[].class));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeAndDecodePayloads() {
|
||||
decodePayload(encodePayload(new Packet[] {new Packet(Packet.MESSAGE, "a")}),
|
||||
new DecodePayloadCallback() {
|
||||
encodePayload(new Packet[] {new Packet<String>(Packet.MESSAGE, "a")}, new EncodeCallback<byte[]>() {
|
||||
@Override
|
||||
public void call(byte[] data) {
|
||||
decodePayload(data, new DecodePayloadCallback() {
|
||||
@Override
|
||||
public boolean call(Packet packet, int index, int total) {
|
||||
boolean isLast = index + 1 == total;
|
||||
@@ -107,9 +168,12 @@ public class ParserTest {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
decodePayload(encodePayload(new Packet[] {
|
||||
new Packet(Packet.MESSAGE, "a"), new Packet(Packet.PING)}),
|
||||
new DecodePayloadCallback() {
|
||||
}
|
||||
});
|
||||
encodePayload(new Packet[]{new Packet<String>(Packet.MESSAGE, "a"), new Packet(Packet.PING)}, new EncodeCallback<byte[]>() {
|
||||
@Override
|
||||
public void call(byte[] data) {
|
||||
decodePayload(data, new DecodePayloadCallback() {
|
||||
@Override
|
||||
public boolean call(Packet packet, int index, int total) {
|
||||
boolean isLast = index + 1 == total;
|
||||
@@ -121,26 +185,33 @@ public class ParserTest {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeAndDecodeEmptyPayloads() {
|
||||
decodePayload(encodePayload(new Packet[] {}), new DecodePayloadCallback() {
|
||||
encodePayload(new Packet[] {}, new EncodeCallback<byte[]>() {
|
||||
@Override
|
||||
public boolean call(Packet packet, int index, int total) {
|
||||
assertThat(packet.type, is(Packet.OPEN));
|
||||
boolean isLast = index + 1 == total;
|
||||
assertThat(isLast, is(true));
|
||||
return true;
|
||||
public void call(byte[] data) {
|
||||
decodePayload(data, new DecodePayloadCallback() {
|
||||
@Override
|
||||
public boolean call(Packet packet, int index, int total) {
|
||||
assertThat(packet.type, is(Packet.OPEN));
|
||||
boolean isLast = index + 1 == total;
|
||||
assertThat(isLast, is(true));
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decodePayloadBadFormat() {
|
||||
decodePayload("1!", new DecodePayloadCallback() {
|
||||
decodePayload("1!", new DecodePayloadCallback<String>() {
|
||||
@Override
|
||||
public boolean call(Packet packet, int index, int total) {
|
||||
public boolean call(Packet<String> packet, int index, int total) {
|
||||
boolean isLast = index + 1 == total;
|
||||
assertThat(packet.type, is(Packet.ERROR));
|
||||
assertThat(packet.data, is(ERROR_DATA));
|
||||
@@ -148,9 +219,9 @@ public class ParserTest {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
decodePayload("", new DecodePayloadCallback() {
|
||||
decodePayload("", new DecodePayloadCallback<String>() {
|
||||
@Override
|
||||
public boolean call(Packet packet, int index, int total) {
|
||||
public boolean call(Packet<String> packet, int index, int total) {
|
||||
boolean isLast = index + 1 == total;
|
||||
assertThat(packet.type, is(Packet.ERROR));
|
||||
assertThat(packet.data, is(ERROR_DATA));
|
||||
@@ -158,9 +229,9 @@ public class ParserTest {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
decodePayload("))", new DecodePayloadCallback() {
|
||||
decodePayload("))", new DecodePayloadCallback<String>() {
|
||||
@Override
|
||||
public boolean call(Packet packet, int index, int total) {
|
||||
public boolean call(Packet<String> packet, int index, int total) {
|
||||
boolean isLast = index + 1 == total;
|
||||
assertThat(packet.type, is(Packet.ERROR));
|
||||
assertThat(packet.data, is(ERROR_DATA));
|
||||
@@ -172,9 +243,9 @@ public class ParserTest {
|
||||
|
||||
@Test
|
||||
public void decodePayloadBadLength() {
|
||||
decodePayload("1:", new DecodePayloadCallback() {
|
||||
decodePayload("1:", new DecodePayloadCallback<String>() {
|
||||
@Override
|
||||
public boolean call(Packet packet, int index, int total) {
|
||||
public boolean call(Packet<String> packet, int index, int total) {
|
||||
boolean isLast = index + 1 == total;
|
||||
assertThat(packet.type, is(Packet.ERROR));
|
||||
assertThat(packet.data, is(ERROR_DATA));
|
||||
@@ -186,9 +257,9 @@ public class ParserTest {
|
||||
|
||||
@Test
|
||||
public void decodePayloadBadPacketFormat() {
|
||||
decodePayload("3:99:", new DecodePayloadCallback() {
|
||||
decodePayload("3:99:", new DecodePayloadCallback<String>() {
|
||||
@Override
|
||||
public boolean call(Packet packet, int index, int total) {
|
||||
public boolean call(Packet<String> packet, int index, int total) {
|
||||
boolean isLast = index + 1 == total;
|
||||
assertThat(packet.type, is(Packet.ERROR));
|
||||
assertThat(packet.data, is(ERROR_DATA));
|
||||
@@ -196,9 +267,9 @@ public class ParserTest {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
decodePayload("1:aa", new DecodePayloadCallback() {
|
||||
decodePayload("1:aa", new DecodePayloadCallback<String>() {
|
||||
@Override
|
||||
public boolean call(Packet packet, int index, int total) {
|
||||
public boolean call(Packet<String> packet, int index, int total) {
|
||||
boolean isLast = index + 1 == total;
|
||||
assertThat(packet.type, is(Packet.ERROR));
|
||||
assertThat(packet.data, is(ERROR_DATA));
|
||||
@@ -206,9 +277,9 @@ public class ParserTest {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
decodePayload("1:a2:b", new DecodePayloadCallback() {
|
||||
decodePayload("1:a2:b", new DecodePayloadCallback<String>() {
|
||||
@Override
|
||||
public boolean call(Packet packet, int index, int total) {
|
||||
public boolean call(Packet<String> packet, int index, int total) {
|
||||
boolean isLast = index + 1 == total;
|
||||
assertThat(packet.type, is(Packet.ERROR));
|
||||
assertThat(packet.data, is(ERROR_DATA));
|
||||
|
||||
Reference in New Issue
Block a user