Enhance Parser decode
* Avoid using expected IndexOutOfBound exception * Add few more error checks * Add few malformated message error test cases
This commit is contained in:
@@ -15,6 +15,7 @@ import static org.junit.Assert.assertThat;
|
||||
public class Helpers {
|
||||
|
||||
private static Parser.Encoder encoder = new Parser.Encoder();
|
||||
private static Packet<String> errorPacket = new Packet<String>(Parser.ERROR, "parser error");
|
||||
|
||||
public static void test(final Packet obj) {
|
||||
encoder.encode(obj, new Parser.Encoder.Callback() {
|
||||
@@ -33,6 +34,18 @@ public class Helpers {
|
||||
});
|
||||
}
|
||||
|
||||
public static void testDecodeError(final String errorMessage) {
|
||||
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];
|
||||
assertPacket(errorPacket, packet);
|
||||
}
|
||||
});
|
||||
decoder.add(errorMessage);
|
||||
}
|
||||
|
||||
public static void testBin(final Packet obj) {
|
||||
final Object originalData = obj.data;
|
||||
encoder.encode(obj, new Parser.Encoder.Callback() {
|
||||
|
||||
@@ -11,7 +11,6 @@ public class ParserTest {
|
||||
|
||||
private static Parser.Encoder encoder = new Parser.Encoder();
|
||||
|
||||
|
||||
@Test
|
||||
public void encodeConnection() {
|
||||
Packet packet = new Packet(Parser.CONNECT);
|
||||
@@ -47,4 +46,22 @@ public class ParserTest {
|
||||
packet.nsp = "/";
|
||||
Helpers.test(packet);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decodeInError() throws JSONException {
|
||||
// Random string
|
||||
Helpers.testDecodeError("asdf");
|
||||
// Unknown type
|
||||
Helpers.testDecodeError(Parser.types.length + "asdf");
|
||||
// Binary event with no `-`
|
||||
Helpers.testDecodeError(Parser.BINARY_EVENT + "asdf");
|
||||
// Binary ack with no `-`
|
||||
Helpers.testDecodeError(Parser.BINARY_ACK + "asdf");
|
||||
// Binary event with no attachment
|
||||
Helpers.testDecodeError(String.valueOf(Parser.BINARY_EVENT));
|
||||
// event non numeric id
|
||||
Helpers.testDecodeError(Parser.EVENT + "2sd");
|
||||
// event with invalid json data
|
||||
Helpers.testDecodeError(Parser.EVENT + "2[\"a\",1,{asdf}]");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user