Search in sources :

Example 1 with WebsocketOutbound

use of reactor.netty.http.websocket.WebsocketOutbound in project reactor-netty by reactor.

the class WebsocketTest method doTestIssue970.

private void doTestIssue970(boolean compress) {
    disposableServer = createServer().handle((req, res) -> res.sendWebsocket((in, out) -> out.sendString(Mono.just("test")), WebsocketServerSpec.builder().compress(compress).build())).bindNow();
    AtomicBoolean clientHandler = new AtomicBoolean();
    HttpClient client = createClient(disposableServer::address);
    String perMessageDeflateEncoder = "io.netty.handler.codec.http.websocketx.extensions.compression.PerMessageDeflateEncoder";
    BiFunction<WebsocketInbound, WebsocketOutbound, Mono<Tuple2<String, String>>> receiver = (in, out) -> {
        in.withConnection(conn -> clientHandler.set(conn.channel().pipeline().get(perMessageDeflateEncoder) != null));
        String header = in.headers().get(HttpHeaderNames.SEC_WEBSOCKET_EXTENSIONS);
        return in.receive().aggregate().asString().zipWith(Mono.just(header == null ? "null" : header));
    };
    Predicate<Tuple2<String, String>> predicate = t -> "test".equals(t.getT1()) && "null".equals(t.getT2());
    StepVerifier.create(client.websocket().uri("/").handle(receiver)).expectNextMatches(predicate).expectComplete().verify(Duration.ofSeconds(30));
    assertThat(clientHandler.get()).isFalse();
    if (compress) {
        predicate = t -> "test".equals(t.getT1()) && !"null".equals(t.getT2());
    }
    StepVerifier.create(client.websocket(WebsocketClientSpec.builder().compress(compress).build()).uri("/").handle(receiver)).expectNextMatches(predicate).expectComplete().verify(Duration.ofSeconds(30));
    assertThat(clientHandler.get()).isEqualTo(compress);
}
Also used : StepVerifier(reactor.test.StepVerifier) AbortedException(reactor.netty.channel.AbortedException) BiFunction(java.util.function.BiFunction) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BaseHttpTest(reactor.netty.BaseHttpTest) Unpooled(io.netty.buffer.Unpooled) WebsocketOutbound(reactor.netty.http.websocket.WebsocketOutbound) ConnectionObserver(reactor.netty.ConnectionObserver) Loggers(reactor.util.Loggers) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Logger(reactor.util.Logger) URI(java.net.URI) Predicate(java.util.function.Predicate) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) WebSocketClientHandshakeException(io.netty.handler.codec.http.websocketx.WebSocketClientHandshakeException) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebSocketCloseStatus(io.netty.handler.codec.http.websocketx.WebSocketCloseStatus) Tuple2(reactor.util.function.Tuple2) WebSocketHandshakeException(io.netty.handler.codec.http.websocketx.WebSocketHandshakeException) Scheduler(reactor.core.scheduler.Scheduler) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Nullable(reactor.util.annotation.Nullable) ArrayList(java.util.ArrayList) WebsocketServerSpec(reactor.netty.http.server.WebsocketServerSpec) Charset(java.nio.charset.Charset) PongWebSocketFrame(io.netty.handler.codec.http.websocketx.PongWebSocketFrame) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) ClientCookieEncoder(io.netty.handler.codec.http.cookie.ClientCookieEncoder) Schedulers(reactor.core.scheduler.Schedulers) CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) Connection(reactor.netty.Connection) WebSocketFrame(io.netty.handler.codec.http.websocketx.WebSocketFrame) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) PingWebSocketFrame(io.netty.handler.codec.http.websocketx.PingWebSocketFrame) Publisher(org.reactivestreams.Publisher) Mono(reactor.core.publisher.Mono) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Flux(reactor.core.publisher.Flux) WebsocketInbound(reactor.netty.http.websocket.WebsocketInbound) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) ConnectionProvider(reactor.netty.resources.ConnectionProvider) CorruptedFrameException(io.netty.handler.codec.CorruptedFrameException) ClientCookieDecoder(io.netty.handler.codec.http.cookie.ClientCookieDecoder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Tuple2(reactor.util.function.Tuple2) Mono(reactor.core.publisher.Mono) WebsocketOutbound(reactor.netty.http.websocket.WebsocketOutbound) WebsocketInbound(reactor.netty.http.websocket.WebsocketInbound)

Aggregations

Unpooled (io.netty.buffer.Unpooled)1 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)1 CorruptedFrameException (io.netty.handler.codec.CorruptedFrameException)1 HttpHeaderNames (io.netty.handler.codec.http.HttpHeaderNames)1 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1 ClientCookieDecoder (io.netty.handler.codec.http.cookie.ClientCookieDecoder)1 ClientCookieEncoder (io.netty.handler.codec.http.cookie.ClientCookieEncoder)1 CloseWebSocketFrame (io.netty.handler.codec.http.websocketx.CloseWebSocketFrame)1 PingWebSocketFrame (io.netty.handler.codec.http.websocketx.PingWebSocketFrame)1 PongWebSocketFrame (io.netty.handler.codec.http.websocketx.PongWebSocketFrame)1 TextWebSocketFrame (io.netty.handler.codec.http.websocketx.TextWebSocketFrame)1 WebSocketClientHandshakeException (io.netty.handler.codec.http.websocketx.WebSocketClientHandshakeException)1 WebSocketCloseStatus (io.netty.handler.codec.http.websocketx.WebSocketCloseStatus)1 WebSocketFrame (io.netty.handler.codec.http.websocketx.WebSocketFrame)1 WebSocketHandshakeException (io.netty.handler.codec.http.websocketx.WebSocketHandshakeException)1 URI (java.net.URI)1 Charset (java.nio.charset.Charset)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1