Search in sources :

Example 6 with HttpServer

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

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

the class ProxyClientIssue method startContentServer.

@Test
@Ignore
public void startContentServer() throws Exception {
    Random random = new Random(0);
    byte[] content = new byte[1024 * 10];
    random.nextBytes(content);
    HttpServer server = HttpServer.create(options -> options.host("0.0.0.0").port(CONTENT_SERVER_PORT).option(ChannelOption.SO_LINGER, -1));
    server.startRouterAndAwait(routes -> routes.get("/**", (req, res) -> res.header("Content-length", String.valueOf(content.length)).header("Content-type", "application/octet-stream").header("Connection", "Close").sendByteArray(Flux.just(content))));
}
Also used : Arrays(java.util.Arrays) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) HttpServerRequest(reactor.ipc.netty.http.server.HttpServerRequest) ChannelOption(io.netty.channel.ChannelOption) Set(java.util.Set) Random(java.util.Random) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Flux(reactor.core.publisher.Flux) List(java.util.List) ByteBuf(io.netty.buffer.ByteBuf) Ignore(org.junit.Ignore) Collections.addAll(java.util.Collections.addAll) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) URI(java.net.URI) ENGLISH(java.util.Locale.ENGLISH) HttpServer(reactor.ipc.netty.http.server.HttpServer) HttpServerResponse(reactor.ipc.netty.http.server.HttpServerResponse) OK(io.netty.handler.codec.http.HttpResponseStatus.OK) Random(java.util.Random) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) HttpServer(reactor.ipc.netty.http.server.HttpServer) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 8 with HttpServer

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

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

the class WebsocketTest method sendToWebsocketSubprotocol.

@Test
public void sendToWebsocketSubprotocol() throws InterruptedException {
    AtomicReference<String> serverSelectedProtocol = new AtomicReference<>();
    AtomicReference<String> clientSelectedProtocol = new AtomicReference<>();
    AtomicReference<String> clientSelectedProtocolWhenSimplyUpgrading = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    httpServer = HttpServer.create(0).newHandler((in, out) -> out.sendWebsocket("not,proto1", (i, o) -> {
        serverSelectedProtocol.set(i.selectedSubprotocol());
        latch.countDown();
        return i.receive().asString().doOnNext(System.err::println).then();
    })).block(Duration.ofSeconds(30));
    HttpClient.create(httpServer.address().getPort()).ws("/test", "proto1,proto2").flatMap(in -> {
        clientSelectedProtocolWhenSimplyUpgrading.set(in.receiveWebsocket().selectedSubprotocol());
        return in.receiveWebsocket((i, o) -> {
            clientSelectedProtocol.set(o.selectedSubprotocol());
            return o.sendString(Mono.just("HELLO" + o.selectedSubprotocol()));
        });
    }).block(Duration.ofSeconds(30));
    Assert.assertTrue(latch.await(30, TimeUnit.SECONDS));
    Assert.assertThat(serverSelectedProtocol.get(), is("proto1"));
    Assert.assertThat(clientSelectedProtocol.get(), is("proto1"));
    Assert.assertThat(clientSelectedProtocolWhenSimplyUpgrading.get(), is("proto1"));
}
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) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 10 with HttpServer

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

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