use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class HttpServerTests method httpServerRequestConfigInjectAttributes.
@Test
void httpServerRequestConfigInjectAttributes() {
AtomicReference<Channel> channelRef = new AtomicReference<>();
AtomicBoolean validate = new AtomicBoolean();
AtomicInteger chunkSize = new AtomicInteger();
AtomicBoolean allowDuplicateContentLengths = new AtomicBoolean();
disposableServer = createServer().httpRequestDecoder(opt -> opt.maxInitialLineLength(123).maxHeaderSize(456).maxChunkSize(789).validateHeaders(false).initialBufferSize(10).allowDuplicateContentLengths(true)).handle((req, resp) -> req.receive().then(resp.sendNotFound())).doOnConnection(c -> {
channelRef.set(c.channel());
HttpServerCodec codec = c.channel().pipeline().get(HttpServerCodec.class);
HttpObjectDecoder decoder = (HttpObjectDecoder) getValueReflection(codec, "inboundHandler", 1);
chunkSize.set((Integer) getValueReflection(decoder, "maxChunkSize", 2));
validate.set((Boolean) getValueReflection(decoder, "validateHeaders", 2));
allowDuplicateContentLengths.set((Boolean) getValueReflection(decoder, "allowDuplicateContentLengths", 2));
}).bindNow();
createClient(disposableServer::address).post().uri("/").send(ByteBufFlux.fromString(Mono.just("bodysample"))).responseContent().aggregate().asString().block();
assertThat(channelRef.get()).isNotNull();
assertThat(chunkSize).as("line length").hasValue(789);
assertThat(validate).as("validate headers").isFalse();
assertThat(allowDuplicateContentLengths).as("allow duplicate Content-Length").isTrue();
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class HttpServerTests method doTestConnectionClosePropagatedAsError.
private void doTestConnectionClosePropagatedAsError(String request) throws Exception {
AtomicReference<Throwable> error = new AtomicReference<>();
CountDownLatch msgLatch = new CountDownLatch(1);
CountDownLatch errLatch = new CountDownLatch(1);
disposableServer = createServer().handle((req, res) -> req.receive().doOnNext(b -> msgLatch.countDown()).doOnError(t -> {
errLatch.countDown();
error.set(t);
}).then(res.send())).bindNow();
int port = disposableServer.port();
Connection connection = TcpClient.create().remoteAddress(disposableServer::address).wiretap(true).connectNow();
String address = HttpUtil.formatHostnameForHttp((InetSocketAddress) disposableServer.address()) + ":" + port;
connection.outbound().sendString(Mono.just(String.format(request, address, address))).then().subscribe();
assertThat(msgLatch.await(5, TimeUnit.SECONDS)).as("Wait for the first message").isTrue();
connection.dispose();
assertThat(errLatch.await(5, TimeUnit.SECONDS)).as("Wait for the close connection error").isTrue();
assertThat(error.get()).isNotNull().isInstanceOf(AbortedException.class).hasMessage("Connection has been closed");
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class HttpRedirectTest method doTestHttpServerWithDomainSockets.
private void doTestHttpServerWithDomainSockets(HttpServer server, HttpClient client) {
assumeThat(LoopResources.hasNativeSupport()).isTrue();
disposableServer = server.bindAddress(() -> new DomainSocketAddress("/tmp/test.sock")).wiretap(true).route(r -> r.get("/redirect", (req, res) -> res.sendRedirect("/end")).get("/end", (req, res) -> res.sendString(Mono.just("END")))).bindNow();
String response = client.remoteAddress(disposableServer::address).wiretap(true).followRedirect(true).get().uri("/redirect").responseContent().aggregate().asString().block(Duration.ofSeconds(30));
assertThat(response).isEqualTo("END");
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class HttpRedirectTest method testHttp2Redirect.
@Test
void testHttp2Redirect() {
Http2SslContextSpec serverCtx = Http2SslContextSpec.forServer(ssc.certificate(), ssc.privateKey());
Http2SslContextSpec clientCtx = Http2SslContextSpec.forClient().configure(builder -> builder.trustManager(InsecureTrustManagerFactory.INSTANCE));
disposableServer = createServer().host("localhost").protocol(HttpProtocol.H2).secure(spec -> spec.sslContext(serverCtx)).route(r -> r.get("/1", (req, res) -> res.sendRedirect("/3")).get("/3", (req, res) -> res.status(200).sendString(Mono.just("OK")))).wiretap(true).bindNow();
Tuple2<String, Integer> response = createClient(disposableServer::address).followRedirect(true).protocol(HttpProtocol.H2).secure(spec -> spec.sslContext(clientCtx)).get().uri("/1").responseSingle((res, bytes) -> bytes.asString().zipWith(Mono.just(res.status().code()))).block(Duration.ofSeconds(30));
assertThat(response).isNotNull();
assertThat(response.getT2()).isEqualTo(200);
assertThat(response.getT1()).isEqualTo("OK");
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class HttpRedirectTest method testAbsoluteAndRelativeLocationRedirection.
/**
* https://github.com/reactor/reactor-netty/issues/278
* https://github.com/reactor/reactor-netty/issues/1533
*/
@Test
void testAbsoluteAndRelativeLocationRedirection() {
final int serverPort1 = SocketUtils.findAvailableTcpPort();
final int serverPort2 = SocketUtils.findAvailableTcpPort();
DisposableServer server1 = null;
DisposableServer server2 = null;
try {
server1 = createServer(serverPort1).host("localhost").route(r -> r.get("/1", (req, res) -> res.sendRedirect("/3")).get("/2", (req, res) -> res.sendRedirect("http://localhost:" + serverPort1 + "/3")).get("/3", (req, res) -> res.sendString(Mono.just("OK"))).get("/4", (req, res) -> res.sendRedirect("http://localhost:" + serverPort2 + "/1")).get("/5", (req, res) -> res.sendRedirect("//localhost:" + serverPort1 + "/3")).get("/6", (req, res) -> res.sendRedirect("./3")).get("/7/1", (req, res) -> res.sendRedirect("../3"))).bindNow();
server2 = createServer(serverPort2).host("localhost").route(r -> r.get("/1", (req, res) -> res.sendString(Mono.just("Other")))).bindNow();
String baseUrl1 = "http://localhost:" + serverPort1;
String baseUrl2 = "http://localhost:" + serverPort2;
HttpClient client = HttpClient.create().baseUrl(baseUrl1);
doTestAbsoluteAndRelativeLocationRedirection(client, "/1", "OK", true, baseUrl1 + "/1", baseUrl1 + "/3");
doTestAbsoluteAndRelativeLocationRedirection(client, "/2", "OK", true, baseUrl1 + "/2", baseUrl1 + "/3");
doTestAbsoluteAndRelativeLocationRedirection(client, "/3", "OK", false, baseUrl1 + "/3");
doTestAbsoluteAndRelativeLocationRedirection(client, "/4", "Other", true, baseUrl1 + "/4", baseUrl2 + "/1");
doTestAbsoluteAndRelativeLocationRedirection(client, "/5", "OK", true, baseUrl1 + "/5", baseUrl1 + "/3");
doTestAbsoluteAndRelativeLocationRedirection(client, "/6", "OK", true, baseUrl1 + "/6", baseUrl1 + "/3");
doTestAbsoluteAndRelativeLocationRedirection(client, "/7/1", "OK", true, baseUrl1 + "/7/1", baseUrl1 + "/3");
} finally {
if (server1 != null) {
server1.disposeNow();
}
if (server2 != null) {
server2.disposeNow();
}
}
}
Aggregations