Search in sources :

Example 21 with HttpClientResponse

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

the class HttpCompressionClientServerTests method serverCompressionAlwaysEnabled.

@Test
public void serverCompressionAlwaysEnabled() throws Exception {
    HttpServer server = HttpServer.create(o -> o.port(0).compression(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 22 with HttpClientResponse

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

the class HttpCompressionClientServerTests method compressionServerDefaultClientDefaultIsNone.

@Test
public void compressionServerDefaultClientDefaultIsNone() throws Exception {
    HttpServer server = HttpServer.create(o -> o.port(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").block();
    String reply = resp.receive().asString().blockFirst();
    assertThat(resp.responseHeaders().get("Content-Encoding")).isNull();
    assertThat(reply).isEqualTo("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 23 with HttpClientResponse

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

the class HttpCompressionClientServerTests method trueEnabledIncludeContentEncoding.

@Test
public void trueEnabledIncludeContentEncoding() throws Exception {
    HttpServer server = HttpServer.create(o -> o.port(0).compression(true));
    NettyContext nettyContext = server.newHandler((in, out) -> out.sendString(Mono.just("reply"))).block(Duration.ofMillis(10_000));
    HttpClient client = HttpClient.create(o -> o.compression(true).connectAddress(() -> address(nettyContext)));
    HttpClientResponse res = client.get("/test", o -> {
        Assert.assertTrue(o.requestHeaders().contains("Accept-Encoding", "gzip", true));
        return o;
    }).block();
    res.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 24 with HttpClientResponse

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

the class HttpCompressionClientServerTests method serverCompressionPredicateFalse.

@Test
public void serverCompressionPredicateFalse() throws Exception {
    HttpServer server = HttpServer.create(o -> o.port(0).compression((req, res) -> 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();
    // check the server didn't send the gzip header, only transfer-encoding
    HttpHeaders headers = resp.responseHeaders();
    assertThat(headers.get("transFER-encoding")).isEqualTo("chunked");
    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 25 with HttpClientResponse

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

the class HttpCompressionClientServerTests method compressionActivatedOnClientAddsHeader.

@Test
public void compressionActivatedOnClientAddsHeader() {
    AtomicReference<String> zip = new AtomicReference<>("fail");
    HttpServer server = HttpServer.create(o -> o.port(0).compression(true));
    NettyContext nettyContext = server.newHandler((in, out) -> out.sendString(Mono.just("reply"))).block(Duration.ofMillis(10_000));
    HttpClient client = HttpClient.create(opt -> opt.compression(true).connectAddress(() -> address(nettyContext)));
    HttpClientResponse resp = client.get("/test", req -> {
        zip.set(req.requestHeaders().get("accept-encoding"));
        return req;
    }).block();
    assertThat(zip.get()).isEqualTo("gzip");
    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) AtomicReference(java.util.concurrent.atomic.AtomicReference) 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