use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class HttpServerTests method httpPipelining.
@Test
void httpPipelining() throws Exception {
AtomicInteger i = new AtomicInteger();
disposableServer = createServer().handle((req, resp) -> resp.header(HttpHeaderNames.CONTENT_LENGTH, "1").sendString(Mono.just(i.incrementAndGet()).flatMap(d -> Mono.delay(Duration.ofSeconds(4 - d)).map(x -> d + "\n")))).bindNow();
DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/plaintext");
CountDownLatch latch = new CountDownLatch(6);
Connection client = TcpClient.create().port(disposableServer.port()).handle((in, out) -> {
in.withConnection(x -> x.addHandlerFirst(new HttpClientCodec())).receiveObject().ofType(DefaultHttpContent.class).as(ByteBufFlux::fromInbound).asString().log().map(Integer::parseInt).subscribe(d -> {
for (int x = 0; x < d; x++) {
latch.countDown();
}
});
return out.sendObject(Flux.just(request.retain(), request.retain(), request.retain())).neverComplete();
}).wiretap(true).connectNow();
assertThat(latch.await(45, TimeUnit.SECONDS)).as("latch await").isTrue();
client.disposeNow();
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class HttpServerTests method testIssue525.
@Test
void testIssue525() {
disposableServer = createServer().doOnConnection(c -> c.addHandlerFirst("decompressor", new HttpContentDecompressor())).handle((req, res) -> res.send(req.receive().retain())).bindNow(Duration.ofSeconds(30));
byte[] bytes = "test".getBytes(Charset.defaultCharset());
String response = createClient(disposableServer.port()).headers(h -> h.add("Content-Encoding", "gzip")).post().uri("/").send(Mono.just(Unpooled.wrappedBuffer(compress(bytes)))).responseContent().aggregate().asString().block(Duration.ofSeconds(30));
assertThat(response).isEqualTo("test");
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class HttpServerTests method startRouter.
@Test
void startRouter() {
disposableServer = createServer().route(routes -> routes.get("/hello", (req, resp) -> resp.sendString(Mono.just("hello!")))).bindNow();
Integer code = createClient(disposableServer.port()).get().uri("/hello").responseSingle((res, buf) -> Mono.just(res.status().code())).block();
assertThat(code).isEqualTo(200);
code = createClient(disposableServer.port()).get().uri("/helloMan").responseSingle((res, buf) -> Mono.just(res.status().code())).block();
assertThat(code).isEqualTo(404);
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class HttpServerTests method testExpectErrorWhenConnectionClosed.
@Test
@SuppressWarnings("FutureReturnValueIgnored")
void testExpectErrorWhenConnectionClosed() throws Exception {
SslContext serverCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
AtomicReference<Throwable> error = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
disposableServer = createServer().secure(spec -> spec.sslContext(serverCtx)).handle((req, res) -> {
// "FutureReturnValueIgnored" is suppressed deliberately
res.withConnection(conn -> conn.channel().close());
return res.sendString(Flux.just("OK").hide()).then().doOnError(t -> {
error.set(t);
latch.countDown();
});
}).bindNow();
SslContext clientCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
StepVerifier.create(createClient(disposableServer::address).secure(spec -> spec.sslContext(clientCtx)).get().uri("/").responseContent()).verifyError(PrematureCloseException.class);
assertThat(latch.await(30, TimeUnit.SECONDS)).isTrue();
assertThat(error.get()).isInstanceOf(AbortedException.class);
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class HttpServerTests method testCancelConnectionCloseForWebSocketClient.
@Test
void testCancelConnectionCloseForWebSocketClient() throws Exception {
AtomicReference<WebSocketCloseStatus> statusServer = new AtomicReference<>();
AtomicReference<WebSocketCloseStatus> statusClient = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(2);
disposableServer = createServer().handle((req, resp) -> resp.sendWebsocket((in, out) -> in.receiveCloseStatus().doOnNext(o -> {
statusServer.set(o);
latch.countDown();
}).then())).bindNow();
createClient(disposableServer.port()).websocket().uri("/").handle((in, out) -> {
in.receiveCloseStatus().doOnNext(o -> {
statusClient.set(o);
latch.countDown();
}).subscribe();
in.withConnection(Connection::dispose);
return Mono.never();
}).subscribe();
assertThat(latch.await(30, TimeUnit.SECONDS)).isTrue();
assertThat(statusClient.get()).isNotNull().isEqualTo(WebSocketCloseStatus.ABNORMAL_CLOSURE);
assertThat(statusServer.get()).isNotNull().isEqualTo(WebSocketCloseStatus.EMPTY);
}
Aggregations