Search in sources :

Example 16 with HttpClientResponse

use of reactor.ipc.netty.http.client.HttpClientResponse in project reactor-netty by reactor.

the class HttpServerTests method testConnectionCloseOnServerError.

@Test
public void testConnectionCloseOnServerError() throws Exception {
    Flux<String> content = Flux.range(1, 3).doOnNext(i -> {
        if (i == 3) {
            throw new RuntimeException("test");
        }
    }).map(i -> "foo " + i);
    NettyContext server = HttpServer.create(0).newHandler((req, res) -> res.sendString(content)).block(Duration.ofSeconds(30));
    HttpClientResponse r = HttpClient.create(ops -> ops.port(server.address().getPort())).get("/").block(Duration.ofSeconds(30));
    ByteBufFlux response = r.receive();
    StepVerifier.create(response).expectNextCount(2).expectError(IOException.class).verify(Duration.ofSeconds(30));
    FutureMono.from(r.context().channel().closeFuture()).block(Duration.ofSeconds(30));
    r.dispose();
    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) ByteBufFlux(reactor.ipc.netty.ByteBufFlux) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) IOException(java.io.IOException) BlockingNettyContext(reactor.ipc.netty.tcp.BlockingNettyContext) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 17 with HttpClientResponse

use of reactor.ipc.netty.http.client.HttpClientResponse in project reactor-netty by reactor.

the class NettyOptionsTest method afterChannelInitAfterChannelGroup.

@Test
public void afterChannelInitAfterChannelGroup() {
    // this test only differs from afterChannelInitThenChannelGroup in the order of the options
    ChannelGroup group = new DefaultChannelGroup(null);
    List<Channel> initializedChannels = new ArrayList<>();
    NettyContext nettyContext = HttpServer.create(opt -> opt.channelGroup(group).afterChannelInit(initializedChannels::add)).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).hasSize(1).hasSameElementsAs(initializedChannels).doesNotContain(nettyContext.channel());
    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) Channel(io.netty.channel.Channel) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) ArrayList(java.util.ArrayList) ChannelGroup(io.netty.channel.group.ChannelGroup) DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 18 with HttpClientResponse

use of reactor.ipc.netty.http.client.HttpClientResponse in project reactor-netty by reactor.

the class NettyOptionsTest method afterChannelInitThenChannelGroup.

@Test
public void afterChannelInitThenChannelGroup() {
    ChannelGroup group = new DefaultChannelGroup(null);
    List<Channel> initializedChannels = new ArrayList<>();
    NettyContext nettyContext = HttpServer.create(opt -> opt.afterChannelInit(initializedChannels::add).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).hasSize(1).hasSameElementsAs(initializedChannels).doesNotContain(nettyContext.channel());
    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) Channel(io.netty.channel.Channel) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) ArrayList(java.util.ArrayList) ChannelGroup(io.netty.channel.group.ChannelGroup) DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 19 with HttpClientResponse

use of reactor.ipc.netty.http.client.HttpClientResponse in project reactor-netty by reactor.

the class NettyOptionsTest method afterNettyContextInit.

@Test
public void afterNettyContextInit() {
    AtomicInteger readCount = new AtomicInteger();
    ChannelInboundHandlerAdapter handler = new ChannelInboundHandlerAdapter() {

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            readCount.incrementAndGet();
            super.channelRead(ctx, msg);
        }
    };
    String handlerName = "test";
    NettyContext nettyContext = HttpServer.create(opt -> opt.afterNettyContextInit(c -> c.addHandlerFirst(handlerName, handler))).start((req, resp) -> resp.sendNotFound()).getContext();
    HttpClientResponse response1 = HttpClient.create(opt -> opt.connectAddress(() -> nettyContext.address())).get("/", req -> req.failOnClientError(false).send()).block();
    assertThat(response1.status().code()).isEqualTo(404);
    response1.dispose();
    // the "main" context doesn't get enriched with handlers from options...
    assertThat(nettyContext.channel().pipeline().names()).doesNotContain(handlerName);
    // ...but the child channels that are created for requests are
    assertThat(readCount.get()).isEqualTo(1);
    HttpClientResponse response2 = HttpClient.create(opt -> opt.connectAddress(() -> nettyContext.address())).get("/", req -> req.failOnClientError(false).send()).block();
    // reactor handler was applied and produced a response
    assertThat(response2.status().code()).isEqualTo(404);
    response2.dispose();
    // BUT channelHandler wasn't applied a second time since not Shareable
    assertThat(readCount.get()).isEqualTo(1);
    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) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 20 with HttpClientResponse

use of reactor.ipc.netty.http.client.HttpClientResponse in project reactor-netty by reactor.

the class HttpCompressionClientServerTests method serverCompressionDefault.

@Test
public void serverCompressionDefault() throws Exception {
    HttpServer server = HttpServer.create(0);
    NettyContext nettyContext = server.newHandler((in, out) -> out.sendString(Mono.just("reply"))).block(Duration.ofMillis(10_000));
    HttpClient client = HttpClient.create(o -> o.connectAddress(() -> address(nettyContext)));
    HttpClientResponse resp = client.get("/test", req -> req.header("Accept-Encoding", "gzip")).block();
    assertThat(resp.responseHeaders().get("content-encoding")).isNull();
    String reply = resp.receive().asString().blockFirst();
    Assert.assertEquals("reply", reply);
    resp.dispose();
    nettyContext.dispose();
    nettyContext.onClose().block();
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) StepVerifier(reactor.test.StepVerifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) InetSocketAddress(java.net.InetSocketAddress) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) Flux(reactor.core.publisher.Flux) ByteArrayInputStream(java.io.ByteArrayInputStream) Ignore(org.junit.Ignore) Duration(java.time.Duration) NettyContext(reactor.ipc.netty.NettyContext) HttpClient(reactor.ipc.netty.http.client.HttpClient) Assert(org.junit.Assert) HttpServer(reactor.ipc.netty.http.server.HttpServer) HttpClient(reactor.ipc.netty.http.client.HttpClient) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) HttpServer(reactor.ipc.netty.http.server.HttpServer) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Aggregations

HttpClientResponse (reactor.ipc.netty.http.client.HttpClientResponse)30 Mono (reactor.core.publisher.Mono)29 Test (org.junit.Test)27 NettyContext (reactor.ipc.netty.NettyContext)27 HttpClient (reactor.ipc.netty.http.client.HttpClient)27 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)25 Duration (java.time.Duration)21 AtomicReference (java.util.concurrent.atomic.AtomicReference)21 Flux (reactor.core.publisher.Flux)21 StepVerifier (reactor.test.StepVerifier)21 InetSocketAddress (java.net.InetSocketAddress)20 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)19 HttpServer (reactor.ipc.netty.http.server.HttpServer)19 Ignore (org.junit.Ignore)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 Assert (org.junit.Assert)12 ByteArrayInputStream (java.io.ByteArrayInputStream)11 IOException (java.io.IOException)11 GZIPInputStream (java.util.zip.GZIPInputStream)11 Consumer (java.util.function.Consumer)10