Search in sources :

Example 26 with HttpClientResponse

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

the class HttpCompressionClientServerTests method compressionServerEnabledClientDisabledIsNone.

@Test
public void compressionServerEnabledClientDisabledIsNone() throws Exception {
    HttpServer server = HttpServer.create(o -> o.port(0).compression(true));
    String serverReply = "reply";
    NettyContext nettyContext = server.newHandler((in, out) -> out.sendString(Mono.just(serverReply))).block(Duration.ofMillis(10_000));
    HttpClient client = HttpClient.create(o -> o.compression(false).connectAddress(() -> address(nettyContext)));
    HttpClientResponse resp = client.get("/test").block();
    String reply = resp.receive().asString().blockFirst();
    assertThat(resp.responseHeaders().get("Content-Encoding")).isNull();
    assertThat(reply).isEqualTo(serverReply);
    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)

Example 27 with HttpClientResponse

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

the class HttpErrorTests method test.

@Test
public void test() {
    NettyContext server = HttpServer.create(0).newRouter(httpServerRoutes -> httpServerRoutes.get("/", (httpServerRequest, httpServerResponse) -> {
        return httpServerResponse.sendString(Mono.error(new IllegalArgumentException()));
    })).block(Duration.ofSeconds(30));
    HttpClient client = HttpClient.create(opt -> opt.host("localhost").port(server.address().getPort()).disablePool());
    HttpClientResponse r = client.get("/").block(Duration.ofSeconds(30));
    Mono<List<String>> result = r.receive().asString(StandardCharsets.UTF_8).collectList();
    StepVerifier.create(result).expectError(IOException.class).verify(Duration.ofSeconds(30));
    System.out.println("END");
    FutureMono.from(r.context().channel().closeFuture()).block(Duration.ofSeconds(30));
    r.dispose();
    server.dispose();
}
Also used : StepVerifier(reactor.test.StepVerifier) FutureMono(reactor.ipc.netty.FutureMono) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) 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) List(java.util.List) IOException(java.io.IOException) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 28 with HttpClientResponse

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

the class ChannelOperationsHandlerTest method doTestPublisherSenderOnCompleteFlushInProgress.

private void doTestPublisherSenderOnCompleteFlushInProgress(boolean useScheduler) {
    NettyContext server = HttpServer.create(0).newHandler((req, res) -> req.receive().asString().doOnNext(System.err::println).then(res.status(200).sendHeaders().then())).block(Duration.ofSeconds(30));
    Flux<String> flux = Flux.range(1, 257).map(count -> count + "");
    if (useScheduler) {
        flux.publishOn(Schedulers.single());
    }
    Mono<HttpClientResponse> client = HttpClient.create(server.address().getPort()).post("/", req -> req.sendString(flux));
    StepVerifier.create(client).expectNextMatches(res -> {
        res.dispose();
        return res.status().code() == 200;
    }).expectComplete().verify(Duration.ofSeconds(30));
    server.dispose();
}
Also used : StepVerifier(reactor.test.StepVerifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ByteBuffer(java.nio.ByteBuffer) Loggers(reactor.util.Loggers) SocketChannel(java.nio.channels.SocketChannel) Duration(java.time.Duration) Logger(reactor.util.Logger) HttpClient(reactor.ipc.netty.http.client.HttpClient) Schedulers(reactor.core.scheduler.Schedulers) SocketUtils(reactor.ipc.netty.SocketUtils) ExecutorService(java.util.concurrent.ExecutorService) HttpServer(reactor.ipc.netty.http.server.HttpServer) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) FutureMono(reactor.ipc.netty.FutureMono) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) PoolResources(reactor.ipc.netty.resources.PoolResources) InetSocketAddress(java.net.InetSocketAddress) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) Executors(java.util.concurrent.Executors) ServerSocketChannel(java.nio.channels.ServerSocketChannel) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) Ignore(org.junit.Ignore) NettyContext(reactor.ipc.netty.NettyContext) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) NettyContext(reactor.ipc.netty.NettyContext)

Example 29 with HttpClientResponse

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

the class ChannelOperationsHandlerTest method testChannelInactiveThrowsIOException.

@Test
public void testChannelInactiveThrowsIOException() throws Exception {
    ExecutorService threadPool = Executors.newCachedThreadPool();
    int abortServerPort = SocketUtils.findAvailableTcpPort();
    ConnectionAbortServer abortServer = new ConnectionAbortServer(abortServerPort);
    threadPool.submit(abortServer);
    if (!abortServer.await(10, TimeUnit.SECONDS)) {
        throw new IOException("Fail to start test server");
    }
    Mono<HttpClientResponse> response = HttpClient.create(ops -> ops.host("localhost").port(abortServerPort)).get("/", req -> req.sendString(Flux.just("a", "b", "c")));
    StepVerifier.create(response.log()).expectErrorMessage("Connection closed prematurely").verify();
    abortServer.close();
}
Also used : StepVerifier(reactor.test.StepVerifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ByteBuffer(java.nio.ByteBuffer) Loggers(reactor.util.Loggers) SocketChannel(java.nio.channels.SocketChannel) Duration(java.time.Duration) Logger(reactor.util.Logger) HttpClient(reactor.ipc.netty.http.client.HttpClient) Schedulers(reactor.core.scheduler.Schedulers) SocketUtils(reactor.ipc.netty.SocketUtils) ExecutorService(java.util.concurrent.ExecutorService) HttpServer(reactor.ipc.netty.http.server.HttpServer) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) FutureMono(reactor.ipc.netty.FutureMono) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) PoolResources(reactor.ipc.netty.resources.PoolResources) InetSocketAddress(java.net.InetSocketAddress) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) Executors(java.util.concurrent.Executors) ServerSocketChannel(java.nio.channels.ServerSocketChannel) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) Ignore(org.junit.Ignore) NettyContext(reactor.ipc.netty.NettyContext) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) ExecutorService(java.util.concurrent.ExecutorService) IOException(java.io.IOException) Test(org.junit.Test)

Example 30 with HttpClientResponse

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

the class NetworkLogging method response.

public static Function<Mono<HttpClientResponse>, Mono<HttpClientResponse>> response(String uri) {
    if (!RESPONSE_LOGGER.isDebugEnabled()) {
        return inbound -> inbound;
    }
    AtomicLong startTimeHolder = new AtomicLong();
    AtomicReference<HttpClientResponse> responseHolder = new AtomicReference<>();
    return inbound -> inbound.doOnSubscribe(s -> startTimeHolder.set(System.currentTimeMillis())).doOnNext(responseHolder::set).doFinally(signalType -> {
        String elapsed = TimeUtils.asTime(System.currentTimeMillis() - startTimeHolder.get());
        Optional.ofNullable(responseHolder.get()).ifPresent(response -> {
            List<String> warnings = response.responseHeaders().getAll(CF_WARNINGS);
            if (warnings.isEmpty()) {
                RESPONSE_LOGGER.debug("{}    {} ({})", response.status().code(), uri, elapsed);
            } else {
                RESPONSE_LOGGER.warn("{}    {} ({}) [{}]", response.status().code(), uri, elapsed, warnings.stream().collect(Collectors.joining(", ")));
            }
        });
    });
}
Also used : Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Mono(reactor.core.publisher.Mono) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) TimeUtils(org.cloudfoundry.util.TimeUtils) List(java.util.List) Subscription(org.reactivestreams.Subscription) Optional(java.util.Optional) AtomicLong(java.util.concurrent.atomic.AtomicLong) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference)

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