use of io.netty.handler.codec.json.JsonObjectDecoder in project reactor-netty by reactor.
the class TcpServerTests method flushEvery5ElementsWithManualDecoding.
@Test
public void flushEvery5ElementsWithManualDecoding() throws Exception {
ObjectMapper mapper = new ObjectMapper();
Function<List<Pojo>, ByteBuf> jsonEncoder = pojo -> {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
mapper.writeValue(out, pojo);
} catch (Exception e) {
throw new RuntimeException(e);
}
return Unpooled.copiedBuffer(out.toByteArray());
};
Function<String, Pojo[]> jsonDecoder = s -> {
try {
return mapper.readValue(s, Pojo[].class);
} catch (Exception e) {
throw new RuntimeException(e);
}
};
CountDownLatch dataLatch = new CountDownLatch(10);
NettyContext server = TcpServer.create().newHandler((in, out) -> in.context(c -> c.addHandler(new JsonObjectDecoder())).receive().asString().log("serve").map(jsonDecoder).concatMap(d -> Flux.fromArray(d)).window(5).concatMap(w -> out.send(w.collectList().map(jsonEncoder)))).block(Duration.ofSeconds(30));
NettyContext client = TcpClient.create(o -> o.port(server.address().getPort())).newHandler((in, out) -> {
in.context(c -> c.addHandler(new JsonObjectDecoder())).receive().asString().log("receive").map(jsonDecoder).concatMap(d -> Flux.fromArray(d)).subscribe(c -> dataLatch.countDown());
return out.send(Flux.range(1, 10).map(it -> new Pojo("test" + it)).log("send").collectList().map(jsonEncoder)).neverComplete();
}).block(Duration.ofSeconds(30));
Assertions.assertThat(dataLatch.await(30, TimeUnit.SECONDS)).isTrue();
Assertions.assertThat(dataLatch.getCount()).isEqualTo(0);
server.dispose();
client.dispose();
}
use of io.netty.handler.codec.json.JsonObjectDecoder in project reactor-netty by reactor.
the class HttpClientOperationsTest method addEncoderReplaysLastHttp.
@Test
public void addEncoderReplaysLastHttp() throws Exception {
ByteBuf buf = Unpooled.copiedBuffer("{\"foo\":1}", CharsetUtil.UTF_8);
EmbeddedChannel channel = new EmbeddedChannel();
HttpClientOperations ops = new HttpClientOperations(channel, (response, request) -> null, handler);
ops.addHandler(new JsonObjectDecoder());
channel.writeInbound(new DefaultLastHttpContent(buf));
assertThat(channel.pipeline().names().iterator().next(), is("JsonObjectDecoder$extractor"));
Object content = channel.readInbound();
assertThat(content, instanceOf(ByteBuf.class));
((ByteBuf) content).release();
content = channel.readInbound();
assertThat(content, instanceOf(LastHttpContent.class));
((LastHttpContent) content).release();
assertThat(channel.readInbound(), nullValue());
}
use of io.netty.handler.codec.json.JsonObjectDecoder in project reactor-netty by reactor.
the class HttpClientOperationsTest method addDecoderReplaysLastHttp.
@Test
public void addDecoderReplaysLastHttp() throws Exception {
ByteBuf buf = Unpooled.copiedBuffer("{\"foo\":1}", CharsetUtil.UTF_8);
EmbeddedChannel channel = new EmbeddedChannel();
HttpClientOperations ops = new HttpClientOperations(channel, (response, request) -> null, handler);
ops.addHandler(new JsonObjectDecoder());
channel.writeInbound(new DefaultLastHttpContent(buf));
assertThat(channel.pipeline().names().iterator().next(), is("JsonObjectDecoder$extractor"));
Object content = channel.readInbound();
assertThat(content, instanceOf(ByteBuf.class));
((ByteBuf) content).release();
content = channel.readInbound();
assertThat(content, instanceOf(LastHttpContent.class));
((LastHttpContent) content).release();
assertThat(channel.readInbound(), nullValue());
}
use of io.netty.handler.codec.json.JsonObjectDecoder in project reactor-netty by reactor.
the class HttpOperationsTest method httpAndJsonDecoders.
@Test
public void httpAndJsonDecoders() {
EmbeddedChannel channel = new EmbeddedChannel();
NettyContext testContext = () -> channel;
ChannelHandler handler = new JsonObjectDecoder(true);
testContext.addHandlerLast("foo", handler);
HttpOperations.autoAddHttpExtractor(testContext, "foo", handler);
String json1 = "[{\"some\": 1} , {\"valu";
String json2 = "e\": true, \"test\": 1}]";
Object[] content = new Object[3];
content[0] = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
content[1] = new DefaultHttpContent(Unpooled.copiedBuffer(json1, CharsetUtil.UTF_8));
content[2] = new DefaultLastHttpContent(Unpooled.copiedBuffer(json2, CharsetUtil.UTF_8));
channel.writeInbound(content);
Object t = channel.readInbound();
assertThat(t, instanceOf(HttpResponse.class));
assertThat(t, not(instanceOf(HttpContent.class)));
t = channel.readInbound();
assertThat(t, instanceOf(ByteBuf.class));
assertThat(((ByteBuf) t).toString(CharsetUtil.UTF_8), is("{\"some\": 1}"));
((ByteBuf) t).release();
t = channel.readInbound();
assertThat(t, instanceOf(ByteBuf.class));
assertThat(((ByteBuf) t).toString(CharsetUtil.UTF_8), is("{\"value\": true, \"test\": 1}"));
((ByteBuf) t).release();
t = channel.readInbound();
assertThat(t, is(LastHttpContent.EMPTY_LAST_CONTENT));
((LastHttpContent) t).release();
t = channel.readInbound();
assertThat(t, nullValue());
}
use of io.netty.handler.codec.json.JsonObjectDecoder in project reactor-netty by reactor.
the class HttpClientOperationsTest method addNamedDecoderReplaysLastHttp.
@Test
public void addNamedDecoderReplaysLastHttp() throws Exception {
ByteBuf buf = Unpooled.copiedBuffer("{\"foo\":1}", CharsetUtil.UTF_8);
EmbeddedChannel channel = new EmbeddedChannel();
HttpClientOperations ops = new HttpClientOperations(channel, (response, request) -> null, handler);
ops.addHandler("json", new JsonObjectDecoder());
channel.writeInbound(new DefaultLastHttpContent(buf));
assertThat(channel.pipeline().names().iterator().next(), is("json$extractor"));
Object content = channel.readInbound();
assertThat(content, instanceOf(ByteBuf.class));
((ByteBuf) content).release();
content = channel.readInbound();
assertThat(content, instanceOf(LastHttpContent.class));
((LastHttpContent) content).release();
assertThat(channel.readInbound(), nullValue());
}
Aggregations