use of reactor.ipc.netty.NettyContext 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 reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class HttpResponseStatusCodesHandlingTests method httpStatusCode404IsHandledByTheClient.
@Test
public void httpStatusCode404IsHandledByTheClient() {
NettyContext server = HttpServer.create(0).newRouter(r -> r.post("/test", (req, res) -> res.send(req.receive().log("server-received")))).block(Duration.ofSeconds(30));
HttpClient client = HttpClient.create("localhost", server.address().getPort());
List<String> replyReceived = new ArrayList<>();
Mono<String> content = client.get("/unsupportedURI", req -> req.addHeader("Content-Type", "text/plain").sendString(Flux.just("Hello").log("client-send"))).flatMapMany(res -> res.receive().asString().log("client-received").doOnNext(s -> replyReceived.add(s))).next().doOnError(t -> System.err.println("Failed requesting server: " + t.getMessage()));
StepVerifier.create(content).expectErrorMatches(t -> t.getMessage().equals("HTTP request failed with code: 404.\nFailing URI: " + "/unsupportedURI")).verify(Duration.ofSeconds(30));
Assertions.assertThat(replyReceived).isEmpty();
server.dispose();
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class HttpTests method test100Continue.
@Test
public void test100Continue() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
NettyContext server = HttpServer.create(0).newHandler((req, res) -> req.receive().aggregate().asString().flatMap(s -> {
latch.countDown();
return res.sendString(Mono.just(s)).then();
})).block(Duration.ofSeconds(30));
String content = HttpClient.create(server.address().getPort()).post("/", req -> req.header("Expect", "100-continue").sendString(Flux.just("1", "2", "3", "4", "5"))).flatMap(res -> res.receive().aggregate().asString()).block();
System.out.println(content);
Assertions.assertThat(latch.await(30, TimeUnit.SECONDS)).isTrue();
server.dispose();
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class HttpTests method streamAndPoolDefaultCompression.
@Test
public void streamAndPoolDefaultCompression() throws Exception {
EmitterProcessor<String> ep = EmitterProcessor.create();
NettyContext server = HttpServer.create(opts -> opts.port(0).compression(true)).newRouter(r -> r.post("/hi", (req, res) -> req.receive().aggregate().asString().log().then(res.compression(false).sendString(Flux.just("test")).then())).get("/stream", (req, res) -> req.receive().then(res.options(op -> op.flushOnEach()).sendString(ep.log()).then()))).block(Duration.ofSeconds(30));
String content = HttpClient.create(opts -> opts.port(server.address().getPort()).compression(true)).post("/hi", req -> req.sendString(Flux.just("1", "2", "3", "4", "5"))).flatMap(res -> res.receive().aggregate().asString().log()).block();
Flux<String> f = HttpClient.create(opts -> opts.port(server.address().getPort()).compression(true)).get("/stream").flatMapMany(res -> res.receive().asString());
System.out.println(content);
StepVerifier.create(f).then(() -> ep.onNext("test1")).expectNext("test1").thenAwait(Duration.ofMillis(300)).then(() -> ep.onNext("test2")).thenAwait(Duration.ofMillis(300)).expectNext("test2").thenAwait(Duration.ofMillis(300)).then(() -> ep.onComplete()).verifyComplete();
content = HttpClient.create(opts -> opts.port(server.address().getPort()).compression(true)).post("/hi", req -> req.sendString(Flux.just("1", "2", "3", "4", "5"))).flatMap(res -> res.receive().aggregate().asString().log()).block();
server.dispose();
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class HttpTests method webSocketRespondsToRequestsFromClients.
@Test
public void webSocketRespondsToRequestsFromClients() {
AtomicInteger clientRes = new AtomicInteger();
AtomicInteger serverRes = new AtomicInteger();
NettyContext server = HttpServer.create(0).newRouter(r -> r.get("/test/{param}", (req, res) -> {
System.out.println(req.requestHeaders().get("test"));
return res.header("content-type", "text/plain").sendWebsocket((in, out) -> out.options(c -> c.flushOnEach()).sendString(in.receive().asString().publishOn(Schedulers.single()).doOnNext(s -> serverRes.incrementAndGet()).map(it -> it + ' ' + req.param("param") + '!').log("server-reply")));
})).block(Duration.ofSeconds(5));
HttpClient client = HttpClient.create("localhost", server.address().getPort());
Mono<List<String>> response = client.get("/test/World", req -> req.header("Content-Type", "text/plain").header("test", "test").options(c -> c.flushOnEach()).sendWebsocket().sendString(Flux.range(1, 1000).log("client-send").map(i -> "" + i))).flatMapMany(res -> res.receive().asString().log("client-received").publishOn(Schedulers.parallel()).doOnNext(s -> clientRes.incrementAndGet())).take(1000).collectList().cache().doOnError(i -> System.err.println("Failed requesting server: " + i));
System.out.println("STARTING: server[" + serverRes.get() + "] / client[" + clientRes.get() + "]");
StepVerifier.create(response).expectNextMatches(list -> "1000 World!".equals(list.get(999))).expectComplete().verify(Duration.ofSeconds(30));
System.out.println("FINISHED: server[" + serverRes.get() + "] / client[" + clientRes + "]");
server.dispose();
}
Aggregations