Search in sources :

Example 16 with NettyContext

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

the class HttpServerTests method sendFileSecure.

@Test
public void sendFileSecure() throws CertificateException, SSLException, URISyntaxException {
    Path largeFile = Paths.get(getClass().getResource("/largeFile.txt").toURI());
    SelfSignedCertificate ssc = new SelfSignedCertificate();
    SslContext sslServer = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    SslContext sslClient = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
    NettyContext context = HttpServer.create(opt -> opt.sslContext(sslServer)).newHandler((req, resp) -> resp.sendFile(largeFile)).block();
    HttpClientResponse response = HttpClient.create(opt -> opt.port(context.address().getPort()).sslContext(sslClient)).get("/foo").block(Duration.ofSeconds(120));
    context.dispose();
    context.onClose().block();
    String body = response.receive().aggregate().asString(StandardCharsets.UTF_8).block();
    assertThat(body).startsWith("This is an UTF-8 file that is larger than 1024 bytes. " + "It contains accents like é.").contains("1024 mark here -><- 1024 mark here").endsWith("End of File");
}
Also used : Path(java.nio.file.Path) HttpResources(reactor.ipc.netty.http.HttpResources) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) StepVerifier(reactor.test.StepVerifier) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) URISyntaxException(java.net.URISyntaxException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TimeoutException(java.util.concurrent.TimeoutException) NettyOutbound(reactor.ipc.netty.NettyOutbound) ByteBuffer(java.nio.ByteBuffer) Unpooled(io.netty.buffer.Unpooled) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) BlockingNettyContext(reactor.ipc.netty.tcp.BlockingNettyContext) Future(java.util.concurrent.Future) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Path(java.nio.file.Path) Context(reactor.util.context.Context) StandardOpenOption(java.nio.file.StandardOpenOption) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) FileSystem(java.nio.file.FileSystem) InetSocketAddress(java.net.InetSocketAddress) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) CountDownLatch(java.util.concurrent.CountDownLatch) SSLException(javax.net.ssl.SSLException) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) ByteBufFlux(reactor.ipc.netty.ByteBufFlux) HttpVersion(io.netty.handler.codec.http.HttpVersion) FluxSink(reactor.core.publisher.FluxSink) Tuple3(reactor.util.function.Tuple3) Tuple2(reactor.util.function.Tuple2) AsynchronousFileChannel(java.nio.channels.AsynchronousFileChannel) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) StandardCopyOption(java.nio.file.StandardCopyOption) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) ByteBuf(io.netty.buffer.ByteBuf) Assert(org.testng.Assert) HttpClient(reactor.ipc.netty.http.client.HttpClient) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) ExecutorService(java.util.concurrent.ExecutorService) TcpClient(reactor.ipc.netty.tcp.TcpClient) SslContext(io.netty.handler.ssl.SslContext) Files(java.nio.file.Files) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) FutureMono(reactor.ipc.netty.FutureMono) CompletionHandler(java.nio.channels.CompletionHandler) HttpMethod(io.netty.handler.codec.http.HttpMethod) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) CertificateException(java.security.cert.CertificateException) PoolResources(reactor.ipc.netty.resources.PoolResources) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) AtomicLong(java.util.concurrent.atomic.AtomicLong) Flux(reactor.core.publisher.Flux) Paths(java.nio.file.Paths) NettyContext(reactor.ipc.netty.NettyContext) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) FileSystems(java.nio.file.FileSystems) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) SslContext(io.netty.handler.ssl.SslContext) BlockingNettyContext(reactor.ipc.netty.tcp.BlockingNettyContext) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 17 with NettyContext

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

the class HttpServerTests method doTestIssue309.

private void doTestIssue309(String path, Consumer<? super HttpServerOptions.Builder> ops) {
    NettyContext server = HttpServer.create(ops).newHandler((req, res) -> res.sendString(Mono.just("Should not be reached"))).block();
    Mono<HttpResponseStatus> status = HttpClient.create(server.address().getPort()).get(path, req -> req.failOnClientError(false)).flatMap(res -> {
        res.dispose();
        HttpResponseStatus code = res.status();
        return Mono.just(code);
    });
    StepVerifier.create(status).expectNextMatches(HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE::equals).expectComplete().verify();
    server.dispose();
}
Also used : HttpResources(reactor.ipc.netty.http.HttpResources) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) StepVerifier(reactor.test.StepVerifier) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) URISyntaxException(java.net.URISyntaxException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TimeoutException(java.util.concurrent.TimeoutException) NettyOutbound(reactor.ipc.netty.NettyOutbound) ByteBuffer(java.nio.ByteBuffer) Unpooled(io.netty.buffer.Unpooled) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) BlockingNettyContext(reactor.ipc.netty.tcp.BlockingNettyContext) Future(java.util.concurrent.Future) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Path(java.nio.file.Path) Context(reactor.util.context.Context) StandardOpenOption(java.nio.file.StandardOpenOption) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) FileSystem(java.nio.file.FileSystem) InetSocketAddress(java.net.InetSocketAddress) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) CountDownLatch(java.util.concurrent.CountDownLatch) SSLException(javax.net.ssl.SSLException) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) ByteBufFlux(reactor.ipc.netty.ByteBufFlux) HttpVersion(io.netty.handler.codec.http.HttpVersion) FluxSink(reactor.core.publisher.FluxSink) Tuple3(reactor.util.function.Tuple3) Tuple2(reactor.util.function.Tuple2) AsynchronousFileChannel(java.nio.channels.AsynchronousFileChannel) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) StandardCopyOption(java.nio.file.StandardCopyOption) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) ByteBuf(io.netty.buffer.ByteBuf) Assert(org.testng.Assert) HttpClient(reactor.ipc.netty.http.client.HttpClient) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) ExecutorService(java.util.concurrent.ExecutorService) TcpClient(reactor.ipc.netty.tcp.TcpClient) SslContext(io.netty.handler.ssl.SslContext) Files(java.nio.file.Files) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) FutureMono(reactor.ipc.netty.FutureMono) CompletionHandler(java.nio.channels.CompletionHandler) HttpMethod(io.netty.handler.codec.http.HttpMethod) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) CertificateException(java.security.cert.CertificateException) PoolResources(reactor.ipc.netty.resources.PoolResources) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) AtomicLong(java.util.concurrent.atomic.AtomicLong) Flux(reactor.core.publisher.Flux) Paths(java.nio.file.Paths) NettyContext(reactor.ipc.netty.NettyContext) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) FileSystems(java.nio.file.FileSystems) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) BlockingNettyContext(reactor.ipc.netty.tcp.BlockingNettyContext) NettyContext(reactor.ipc.netty.NettyContext)

Example 18 with NettyContext

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

the class HttpServerTests method flushOnComplete.

@Test
public void flushOnComplete() {
    Flux<String> test = Flux.range(0, 100).map(n -> String.format("%010d", n));
    NettyContext c = HttpServer.create(0).newHandler((req, resp) -> resp.sendString(test.map(s -> s + "\n"))).block(Duration.ofSeconds(30));
    Flux<String> client = HttpClient.create(c.address().getPort()).get("/", out -> out.context(ctx -> ctx.addHandler(new LineBasedFrameDecoder(10)))).block(Duration.ofSeconds(30)).receive().asString();
    StepVerifier.create(client).expectNextSequence(test.toIterable()).expectComplete().verify(Duration.ofSeconds(30));
    c.dispose();
}
Also used : HttpResources(reactor.ipc.netty.http.HttpResources) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) StepVerifier(reactor.test.StepVerifier) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) URISyntaxException(java.net.URISyntaxException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TimeoutException(java.util.concurrent.TimeoutException) NettyOutbound(reactor.ipc.netty.NettyOutbound) ByteBuffer(java.nio.ByteBuffer) Unpooled(io.netty.buffer.Unpooled) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) BlockingNettyContext(reactor.ipc.netty.tcp.BlockingNettyContext) Future(java.util.concurrent.Future) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Path(java.nio.file.Path) Context(reactor.util.context.Context) StandardOpenOption(java.nio.file.StandardOpenOption) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) FileSystem(java.nio.file.FileSystem) InetSocketAddress(java.net.InetSocketAddress) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) CountDownLatch(java.util.concurrent.CountDownLatch) SSLException(javax.net.ssl.SSLException) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) ByteBufFlux(reactor.ipc.netty.ByteBufFlux) HttpVersion(io.netty.handler.codec.http.HttpVersion) FluxSink(reactor.core.publisher.FluxSink) Tuple3(reactor.util.function.Tuple3) Tuple2(reactor.util.function.Tuple2) AsynchronousFileChannel(java.nio.channels.AsynchronousFileChannel) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) StandardCopyOption(java.nio.file.StandardCopyOption) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) ByteBuf(io.netty.buffer.ByteBuf) Assert(org.testng.Assert) HttpClient(reactor.ipc.netty.http.client.HttpClient) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) ExecutorService(java.util.concurrent.ExecutorService) TcpClient(reactor.ipc.netty.tcp.TcpClient) SslContext(io.netty.handler.ssl.SslContext) Files(java.nio.file.Files) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) FutureMono(reactor.ipc.netty.FutureMono) CompletionHandler(java.nio.channels.CompletionHandler) HttpMethod(io.netty.handler.codec.http.HttpMethod) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) CertificateException(java.security.cert.CertificateException) PoolResources(reactor.ipc.netty.resources.PoolResources) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) AtomicLong(java.util.concurrent.atomic.AtomicLong) Flux(reactor.core.publisher.Flux) Paths(java.nio.file.Paths) NettyContext(reactor.ipc.netty.NettyContext) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) FileSystems(java.nio.file.FileSystems) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) BlockingNettyContext(reactor.ipc.netty.tcp.BlockingNettyContext) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 19 with NettyContext

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

the class NettyOptionsTest method afterChannelInit.

@Test
public void afterChannelInit() throws InterruptedException {
    List<Channel> initializedChannels = new ArrayList<>();
    NettyContext nettyContext = HttpServer.create(opt -> opt.afterChannelInit(initializedChannels::add)).start((req, resp) -> resp.sendNotFound()).getContext();
    assertThat(initializedChannels).hasSize(0);
    HttpClientResponse resp = HttpClient.create(opt -> opt.connectAddress(() -> nettyContext.address())).get("/", req -> req.failOnClientError(false).send()).block();
    assertThat(initializedChannels).hasSize(1).doesNotContain(nettyContext.channel());
    resp.dispose();
    nettyContext.dispose();
}
Also used : ChannelGroup(io.netty.channel.group.ChannelGroup) DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) ArrayList(java.util.ArrayList) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NettyContext(reactor.ipc.netty.NettyContext) HttpClient(reactor.ipc.netty.http.client.HttpClient) HttpServer(reactor.ipc.netty.http.server.HttpServer) Channel(io.netty.channel.Channel) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) ArrayList(java.util.ArrayList) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 20 with NettyContext

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

the class NettyOptionsTest method channelIsAddedToChannelGroup.

@Test
public void channelIsAddedToChannelGroup() {
    // create a ChannelGroup that never removes disconnected channels
    ChannelGroup group = new DefaultChannelGroup(null) {

        @Override
        public boolean remove(Object o) {
            System.err.println("removed " + o);
            return false;
        }
    };
    NettyContext nettyContext = HttpServer.create(opt -> opt.channelGroup(group)).start((req, resp) -> resp.sendNotFound()).getContext();
    HttpClientResponse resp = HttpClient.create(opt -> opt.connectAddress(() -> nettyContext.address())).get("/", req -> req.failOnClientError(false).send()).block();
    assertThat((Iterable<Channel>) group).doesNotContain(nettyContext.channel()).hasSize(1);
    resp.dispose();
    nettyContext.dispose();
}
Also used : DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) ChannelGroup(io.netty.channel.group.ChannelGroup) DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) ArrayList(java.util.ArrayList) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NettyContext(reactor.ipc.netty.NettyContext) HttpClient(reactor.ipc.netty.http.client.HttpClient) HttpServer(reactor.ipc.netty.http.server.HttpServer) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) Channel(io.netty.channel.Channel) ChannelGroup(io.netty.channel.group.ChannelGroup) DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

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