use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class HttpServerTests method keepAlive.
@Test
void keepAlive() throws URISyntaxException {
Path resource = Paths.get(getClass().getResource("/public").toURI());
disposableServer = createServer().route(routes -> routes.directory("/test", resource)).bindNow();
ConnectionProvider p = ConnectionProvider.create("keepAlive", 1);
Channel response0 = createClient(p, disposableServer.port()).get().uri("/test/index.html").responseConnection((res, c) -> Mono.just(c.channel()).delayUntil(ch -> c.inbound().receive())).blockLast(Duration.ofSeconds(30));
Channel response1 = createClient(p, disposableServer.port()).get().uri("/test/test.css").responseConnection((res, c) -> Mono.just(c.channel()).delayUntil(ch -> c.inbound().receive())).blockLast(Duration.ofSeconds(30));
Channel response2 = createClient(p, disposableServer.port()).get().uri("/test/test1.css").responseConnection((res, c) -> Mono.just(c.channel()).delayUntil(ch -> c.inbound().receive())).blockLast(Duration.ofSeconds(30));
Channel response3 = createClient(p, disposableServer.port()).get().uri("/test/test2.css").responseConnection((res, c) -> Mono.just(c.channel()).delayUntil(ch -> c.inbound().receive())).blockLast(Duration.ofSeconds(30));
Channel response4 = createClient(p, disposableServer.port()).get().uri("/test/test3.css").responseConnection((res, c) -> Mono.just(c.channel()).delayUntil(ch -> c.inbound().receive())).blockLast(Duration.ofSeconds(30));
Channel response5 = createClient(p, disposableServer.port()).get().uri("/test/test4.css").responseConnection((res, c) -> Mono.just(c.channel()).delayUntil(ch -> c.inbound().receive())).blockLast(Duration.ofSeconds(30));
assertThat(response0).isEqualTo(response1);
assertThat(response0).isEqualTo(response2);
assertThat(response0).isEqualTo(response3);
assertThat(response0).isEqualTo(response4);
assertThat(response0).isEqualTo(response5);
p.dispose();
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class HttpServerTests method doTestIssue1978.
private void doTestIssue1978(HttpProtocol[] serverProtocols, HttpProtocol[] clientProtocols, @Nullable Http2SslContextSpec serverCtx, @Nullable Http2SslContextSpec clientCtx, long serverDelay, long clientDelay) throws Exception {
int count = 5;
CountDownLatch latch = new CountDownLatch(count);
Mono<String> mono = Mono.just("testIssue1978");
Mono<String> serverResponse = serverDelay == 0 ? mono : mono.delayElement(Duration.ofMillis(serverDelay));
HttpServer mainServer = HttpServer.create().protocol(serverProtocols).httpRequestDecoder(spec -> spec.h2cMaxContentLength(8 * 1024));
HttpServer server = serverCtx != null ? mainServer.secure(sslContextSpec -> sslContextSpec.sslContext(serverCtx)) : mainServer;
disposableServer = server.handle((req, res) -> {
req.withConnection(conn -> conn.channel().closeFuture().addListener(f -> latch.countDown()));
return res.sendString(serverResponse);
}).bindNow();
byte[] content = new byte[1024];
Random rndm = new Random();
rndm.nextBytes(content);
String strContent = new String(content, Charset.defaultCharset());
Flux<String> flux = Flux.just(strContent, strContent, strContent, strContent);
Flux<String> clientRequest = clientDelay == 0 ? flux : flux.delayElements(Duration.ofMillis(clientDelay));
HttpClient mainClient = HttpClient.create().port(disposableServer.port()).protocol(clientProtocols);
HttpClient client = clientCtx != null ? mainClient.secure(sslContextSpec -> sslContextSpec.sslContext(clientCtx)) : mainClient;
Flux.range(0, count).flatMap(i -> client.post().uri("/").send(ByteBufFlux.fromString(clientRequest)).responseContent().aggregate().asString()).blockLast(Duration.ofSeconds(10));
assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class HttpServerTests method testCustomHandlerInvokedBeforeIOHandler.
@Test
void testCustomHandlerInvokedBeforeIOHandler() {
disposableServer = createServer().doOnConnection(c -> c.addHandlerFirst("custom", new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof HttpRequest) {
((HttpRequest) msg).headers().add("test", "test");
}
super.channelRead(ctx, msg);
}
})).handle((req, res) -> res.sendString(Mono.just(req.requestHeaders().get("test", "not found")))).bindNow();
StepVerifier.create(createClient(disposableServer::address).get().uri("/").responseContent().aggregate().asString()).expectNextMatches("test"::equals).expectComplete().verify(Duration.ofSeconds(30));
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class HttpServerTests method testMaxKeepAliveRequests.
@ParameterizedTest(name = "{displayName}({arguments})")
@ValueSource(ints = { -1, 1, 2 })
void testMaxKeepAliveRequests(int maxKeepAliveRequests) {
HttpServer server = createServer().handle((req, res) -> res.sendString(Mono.just("testMaxKeepAliveRequests")));
assertThat(server.configuration().maxKeepAliveRequests()).isEqualTo(-1);
server = server.maxKeepAliveRequests(maxKeepAliveRequests);
assertThat(server.configuration().maxKeepAliveRequests()).isEqualTo(maxKeepAliveRequests);
disposableServer = server.bindNow();
ConnectionProvider provider = ConnectionProvider.create("testMaxKeepAliveRequests", 1);
HttpClient client = createClient(provider, disposableServer.port());
Flux.range(0, 2).flatMap(i -> client.get().uri("/").responseSingle((res, bytes) -> bytes.asString().zipWith(Mono.just(res.responseHeaders().get(HttpHeaderNames.CONNECTION, "persistent"))))).collectList().as(StepVerifier::create).expectNextMatches(l -> {
boolean result = l.size() == 2 && "testMaxKeepAliveRequests".equals(l.get(0).getT1()) && "testMaxKeepAliveRequests".equals(l.get(1).getT1());
if (maxKeepAliveRequests == -1) {
return result && "persistent".equals(l.get(0).getT2()) && "persistent".equals(l.get(1).getT2());
} else if (maxKeepAliveRequests == 1) {
return result && "close".equals(l.get(0).getT2()) && "close".equals(l.get(1).getT2());
} else if (maxKeepAliveRequests == 2) {
return result && "persistent".equals(l.get(0).getT2()) && "close".equals(l.get(1).getT2());
}
return false;
}).expectComplete().verify(Duration.ofSeconds(5));
provider.disposeLater().block(Duration.ofSeconds(5));
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class HttpServerTests method doTestIssue1286.
private void doTestIssue1286(Function<HttpServer, HttpServer> serverCustomizer, Function<HttpClient, HttpClient> clientCustomizer, boolean connectionClose, boolean throwException) throws Exception {
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<List<ByteBuf>> replay = new AtomicReference<>(new ArrayList<>());
HttpServer server = serverCustomizer.apply(createServer());
disposableServer = server.doOnConnection(conn -> conn.addHandlerLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof ByteBufHolder) {
replay.get().add(((ByteBufHolder) msg).content());
} else if (msg instanceof ByteBuf) {
replay.get().add((ByteBuf) msg);
}
ctx.fireChannelRead(msg);
}
})).handle((req, res) -> {
res.withConnection(conn -> conn.onTerminate().subscribe(null, t -> latch.countDown(), latch::countDown));
if (throwException) {
return Mono.delay(Duration.ofMillis(100)).flatMap(l -> Mono.error(new RuntimeException("testIssue1286")));
}
return res.sendString(Mono.delay(Duration.ofMillis(100)).flatMap(l -> Mono.just("OK")));
}).bindNow();
HttpClient client = clientCustomizer.apply(createClient(disposableServer.port()));
if (connectionClose) {
client = client.headers(h -> h.add(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE));
}
client.post().uri("/").sendForm((req, form) -> form.attr("testIssue1286", "testIssue1286")).responseContent().aggregate().subscribe();
assertThat(latch.await(30, TimeUnit.SECONDS)).isTrue();
Mono.delay(Duration.ofMillis(500)).block();
assertThat(replay.get()).allMatch(buf -> buf.refCnt() == 0);
}
Aggregations