From f6cc92a079a7a80cdda72bb3c46ccb273677c653 Mon Sep 17 00:00:00 2001 From: Paulo Lopes Date: Fri, 8 Jul 2022 11:05:33 +0200 Subject: [PATCH] Update StreamParserTest.java --- .../eventbus/bridge/tcp/StreamParserTest.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/test/java/io/vertx/ext/eventbus/bridge/tcp/StreamParserTest.java b/src/test/java/io/vertx/ext/eventbus/bridge/tcp/StreamParserTest.java index 8db0ff0..1e2fc29 100644 --- a/src/test/java/io/vertx/ext/eventbus/bridge/tcp/StreamParserTest.java +++ b/src/test/java/io/vertx/ext/eventbus/bridge/tcp/StreamParserTest.java @@ -1,6 +1,8 @@ package io.vertx.ext.eventbus.bridge.tcp; import io.vertx.core.buffer.Buffer; +import io.vertx.core.json.Json; +import io.vertx.core.json.JsonArray; import io.vertx.ext.eventbus.bridge.tcp.impl.StreamParser; import io.vertx.ext.unit.Async; import io.vertx.ext.unit.TestContext; @@ -58,4 +60,56 @@ public void testParseSimpleHeaderless(TestContext should) { parser.handle(Buffer.buffer("{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":\"hi\"}\r\n")); } + + @Test(timeout = 30_000) + public void testParseSimpleHeaderlessBatch(TestContext should) { + final Async test = should.async(); + final StreamParser parser = new StreamParser() + .exceptionHandler(should::fail) + .handler(body -> { + Object json = Json.decodeValue(body); + // it's a batch! + should.assertTrue(json instanceof JsonArray); + test.complete(); + }); + + parser.handle(Buffer.buffer("[\n" + + " {\"jsonrpc\": \"2.0\", \"method\": \"sum\", \"params\": [1,2,4], \"id\": \"1\"},\n" + + " {\"jsonrpc\": \"2.0\", \"method\": \"notify_hello\", \"params\": [7]},\n" + + " {\"jsonrpc\": \"2.0\", \"method\": \"subtract\", \"params\": [42,23], \"id\": \"2\"},\n" + + " {\"foo\": \"boo\"},\n" + + " {\"jsonrpc\": \"2.0\", \"method\": \"foo.get\", \"params\": {\"name\": \"myself\"}, \"id\": \"5\"},\n" + + " {\"jsonrpc\": \"2.0\", \"method\": \"get_data\", \"id\": \"9\"} \n" + + " ]\r\n")); + } + + @Test(timeout = 30_000) + public void testParseSimpleHeaderlessBatchFromSpec(TestContext should) { + final Async test = should.async(); + final StreamParser parser = new StreamParser() + .exceptionHandler(should::fail) + .handler(body -> { + Object json = Json.decodeValue(body); + // it's a batch! + should.assertTrue(json instanceof JsonArray); + test.complete(); + }); + + parser.handle(Buffer.buffer("[]\r\n")); + } + + @Test(timeout = 30_000) + public void testParseSimpleHeaderlessBatchFromSpec2(TestContext should) { + final Async test = should.async(); + final StreamParser parser = new StreamParser() + .exceptionHandler(should::fail) + .handler(body -> { + Object json = Json.decodeValue(body); + // it's a batch! + should.assertTrue(json instanceof JsonArray); + test.complete(); + }); + + parser.handle(Buffer.buffer("[1, 2, 3]\r\n")); + } }