Search in sources :

Example 26 with NettyContext

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

the class HttpClientWSOperationsTest method failOnClientServerError.

private void failOnClientServerError(boolean clientError, boolean serverError, int serverStatus, String serverSubprotocol, String clientSubprotocol) {
    NettyContext httpServer = HttpServer.create(0).newRouter(routes -> routes.post("/login", (req, res) -> res.status(serverStatus).sendHeaders()).get("/ws", (req, res) -> {
        int token = Integer.parseInt(req.requestHeaders().get("Authorization"));
        if (token >= 400) {
            return res.status(token).send();
        }
        return res.sendWebsocket(serverSubprotocol, (i, o) -> o.sendString(Mono.just("test")));
    })).block(Duration.ofSeconds(30));
    Mono<HttpClientResponse> response = HttpClient.create(httpServer.address().getPort()).get("/ws", request -> Mono.just(request.failOnClientError(clientError).failOnServerError(serverError)).transform(req -> doLoginFirst(req, httpServer.address().getPort())).flatMapMany(req -> ws(req, clientSubprotocol))).switchIfEmpty(Mono.error(new Exception()));
    if (clientError || serverError) {
        StepVerifier.create(response).expectError().verify(Duration.ofSeconds(30));
    } else {
        StepVerifier.create(response).assertNext(res -> {
            assertThat(res.status().code()).isEqualTo(serverStatus == 200 ? 101 : serverStatus);
            res.dispose();
        }).expectComplete().verify(Duration.ofSeconds(30));
    }
    httpServer.dispose();
}
Also used : StepVerifier(reactor.test.StepVerifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Duration(java.time.Duration) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) WebsocketOutbound(reactor.ipc.netty.http.websocket.WebsocketOutbound) HttpServer(reactor.ipc.netty.http.server.HttpServer) NettyContext(reactor.ipc.netty.NettyContext)

Example 27 with NettyContext

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

the class HttpRedirectTest method testIssue253.

@Test
public void testIssue253() {
    NettyContext server = HttpServer.create(9991).newRouter(r -> r.get("/1", (req, res) -> res.sendRedirect("http://localhost:9991/3")).get("/2", (req, res) -> res.status(301).header(HttpHeaderNames.LOCATION, "http://localhost:9991/3").send()).get("/3", (req, res) -> res.status(200).sendString(Mono.just("OK")))).block(Duration.ofSeconds(30));
    HttpClient client = HttpClient.create(ops -> ops.connectAddress(() -> server.address()));
    String value = client.get("/1", req -> req.followRedirect().send()).flatMap(res -> res.receive().aggregate().asString()).block(Duration.ofSeconds(30));
    Assertions.assertThat(value).isEqualTo("OK");
    value = client.get("/1").flatMap(res -> res.receive().aggregate().asString()).block(Duration.ofSeconds(30));
    Assertions.assertThat(value).isNull();
    value = client.get("/2", req -> req.followRedirect().send()).flatMap(res -> res.receive().aggregate().asString()).block(Duration.ofSeconds(30));
    Assertions.assertThat(value).isEqualTo("OK");
    value = client.get("/2").flatMap(res -> res.receive().aggregate().asString()).block(Duration.ofSeconds(30));
    Assertions.assertThat(value).isNull();
    server.dispose();
}
Also used : Flux(reactor.core.publisher.Flux) StepVerifier(reactor.test.StepVerifier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) NettyContext(reactor.ipc.netty.NettyContext) HttpMethod(io.netty.handler.codec.http.HttpMethod) Assertions(org.assertj.core.api.Assertions) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) PoolResources(reactor.ipc.netty.resources.PoolResources) HttpServer(reactor.ipc.netty.http.server.HttpServer) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 28 with NettyContext

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

the class WebsocketTest method closePool.

@Test
public void closePool() {
    PoolResources pr = PoolResources.fixed("wstest", 1);
    NettyContext httpServer = HttpServer.create(0).newHandler((in, out) -> out.sendWebsocket((i, o) -> o.options(opt -> opt.flushOnEach()).sendString(Mono.just("test").delayElement(Duration.ofMillis(100)).repeat()))).block(Duration.ofSeconds(30));
    Flux<String> ws = HttpClient.create(opts -> opts.port(httpServer.address().getPort()).poolResources(pr)).ws("/").flatMapMany(in -> in.receiveWebsocket().aggregateFrames().receive().asString());
    StepVerifier.create(Flux.range(1, 10).concatMap(i -> ws.take(2).log())).expectNextSequence(Flux.range(1, 20).map(v -> "test").toIterable()).expectComplete().verify();
    httpServer.dispose();
    pr.dispose();
}
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) PoolResources(reactor.ipc.netty.resources.PoolResources) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 29 with NettyContext

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

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

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