Search in sources :

Example 6 with HttpClientResponse

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

the class HttpCompressionClientServerTests method serverCompressionEnabledBigResponse.

@Test
public void serverCompressionEnabledBigResponse() throws Exception {
    HttpServer server = HttpServer.create(o -> o.port(0).compression(4));
    NettyContext nettyContext = server.newHandler((in, out) -> out.sendString(Mono.just("reply"))).block(Duration.ofMillis(10_000));
    // don't activate compression on the client options to avoid auto-handling (which removes the header)
    HttpClient client = HttpClient.create(o -> o.connectAddress(() -> address(nettyContext)));
    HttpClientResponse resp = // edit the header manually to attempt to trigger compression on server side
    client.get("/test", req -> req.header("accept-encoding", "gzip")).block();
    assertThat(resp.responseHeaders().get("content-encoding")).isEqualTo("gzip");
    byte[] replyBuffer = resp.receive().aggregate().asByteArray().block();
    assertThat(new String(replyBuffer)).isNotEqualTo("reply");
    GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(replyBuffer));
    byte[] deflatedBuf = new byte[1024];
    int readable = gis.read(deflatedBuf);
    gis.close();
    assertThat(readable).isGreaterThan(0);
    String deflated = new String(deflatedBuf, 0, readable);
    assertThat(deflated).isEqualTo("reply");
    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) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) 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 7 with HttpClientResponse

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

the class HttpCompressionClientServerTests method serverCompressionEnabledSmallResponse.

@Test
public void serverCompressionEnabledSmallResponse() throws Exception {
    HttpServer server = HttpServer.create(o -> o.port(0).compression(25));
    NettyContext nettyContext = server.newHandler((in, out) -> out.header("content-length", "5").sendString(Mono.just("reply"))).block(Duration.ofMillis(10_000));
    // don't activate compression on the client options to avoid auto-handling (which removes the header)
    HttpClient client = HttpClient.create(o -> o.connectAddress(() -> address(nettyContext)));
    HttpClientResponse resp = // edit the header manually to attempt to trigger compression on server side
    client.get("/test", req -> req.header("Accept-Encoding", "gzip")).block();
    // check the server didn't send the gzip header, only transfer-encoding
    HttpHeaders headers = resp.responseHeaders();
    assertThat(headers.get("conTENT-encoding")).isNull();
    // check the server sent plain text
    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) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) 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 8 with HttpClientResponse

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

the class HttpCompressionClientServerTests method serverCompressionPredicateTrue.

@Test
public void serverCompressionPredicateTrue() throws Exception {
    HttpServer server = HttpServer.create(o -> o.port(0).compression((req, res) -> true));
    NettyContext nettyContext = server.newHandler((in, out) -> out.sendString(Mono.just("reply"))).block(Duration.ofMillis(10_000));
    // don't activate compression on the client options to avoid auto-handling (which removes the header)
    HttpClient client = HttpClient.create(o -> o.connectAddress(() -> address(nettyContext)));
    HttpClientResponse resp = // edit the header manually to attempt to trigger compression on server side
    client.get("/test", req -> req.header("Accept-Encoding", "gzip")).block();
    assertThat(resp.responseHeaders().get("content-encoding")).isEqualTo("gzip");
    byte[] replyBuffer = resp.receive().aggregate().asByteArray().block();
    assertThat(new String(replyBuffer)).isNotEqualTo("reply");
    GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(replyBuffer));
    byte[] deflatedBuf = new byte[1024];
    int readable = gis.read(deflatedBuf);
    gis.close();
    assertThat(readable).isGreaterThan(0);
    String deflated = new String(deflatedBuf, 0, readable);
    assertThat(deflated).isEqualTo("reply");
    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) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) 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 9 with HttpClientResponse

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

the class HttpCompressionClientServerTests method serverCompressionDisabled.

@Test
public void serverCompressionDisabled() throws Exception {
    HttpServer server = HttpServer.create(o -> o.port(0).compression(false));
    NettyContext nettyContext = server.newHandler((in, out) -> out.sendString(Mono.just("reply"))).block(Duration.ofMillis(10_000));
    // don't activate compression on the client options to avoid auto-handling (which removes the header)
    HttpClient client = HttpClient.create(o -> o.connectAddress(() -> address(nettyContext)));
    HttpClientResponse resp = // edit the header manually to attempt to trigger compression on server side
    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)

Example 10 with HttpClientResponse

use of reactor.ipc.netty.http.client.HttpClientResponse in project java-buildpack-dependency-builder by cloudfoundry.

the class NetworkLogging method response.

public static Function<Mono<HttpClientResponse>, Mono<HttpClientResponse>> response(String uri) {
    if (!LOGGER.isInfoEnabled()) {
        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 = asTime(System.currentTimeMillis() - startTimeHolder.get());
        HttpClientResponse response = responseHolder.get();
        if (response == null) {
            return;
        }
        LOGGER.info("{}  {} ({})", response.status().code(), uri, elapsed);
    });
}
Also used : Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) Logger(org.slf4j.Logger) Subscription(org.reactivestreams.Subscription) 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) 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