Search in sources :

Example 61 with DisposableServer

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));
}
Also used : StepVerifier(reactor.test.StepVerifier) Http11SslContextSpec(reactor.netty.http.Http11SslContextSpec) SocketUtils(reactor.netty.SocketUtils) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) HttpProtocol(reactor.netty.http.HttpProtocol) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Http2SslContextSpec(reactor.netty.http.Http2SslContextSpec) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BaseHttpTest(reactor.netty.BaseHttpTest) Tuple2(reactor.util.function.Tuple2) AtomicReference(java.util.concurrent.atomic.AtomicReference) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BeforeAll(org.junit.jupiter.api.BeforeAll) Duration(java.time.Duration) Connection(reactor.netty.Connection) LoopResources(reactor.netty.resources.LoopResources) HttpContent(io.netty.handler.codec.http.HttpContent) Assumptions.assumeThat(org.assertj.core.api.Assumptions.assumeThat) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) HttpMethod(io.netty.handler.codec.http.HttpMethod) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) BlockingQueue(java.util.concurrent.BlockingQueue) Mono(reactor.core.publisher.Mono) CertificateException(java.security.cert.CertificateException) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) HttpServer(reactor.netty.http.server.HttpServer) SslHandler(io.netty.handler.ssl.SslHandler) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) ConnectionProvider(reactor.netty.resources.ConnectionProvider) DisposableServer(reactor.netty.DisposableServer) Http11SslContextSpec(reactor.netty.http.Http11SslContextSpec) BaseHttpTest(reactor.netty.BaseHttpTest) Test(org.junit.jupiter.api.Test)

Example 62 with DisposableServer

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));
}
Also used : StepVerifier(reactor.test.StepVerifier) Http11SslContextSpec(reactor.netty.http.Http11SslContextSpec) SocketUtils(reactor.netty.SocketUtils) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) HttpProtocol(reactor.netty.http.HttpProtocol) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Http2SslContextSpec(reactor.netty.http.Http2SslContextSpec) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BaseHttpTest(reactor.netty.BaseHttpTest) Tuple2(reactor.util.function.Tuple2) AtomicReference(java.util.concurrent.atomic.AtomicReference) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BeforeAll(org.junit.jupiter.api.BeforeAll) Duration(java.time.Duration) Connection(reactor.netty.Connection) LoopResources(reactor.netty.resources.LoopResources) HttpContent(io.netty.handler.codec.http.HttpContent) Assumptions.assumeThat(org.assertj.core.api.Assumptions.assumeThat) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) HttpMethod(io.netty.handler.codec.http.HttpMethod) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) BlockingQueue(java.util.concurrent.BlockingQueue) Mono(reactor.core.publisher.Mono) CertificateException(java.security.cert.CertificateException) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) HttpServer(reactor.netty.http.server.HttpServer) SslHandler(io.netty.handler.ssl.SslHandler) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) ConnectionProvider(reactor.netty.resources.ConnectionProvider) DisposableServer(reactor.netty.DisposableServer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpContent(io.netty.handler.codec.http.HttpContent) ConnectionProvider(reactor.netty.resources.ConnectionProvider) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 63 with DisposableServer

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();
        }
    }
}
Also used : StepVerifier(reactor.test.StepVerifier) Http11SslContextSpec(reactor.netty.http.Http11SslContextSpec) SocketUtils(reactor.netty.SocketUtils) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) HttpProtocol(reactor.netty.http.HttpProtocol) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Http2SslContextSpec(reactor.netty.http.Http2SslContextSpec) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BaseHttpTest(reactor.netty.BaseHttpTest) Tuple2(reactor.util.function.Tuple2) AtomicReference(java.util.concurrent.atomic.AtomicReference) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BeforeAll(org.junit.jupiter.api.BeforeAll) Duration(java.time.Duration) Connection(reactor.netty.Connection) LoopResources(reactor.netty.resources.LoopResources) HttpContent(io.netty.handler.codec.http.HttpContent) Assumptions.assumeThat(org.assertj.core.api.Assumptions.assumeThat) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) HttpMethod(io.netty.handler.codec.http.HttpMethod) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) BlockingQueue(java.util.concurrent.BlockingQueue) Mono(reactor.core.publisher.Mono) CertificateException(java.security.cert.CertificateException) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) HttpServer(reactor.netty.http.server.HttpServer) SslHandler(io.netty.handler.ssl.SslHandler) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) ConnectionProvider(reactor.netty.resources.ConnectionProvider) DisposableServer(reactor.netty.DisposableServer) DisposableServer(reactor.netty.DisposableServer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Http11SslContextSpec(reactor.netty.http.Http11SslContextSpec) BaseHttpTest(reactor.netty.BaseHttpTest) Test(org.junit.jupiter.api.Test)

Example 64 with DisposableServer

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));
}
Also used : StepVerifier(reactor.test.StepVerifier) Http11SslContextSpec(reactor.netty.http.Http11SslContextSpec) SocketUtils(reactor.netty.SocketUtils) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) HttpProtocol(reactor.netty.http.HttpProtocol) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Http2SslContextSpec(reactor.netty.http.Http2SslContextSpec) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BaseHttpTest(reactor.netty.BaseHttpTest) Tuple2(reactor.util.function.Tuple2) AtomicReference(java.util.concurrent.atomic.AtomicReference) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BeforeAll(org.junit.jupiter.api.BeforeAll) Duration(java.time.Duration) Connection(reactor.netty.Connection) LoopResources(reactor.netty.resources.LoopResources) HttpContent(io.netty.handler.codec.http.HttpContent) Assumptions.assumeThat(org.assertj.core.api.Assumptions.assumeThat) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) HttpMethod(io.netty.handler.codec.http.HttpMethod) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) BlockingQueue(java.util.concurrent.BlockingQueue) Mono(reactor.core.publisher.Mono) CertificateException(java.security.cert.CertificateException) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) HttpServer(reactor.netty.http.server.HttpServer) SslHandler(io.netty.handler.ssl.SslHandler) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) ConnectionProvider(reactor.netty.resources.ConnectionProvider) DisposableServer(reactor.netty.DisposableServer) Http11SslContextSpec(reactor.netty.http.Http11SslContextSpec) BaseHttpTest(reactor.netty.BaseHttpTest) Test(org.junit.jupiter.api.Test)

Example 65 with DisposableServer

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");
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) StepVerifier(reactor.test.StepVerifier) SocketUtils(reactor.netty.SocketUtils) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BaseHttpTest(reactor.netty.BaseHttpTest) Tuple2(reactor.util.function.Tuple2) AtomicReference(java.util.concurrent.atomic.AtomicReference) Retention(java.lang.annotation.Retention) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) Charset(java.nio.charset.Charset) Duration(java.time.Duration) Schedulers(reactor.core.scheduler.Schedulers) MethodSource(org.junit.jupiter.params.provider.MethodSource) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) Target(java.lang.annotation.Target) Mono(reactor.core.publisher.Mono) ElementType(java.lang.annotation.ElementType) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) HttpServer(reactor.netty.http.server.HttpServer) DisposableServer(reactor.netty.DisposableServer) RetentionPolicy(java.lang.annotation.RetentionPolicy) HttpClient(reactor.netty.http.client.HttpClient) GZIPInputStream(java.util.zip.GZIPInputStream) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) ByteArrayInputStream(java.io.ByteArrayInputStream)

Aggregations

DisposableServer (reactor.netty.DisposableServer)137 Mono (reactor.core.publisher.Mono)84 Test (org.junit.jupiter.api.Test)79 Duration (java.time.Duration)71 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)71 AtomicReference (java.util.concurrent.atomic.AtomicReference)68 Flux (reactor.core.publisher.Flux)68 Connection (reactor.netty.Connection)67 TimeUnit (java.util.concurrent.TimeUnit)65 LoopResources (reactor.netty.resources.LoopResources)60 List (java.util.List)58 CountDownLatch (java.util.concurrent.CountDownLatch)57 DomainSocketAddress (io.netty.channel.unix.DomainSocketAddress)56 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)55 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)54 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)54 InsecureTrustManagerFactory (io.netty.handler.ssl.util.InsecureTrustManagerFactory)53 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)53 Assumptions.assumeThat (org.assertj.core.api.Assumptions.assumeThat)53 StepVerifier (reactor.test.StepVerifier)53