Skip to content

Commit bc766b3

Browse files
authored
Test that WebSocket extensions are negotiated between the client and the backend (#127)
The proxy must not interfere in the handshake. Follows-up on #125 Signed-off-by: Thomas Segismont <[email protected]>
1 parent e70912f commit bc766b3

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/test/java/io/vertx/tests/TestBase.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.io.Closeable;
3535
import java.util.concurrent.*;
3636
import java.util.function.Consumer;
37+
import java.util.function.UnaryOperator;
3738

3839
/**
3940
* @author <a href="mailto:[email protected]">Julien Viet</a>
@@ -72,6 +73,10 @@ protected Closeable startProxy(SocketAddress backend) {
7273
}
7374

7475
protected Closeable startProxy(Consumer<HttpProxy> config) {
76+
return startProxy(UnaryOperator.identity(), config);
77+
}
78+
79+
protected Closeable startProxy(UnaryOperator<HttpServerOptions> proxyOptionsConfig, Consumer<HttpProxy> config) {
7580
CompletableFuture<Closeable> res = new CompletableFuture<>();
7681
vertx.deployVerticle(new AbstractVerticle() {
7782
HttpClient proxyClient;
@@ -80,7 +85,7 @@ protected Closeable startProxy(Consumer<HttpProxy> config) {
8085
@Override
8186
public void start(Promise<Void> startFuture) {
8287
proxyClient = vertx.createHttpClient(new HttpClientOptions(clientOptions));
83-
proxyServer = vertx.createHttpServer(new HttpServerOptions(serverOptions));
88+
proxyServer = vertx.createHttpServer(proxyOptionsConfig.apply(new HttpServerOptions(serverOptions)));
8489
proxy = HttpProxy.reverseProxy(proxyOptions, proxyClient);
8590
config.accept(proxy);
8691
proxyServer.requestHandler(proxy);

src/test/java/io/vertx/tests/WebSocketTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,42 @@ public Future<ProxyResponse> handleProxyRequest(ProxyContext context) {
191191
});
192192
}));
193193
}
194+
195+
@Test
196+
public void testWebSocketExtensionsNegotiatedBetweenClientAndBackend(TestContext ctx) {
197+
Async async = ctx.async();
198+
HttpServerOptions backendOptions = new HttpServerOptions().setPort(8081).setHost("localhost")
199+
.setPerFrameWebSocketCompressionSupported(false) // Disable extension in the backend
200+
.setPerMessageWebSocketCompressionSupported(false); // Disable extension in the backend
201+
SocketAddress backend = startHttpBackend(ctx, backendOptions, req -> {
202+
ctx.assertTrue(req.headers().contains("sec-websocket-extensions"));
203+
Future<ServerWebSocket> fut = req.toWebSocket();
204+
fut.onComplete(ctx.asyncAssertSuccess(ws -> {
205+
ws.handler(buff -> ws.writeTextMessage(buff.toString()));
206+
ws.closeHandler(v -> {
207+
async.complete();
208+
});
209+
}));
210+
});
211+
startProxy(proxyServerOptions -> {
212+
return proxyServerOptions
213+
.setPerFrameWebSocketCompressionSupported(true) // Enable extension in the proxy
214+
.setPerMessageWebSocketCompressionSupported(true); // Enable extension in the proxy
215+
}, httpProxy -> httpProxy.origin(backend));
216+
wsClient = vertx.createWebSocketClient(new WebSocketClientOptions()
217+
.setTryUsePerFrameCompression(true) // Enable extension in the client
218+
.setTryUsePerMessageCompression(true)); // Enable extension in the client
219+
WebSocketConnectOptions options = new WebSocketConnectOptions()
220+
.setPort(8080)
221+
.setHost("localhost")
222+
.setURI("/ws");
223+
wsClient.connect(options).onComplete(ctx.asyncAssertSuccess(ws -> {
224+
ctx.assertFalse(ws.headers().contains("sec-websocket-extensions"), "Expected extensions to be declined");
225+
ws.textMessageHandler(msg -> {
226+
ctx.assertEquals("hello", msg);
227+
ws.close();
228+
});
229+
ws.writeTextMessage("hello");
230+
}));
231+
}
194232
}

0 commit comments

Comments
 (0)