Search in sources :

Example 16 with HttpServer

use of reactor.ipc.netty.http.server.HttpServer in project reactor-netty by reactor.

the class WebsocketTest method duplexEcho.

@Test
public void duplexEcho() throws Exception {
    int c = 10;
    CountDownLatch clientLatch = new CountDownLatch(c);
    CountDownLatch serverLatch = new CountDownLatch(c);
    FluxProcessor<String, String> server = ReplayProcessor.<String>create().serialize();
    FluxProcessor<String, String> client = ReplayProcessor.<String>create().serialize();
    server.log("server").subscribe(v -> serverLatch.countDown());
    client.log("client").subscribe(v -> clientLatch.countDown());
    httpServer = HttpServer.create(0).newHandler((in, out) -> out.sendWebsocket((i, o) -> o.sendString(i.receive().asString().take(c).subscribeWith(server)))).block(Duration.ofSeconds(30));
    Flux.interval(Duration.ofMillis(200)).map(Object::toString).subscribe(client::onNext);
    HttpClient.create(httpServer.address().getPort()).ws("/test").flatMap(in -> in.receiveWebsocket((i, o) -> o.options(opt -> opt.flushOnEach()).sendString(i.receive().asString().subscribeWith(client)))).subscribe();
    Assert.assertTrue(serverLatch.await(10, TimeUnit.SECONDS));
    Assert.assertTrue(clientLatch.await(10, TimeUnit.SECONDS));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) StepVerifier(reactor.test.StepVerifier) Test(org.junit.Test) FluxProcessor(reactor.core.publisher.FluxProcessor) Mono(reactor.core.publisher.Mono) WebSocketHandshakeException(io.netty.handler.codec.http.websocketx.WebSocketHandshakeException) PoolResources(reactor.ipc.netty.resources.PoolResources) AtomicReference(java.util.concurrent.atomic.AtomicReference) ReplayProcessor(reactor.core.publisher.ReplayProcessor) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) Duration(java.time.Duration) After(org.junit.After) NettyContext(reactor.ipc.netty.NettyContext) Assert(org.junit.Assert) HttpServer(reactor.ipc.netty.http.server.HttpServer) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 17 with HttpServer

use of reactor.ipc.netty.http.server.HttpServer in project reactor-netty by reactor.

the class HttpCompressionClientServerTests method serverCompressionDefault.

@Test
public void serverCompressionDefault() throws Exception {
    HttpServer server = HttpServer.create(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", 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 18 with HttpServer

use of reactor.ipc.netty.http.server.HttpServer 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 19 with HttpServer

use of reactor.ipc.netty.http.server.HttpServer 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 20 with HttpServer

use of reactor.ipc.netty.http.server.HttpServer 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)

Aggregations

HttpServer (reactor.ipc.netty.http.server.HttpServer)23 Test (org.junit.Test)21 Mono (reactor.core.publisher.Mono)21 Duration (java.time.Duration)20 Flux (reactor.core.publisher.Flux)20 NettyContext (reactor.ipc.netty.NettyContext)20 AtomicReference (java.util.concurrent.atomic.AtomicReference)18 Ignore (org.junit.Ignore)17 StepVerifier (reactor.test.StepVerifier)16 InetSocketAddress (java.net.InetSocketAddress)15 Assert (org.junit.Assert)15 HttpClient (reactor.ipc.netty.http.client.HttpClient)14 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)13 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)12 ByteArrayInputStream (java.io.ByteArrayInputStream)11 GZIPInputStream (java.util.zip.GZIPInputStream)11 HttpClientResponse (reactor.ipc.netty.http.client.HttpClientResponse)11 CountDownLatch (java.util.concurrent.CountDownLatch)7 TimeUnit (java.util.concurrent.TimeUnit)5 After (org.junit.After)5