Search in sources :

Example 71 with NettyContext

use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.

the class HttpClientTest method testIssue303.

@Test
public void testIssue303() {
    NettyContext server = HttpServer.create(0).newHandler((req, resp) -> resp.sendString(Mono.just("OK"))).block(Duration.ofSeconds(30));
    Mono<String> content = HttpClient.create(server.address().getPort()).get("/", req -> req.sendByteArray(Mono.defer(() -> Mono.just("Hello".getBytes())))).flatMap(it -> it.receive().aggregate().asString());
    StepVerifier.create(content).expectNextMatches(s -> "OK".equals(s)).expectComplete().verify(Duration.ofSeconds(30));
    server.dispose();
}
Also used : HttpResources(reactor.ipc.netty.http.HttpResources) HttpVersion(io.netty.handler.codec.http.HttpVersion) StepVerifier(reactor.test.StepVerifier) URISyntaxException(java.net.URISyntaxException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TimeoutException(java.util.concurrent.TimeoutException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Unpooled(io.netty.buffer.Unpooled) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) Duration(java.time.Duration) CharsetUtil(io.netty.util.CharsetUtil) Path(java.nio.file.Path) HttpServer(reactor.ipc.netty.http.server.HttpServer) DirectProcessor(reactor.core.publisher.DirectProcessor) SslContext(io.netty.handler.ssl.SslContext) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) FutureMono(reactor.ipc.netty.FutureMono) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) CertificateException(java.security.cert.CertificateException) PoolResources(reactor.ipc.netty.resources.PoolResources) InetSocketAddress(java.net.InetSocketAddress) StandardCharsets(java.nio.charset.StandardCharsets) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) SSLException(javax.net.ssl.SSLException) Ignore(org.junit.Ignore) Paths(java.nio.file.Paths) TcpServer(reactor.ipc.netty.tcp.TcpServer) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) NettyContext(reactor.ipc.netty.NettyContext) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) HttpContentDecompressor(io.netty.handler.codec.http.HttpContentDecompressor) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) AbortedException(reactor.ipc.netty.channel.AbortedException) Assert(org.junit.Assert) InputStream(java.io.InputStream) Proxy(reactor.ipc.netty.options.ClientProxyOptions.Proxy) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 72 with NettyContext

use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.

the class HttpClientTest method gzip.

@Test
public void gzip() {
    String content = "HELLO WORLD";
    NettyContext c = HttpServer.create(opts -> opts.compression(true).port(0)).newHandler((req, res) -> res.sendString(Mono.just(content))).block();
    // verify gzip is negotiated (when no decoder)
    StepVerifier.create(HttpClient.create(c.address().getPort()).get("/", req -> req.followRedirect().addHeader("Accept-Encoding", "gzip").addHeader("Accept-Encoding", "deflate")).flatMap(r -> r.receive().aggregate().asString().zipWith(Mono.just(r.responseHeaders().get("Content-Encoding", ""))).zipWith(Mono.just(r)))).expectNextMatches(tuple -> {
        return !tuple.getT1().getT1().equals(content) && "gzip".equals(tuple.getT1().getT2());
    }).expectComplete().verify();
    // verify decoder does its job and removes the header
    StepVerifier.create(HttpClient.create(c.address().getPort()).get("/", req -> {
        req.context().addHandlerFirst("gzipDecompressor", new HttpContentDecompressor());
        return req.followRedirect().addHeader("Accept-Encoding", "gzip").addHeader("Accept-Encoding", "deflate");
    }).flatMap(r -> r.receive().aggregate().asString().zipWith(Mono.just(r.responseHeaders().get("Content-Encoding", ""))).zipWith(Mono.just(r)))).expectNextMatches(tuple -> {
        return tuple.getT1().getT1().equals(content) && "".equals(tuple.getT1().getT2());
    }).expectComplete().verify();
    c.dispose();
}
Also used : HttpResources(reactor.ipc.netty.http.HttpResources) HttpVersion(io.netty.handler.codec.http.HttpVersion) StepVerifier(reactor.test.StepVerifier) URISyntaxException(java.net.URISyntaxException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TimeoutException(java.util.concurrent.TimeoutException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Unpooled(io.netty.buffer.Unpooled) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) Duration(java.time.Duration) CharsetUtil(io.netty.util.CharsetUtil) Path(java.nio.file.Path) HttpServer(reactor.ipc.netty.http.server.HttpServer) DirectProcessor(reactor.core.publisher.DirectProcessor) SslContext(io.netty.handler.ssl.SslContext) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) FutureMono(reactor.ipc.netty.FutureMono) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) CertificateException(java.security.cert.CertificateException) PoolResources(reactor.ipc.netty.resources.PoolResources) InetSocketAddress(java.net.InetSocketAddress) StandardCharsets(java.nio.charset.StandardCharsets) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) SSLException(javax.net.ssl.SSLException) Ignore(org.junit.Ignore) Paths(java.nio.file.Paths) TcpServer(reactor.ipc.netty.tcp.TcpServer) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) NettyContext(reactor.ipc.netty.NettyContext) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) HttpContentDecompressor(io.netty.handler.codec.http.HttpContentDecompressor) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) AbortedException(reactor.ipc.netty.channel.AbortedException) Assert(org.junit.Assert) InputStream(java.io.InputStream) Proxy(reactor.ipc.netty.options.ClientProxyOptions.Proxy) NettyContext(reactor.ipc.netty.NettyContext) HttpContentDecompressor(io.netty.handler.codec.http.HttpContentDecompressor) Test(org.junit.Test)

Example 73 with NettyContext

use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.

the class HttpClientTest method abort.

@Test
public void abort() throws Exception {
    NettyContext x = TcpServer.create("localhost", 0).newHandler((in, out) -> in.receive().take(1).thenMany(Flux.defer(() -> out.context(c -> c.addHandlerFirst(new HttpResponseEncoder())).sendObject(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.ACCEPTED)).then(Mono.delay(Duration.ofSeconds(2)).then())))).block(Duration.ofSeconds(30));
    PoolResources pool = PoolResources.fixed("test", 1);
    HttpClient.create(opts -> opts.host("localhost").port(x.address().getPort()).poolResources(pool)).get("/").flatMap(r -> {
        r.dispose();
        return Mono.just(r.status().code());
    }).log().block(Duration.ofSeconds(30));
    HttpClientResponse resp = HttpClient.create(opts -> opts.host("localhost").port(x.address().getPort()).poolResources(pool)).get("/").log().block(Duration.ofSeconds(30));
    resp.dispose();
    resp = HttpClient.create(opts -> opts.host("localhost").port(x.address().getPort()).poolResources(pool)).get("/").log().block(Duration.ofSeconds(30));
    resp.dispose();
    x.dispose();
    pool.dispose();
}
Also used : HttpResources(reactor.ipc.netty.http.HttpResources) HttpVersion(io.netty.handler.codec.http.HttpVersion) StepVerifier(reactor.test.StepVerifier) URISyntaxException(java.net.URISyntaxException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TimeoutException(java.util.concurrent.TimeoutException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Unpooled(io.netty.buffer.Unpooled) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) Duration(java.time.Duration) CharsetUtil(io.netty.util.CharsetUtil) Path(java.nio.file.Path) HttpServer(reactor.ipc.netty.http.server.HttpServer) DirectProcessor(reactor.core.publisher.DirectProcessor) SslContext(io.netty.handler.ssl.SslContext) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) FutureMono(reactor.ipc.netty.FutureMono) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) CertificateException(java.security.cert.CertificateException) PoolResources(reactor.ipc.netty.resources.PoolResources) InetSocketAddress(java.net.InetSocketAddress) StandardCharsets(java.nio.charset.StandardCharsets) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) SSLException(javax.net.ssl.SSLException) Ignore(org.junit.Ignore) Paths(java.nio.file.Paths) TcpServer(reactor.ipc.netty.tcp.TcpServer) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) NettyContext(reactor.ipc.netty.NettyContext) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) HttpContentDecompressor(io.netty.handler.codec.http.HttpContentDecompressor) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) AbortedException(reactor.ipc.netty.channel.AbortedException) Assert(org.junit.Assert) InputStream(java.io.InputStream) Proxy(reactor.ipc.netty.options.ClientProxyOptions.Proxy) HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) PoolResources(reactor.ipc.netty.resources.PoolResources) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 74 with NettyContext

use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.

the class HttpRedirectTest method testIssue278.

@Test
public void testIssue278() {
    NettyContext server1 = HttpServer.create(8888).newRouter(r -> r.get("/1", (req, res) -> res.sendRedirect("/3")).get("/2", (req, res) -> res.sendRedirect("http://localhost:8888/3")).get("/3", (req, res) -> res.sendString(Mono.just("OK"))).get("/4", (req, res) -> res.sendRedirect("http://localhost:8889/1"))).block(Duration.ofSeconds(30));
    NettyContext server2 = HttpServer.create(8889).newRouter(r -> r.get("/1", (req, res) -> res.sendString(Mono.just("Other")))).block(Duration.ofSeconds(30));
    HttpClient client = HttpClient.create(8888);
    Mono<String> response = client.get("/1", req -> req.followRedirect()).flatMap(res -> res.receive().aggregate().asString());
    StepVerifier.create(response).expectNextMatches(s -> "OK".equals(s)).expectComplete().verify(Duration.ofSeconds(30));
    response = client.get("/2", req -> req.followRedirect()).flatMap(res -> res.receive().aggregate().asString());
    StepVerifier.create(response).expectNextMatches(s -> "OK".equals(s)).expectComplete().verify(Duration.ofSeconds(30));
    response = client.get("/4", req -> req.followRedirect()).flatMap(res -> res.receive().aggregate().asString());
    StepVerifier.create(response).expectNextMatches(s -> "Other".equals(s)).expectComplete().verify(Duration.ofSeconds(30));
    server1.dispose();
    server2.dispose();
}
Also used : Flux(reactor.core.publisher.Flux) StepVerifier(reactor.test.StepVerifier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) NettyContext(reactor.ipc.netty.NettyContext) HttpMethod(io.netty.handler.codec.http.HttpMethod) Assertions(org.assertj.core.api.Assertions) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) PoolResources(reactor.ipc.netty.resources.PoolResources) HttpServer(reactor.ipc.netty.http.server.HttpServer) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 75 with NettyContext

use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.

the class HttpRedirectTest method redirectTests.

private void redirectTests(String url) {
    AtomicInteger counter = new AtomicInteger(1);
    NettyContext server = HttpServer.create(0).newHandler((req, res) -> {
        if (req.uri().contains("/login") && req.method().equals(HttpMethod.POST) && counter.getAndDecrement() > 0) {
            return res.sendRedirect(url);
        } else {
            return res.status(200).send();
        }
    }).block(Duration.ofSeconds(30));
    PoolResources pool = PoolResources.fixed("test", 1);
    HttpClient client = HttpClient.create(ops -> ops.connectAddress(() -> server.address()).poolResources(pool));
    try {
        Flux.range(0, this.numberOfTests).concatMap(i -> client.post("/login", r -> r.followRedirect()).flatMap(r -> r.receive().then())).blockLast(Duration.ofSeconds(30));
    } finally {
        server.dispose();
    }
}
Also used : Flux(reactor.core.publisher.Flux) StepVerifier(reactor.test.StepVerifier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) NettyContext(reactor.ipc.netty.NettyContext) HttpMethod(io.netty.handler.codec.http.HttpMethod) Assertions(org.assertj.core.api.Assertions) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) PoolResources(reactor.ipc.netty.resources.PoolResources) HttpServer(reactor.ipc.netty.http.server.HttpServer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PoolResources(reactor.ipc.netty.resources.PoolResources) NettyContext(reactor.ipc.netty.NettyContext)

Aggregations

NettyContext (reactor.ipc.netty.NettyContext)92 Test (org.junit.Test)87 Mono (reactor.core.publisher.Mono)86 Duration (java.time.Duration)83 Flux (reactor.core.publisher.Flux)78 InetSocketAddress (java.net.InetSocketAddress)65 HttpServer (reactor.ipc.netty.http.server.HttpServer)63 HttpClient (reactor.ipc.netty.http.client.HttpClient)61 CountDownLatch (java.util.concurrent.CountDownLatch)60 StepVerifier (reactor.test.StepVerifier)60 AtomicReference (java.util.concurrent.atomic.AtomicReference)56 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)53 Unpooled (io.netty.buffer.Unpooled)50 TimeUnit (java.util.concurrent.TimeUnit)44 StandardCharsets (java.nio.charset.StandardCharsets)43 Ignore (org.junit.Ignore)43 SslContext (io.netty.handler.ssl.SslContext)42 SslContextBuilder (io.netty.handler.ssl.SslContextBuilder)42 InsecureTrustManagerFactory (io.netty.handler.ssl.util.InsecureTrustManagerFactory)42 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)42