use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class HttpRedirectTest method testRelativeRedirectKeepsScheme.
@Test
void testRelativeRedirectKeepsScheme() {
final String requestPath = "/request";
final String redirectPath = "/redirect";
final String responseContent = "Success";
disposableServer = createServer().route(r -> r.get(requestPath, (req, res) -> res.sendRedirect(redirectPath)).get(redirectPath, (req, res) -> res.sendString(Mono.just(responseContent)))).bindNow();
Http11SslContextSpec http11SslContextSpec = Http11SslContextSpec.forClient().configure(builder -> builder.trustManager(InsecureTrustManagerFactory.INSTANCE));
final Mono<String> responseMono = HttpClient.create().wiretap(true).followRedirect(true).secure(spec -> spec.sslContext(http11SslContextSpec)).get().uri("http://localhost:" + disposableServer.port() + requestPath).responseContent().aggregate().asString();
StepVerifier.create(responseMono).expectNext(responseContent).expectComplete().verify(Duration.ofSeconds(5));
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class HttpRedirectTest method doTestBuffersForRedirectWithContentShouldBeReleased.
private void doTestBuffersForRedirectWithContentShouldBeReleased(String redirectResponseContent) {
final String initialPath = "/initial";
final String redirectPath = "/redirect";
disposableServer = createServer().route(r -> r.get(initialPath, (req, res) -> res.status(HttpResponseStatus.MOVED_PERMANENTLY).header(HttpHeaderNames.LOCATION, redirectPath).sendString(Mono.just(redirectResponseContent))).get(redirectPath, (req, res) -> res.send())).bindNow();
ConnectionProvider provider = ConnectionProvider.create("doTestBuffersForRedirectWithContentShouldBeReleased", 1);
final List<Integer> redirectBufferRefCounts = new ArrayList<>();
HttpClient.create(provider).doOnRequest((r, c) -> c.addHandler("test-buffer-released", new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
super.channelRead(ctx, msg);
if (initialPath.equals("/" + r.path()) && msg instanceof HttpContent) {
redirectBufferRefCounts.add(ReferenceCountUtil.refCnt(msg));
}
}
})).wiretap(true).followRedirect(true).get().uri("http://localhost:" + disposableServer.port() + initialPath).response().block(Duration.ofSeconds(30));
System.gc();
assertThat(redirectBufferRefCounts).as("The HttpContents belonging to the redirection response should all be released").containsOnly(0);
provider.disposeLater().block(Duration.ofSeconds(30));
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class HttpRedirectTest method testIssue843.
@Test
void testIssue843() {
final int server2Port = SocketUtils.findAvailableTcpPort();
DisposableServer server1 = null;
DisposableServer server2 = null;
try {
server1 = createServer().secure(spec -> spec.sslContext(Http11SslContextSpec.forServer(ssc.certificate(), ssc.privateKey()))).handle((req, res) -> res.sendRedirect("https://localhost:" + server2Port)).bindNow();
server2 = createServer(server2Port).host("localhost").secure(spec -> spec.sslContext(Http11SslContextSpec.forServer(ssc.certificate(), ssc.privateKey()))).handle((req, res) -> res.sendString(Mono.just("test"))).bindNow();
AtomicInteger peerPort = new AtomicInteger(0);
Http11SslContextSpec http11SslContextSpec = Http11SslContextSpec.forClient().configure(builder -> builder.trustManager(InsecureTrustManagerFactory.INSTANCE));
createClient(server1::address).followRedirect(true).secure(spec -> spec.sslContext(http11SslContextSpec)).doOnRequest((req, conn) -> peerPort.set(conn.channel().pipeline().get(SslHandler.class).engine().getPeerPort())).get().uri("/").responseContent().blockLast(Duration.ofSeconds(30));
assertThat(peerPort.get()).isEqualTo(server2Port);
} finally {
if (server1 != null) {
server1.disposeNow();
}
if (server2 != null) {
server2.disposeNow();
}
}
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class HttpRedirectTest method testHttpsRequestIfRedirectHttpToHttpsEnabled.
@Test
void testHttpsRequestIfRedirectHttpToHttpsEnabled() {
String message = "The client should receive the message";
disposableServer = createServer().host("localhost").secure(spec -> spec.sslContext(Http11SslContextSpec.forServer(ssc.certificate(), ssc.privateKey())), true).handle((request, response) -> response.sendString(Mono.just(message))).bindNow();
Http11SslContextSpec http11SslContextSpec = Http11SslContextSpec.forClient().configure(builder -> builder.trustManager(InsecureTrustManagerFactory.INSTANCE));
HttpClient client = HttpClient.create().secure(spec -> spec.sslContext(http11SslContextSpec));
String uri = String.format("https://%s:%d/for-test/123", disposableServer.host(), disposableServer.port());
StepVerifier.create(client.get().uri(uri).response((response, body) -> {
assertThat(response.status()).isEqualTo(HttpResponseStatus.OK);
return body.aggregate().asString();
})).expectNextMatches(message::equals).expectComplete().verify(Duration.ofSeconds(30));
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class HttpCompressionClientServerTests method serverCompressionEnabledBigResponse.
@ParameterizedCompressionTest
void serverCompressionEnabledBigResponse(HttpServer server, HttpClient client) throws Exception {
disposableServer = server.compress(4).handle((in, out) -> out.sendString(Mono.just("reply"))).bindNow(Duration.ofSeconds(10));
// don't activate compression on the client options to avoid auto-handling (which removes the header)
Tuple2<byte[], HttpHeaders> resp = // edit the header manually to attempt to trigger compression on server side
client.port(disposableServer.port()).headers(h -> h.add("accept-encoding", "gzip")).get().uri("/test").responseSingle((res, buf) -> buf.asByteArray().zipWith(Mono.just(res.responseHeaders()))).block(Duration.ofSeconds(10));
assertThat(resp).isNotNull();
assertThat(resp.getT2().get("content-encoding")).isEqualTo("gzip");
assertThat(new String(resp.getT1(), Charset.defaultCharset())).isNotEqualTo("reply");
GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(resp.getT1()));
byte[] deflatedBuf = new byte[1024];
int readable = gis.read(deflatedBuf);
gis.close();
assertThat(readable).isGreaterThan(0);
String deflated = new String(deflatedBuf, 0, readable, Charset.defaultCharset());
assertThat(deflated).isEqualTo("reply");
}
Aggregations