Search in sources :

Example 31 with NettyContext

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

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

use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.

the class HttpCookieHandlingTests method clientWithoutCookieGetsANewOneFromServer.

@Test
public void clientWithoutCookieGetsANewOneFromServer() {
    NettyContext server = HttpServer.create(0).newRouter(r -> r.get("/test", (req, resp) -> resp.addCookie(new DefaultCookie("cookie1", "test_value")).send(req.receive().log("server received")))).block(Duration.ofSeconds(30));
    Mono<Map<CharSequence, Set<Cookie>>> cookieResponse = HttpClient.create("localhost", server.address().getPort()).get("/test").flatMap(res -> Mono.just(res.cookies())).doOnSuccess(m -> System.out.println(m)).doOnError(t -> System.err.println("Failed requesting server: " + t.getMessage()));
    StepVerifier.create(cookieResponse).expectNextMatches(l -> {
        Set<Cookie> cookies = l.get("cookie1");
        return cookies.stream().filter(e -> e.value().equals("test_value")).findFirst().isPresent();
    }).expectComplete().verify(Duration.ofSeconds(30));
    server.dispose();
}
Also used : StepVerifier(reactor.test.StepVerifier) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) Duration(java.time.Duration) Map(java.util.Map) NettyContext(reactor.ipc.netty.NettyContext) HttpClient(reactor.ipc.netty.http.client.HttpClient) Set(java.util.Set) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Cookie(io.netty.handler.codec.http.cookie.Cookie) HttpServer(reactor.ipc.netty.http.server.HttpServer) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) Cookie(io.netty.handler.codec.http.cookie.Cookie) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) Map(java.util.Map) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 34 with NettyContext

use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.

the class HttpTests method streamAndPoolExplicitCompression.

@Test
public void streamAndPoolExplicitCompression() throws Exception {
    EmitterProcessor<String> ep = EmitterProcessor.create();
    NettyContext server = HttpServer.create(opts -> opts.port(0)).newRouter(r -> r.post("/hi", (req, res) -> req.receive().aggregate().asString().log().then(res.sendString(Flux.just("test")).then())).get("/stream", (req, res) -> req.receive().then(res.compression(true).options(op -> op.flushOnEach()).sendString(ep.log()).then()))).block(Duration.ofSeconds(30));
    String content = HttpClient.create(opts -> opts.port(server.address().getPort()).compression(true)).post("/hi", req -> req.sendString(Flux.just("1", "2", "3", "4", "5"))).flatMap(res -> res.receive().aggregate().asString().log()).block();
    Flux<String> f = HttpClient.create(opts -> opts.port(server.address().getPort()).compression(true)).get("/stream").flatMapMany(res -> res.receive().asString());
    System.out.println(content);
    StepVerifier.create(f).then(() -> ep.onNext("test1")).expectNext("test1").thenAwait(Duration.ofMillis(300)).then(() -> ep.onNext("test2")).thenAwait(Duration.ofMillis(300)).expectNext("test2").thenAwait(Duration.ofMillis(300)).then(() -> ep.onComplete()).verifyComplete();
    content = HttpClient.create(opts -> opts.port(server.address().getPort()).compression(true)).post("/hi", req -> req.sendString(Flux.just("1", "2", "3", "4", "5"))).flatMap(res -> res.receive().aggregate().asString().log()).block();
    server.dispose();
}
Also used : StepVerifier(reactor.test.StepVerifier) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Unpooled(io.netty.buffer.Unpooled) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) List(java.util.List) ByteBuf(io.netty.buffer.ByteBuf) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) EmitterProcessor(reactor.core.publisher.EmitterProcessor) NettyContext(reactor.ipc.netty.NettyContext) HttpClient(reactor.ipc.netty.http.client.HttpClient) Assertions(org.assertj.core.api.Assertions) Schedulers(reactor.core.scheduler.Schedulers) HttpClientException(reactor.ipc.netty.http.client.HttpClientException) HttpServer(reactor.ipc.netty.http.server.HttpServer) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 35 with NettyContext

use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.

the class HttpTests method httpRespondsEmpty.

@Test
public void httpRespondsEmpty() {
    NettyContext server = HttpServer.create(0).newRouter(r -> r.post("/test/{param}", (req, res) -> Mono.empty())).block(Duration.ofSeconds(30));
    HttpClient client = HttpClient.create("localhost", server.address().getPort());
    Mono<ByteBuf> content = client.post("/test/World", req -> req.header("Content-Type", "text/plain").sendString(Mono.just("Hello").log("client-send"))).flatMap(res -> res.receive().log("client-received").next()).doOnError(t -> System.err.println("Failed requesting server: " + t.getMessage()));
    StepVerifier.create(content).expectComplete().verify(Duration.ofSeconds(5000));
    server.dispose();
}
Also used : StepVerifier(reactor.test.StepVerifier) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Unpooled(io.netty.buffer.Unpooled) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) List(java.util.List) ByteBuf(io.netty.buffer.ByteBuf) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) EmitterProcessor(reactor.core.publisher.EmitterProcessor) NettyContext(reactor.ipc.netty.NettyContext) HttpClient(reactor.ipc.netty.http.client.HttpClient) Assertions(org.assertj.core.api.Assertions) Schedulers(reactor.core.scheduler.Schedulers) HttpClientException(reactor.ipc.netty.http.client.HttpClientException) HttpServer(reactor.ipc.netty.http.server.HttpServer) HttpClient(reactor.ipc.netty.http.client.HttpClient) ByteBuf(io.netty.buffer.ByteBuf) 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