Search in sources :

Example 81 with NettyContext

use of reactor.ipc.netty.NettyContext 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 82 with NettyContext

use of reactor.ipc.netty.NettyContext 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 83 with NettyContext

use of reactor.ipc.netty.NettyContext 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)

Example 84 with NettyContext

use of reactor.ipc.netty.NettyContext 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 85 with NettyContext

use of reactor.ipc.netty.NettyContext 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)

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