/** * Set the sub-protocols to use. If configured, specified sub-protocols will be * requested in the handshake through the {@code Sec-WebSocket-Protocol} header. The * resulting WebSocket session will contain the protocol accepted by the server, if * any. */ public void setSubProtocols(List<String> protocols) { this.headers.setSecWebSocketProtocol(protocols); }
@Test public void clientEndpointConfig() throws Exception { URI uri = new URI("ws://localhost/abc"); List<String> protocols = Collections.singletonList("abc"); this.headers.setSecWebSocketProtocol(protocols); this.wsClient.doHandshake(this.wsHandler, this.headers, uri).get(); ArgumentCaptor<ClientEndpointConfig> captor = ArgumentCaptor.forClass(ClientEndpointConfig.class); verify(this.wsContainer).connectToServer(any(Endpoint.class), captor.capture(), any(URI.class)); ClientEndpointConfig endpointConfig = captor.getValue(); assertEquals(protocols, endpointConfig.getPreferredSubprotocols()); }
@Test public void doHandshake() throws Exception { WebSocketHttpHeaders headers = new WebSocketHttpHeaders(); headers.setSecWebSocketProtocol(Arrays.asList("echo")); this.wsSession = this.client.doHandshake(new TextWebSocketHandler(), headers, new URI(this.wsUrl)).get(); assertEquals(this.wsUrl, this.wsSession.getUri().toString()); assertEquals("echo", this.wsSession.getAcceptedProtocol()); }
@Test public void doHandshakeWithTaskExecutor() throws Exception { WebSocketHttpHeaders headers = new WebSocketHttpHeaders(); headers.setSecWebSocketProtocol(Arrays.asList("echo")); this.client.setTaskExecutor(new SimpleAsyncTaskExecutor()); this.wsSession = this.client.doHandshake(new TextWebSocketHandler(), headers, new URI(this.wsUrl)).get(); assertEquals(this.wsUrl, this.wsSession.getUri().toString()); assertEquals("echo", this.wsSession.getAcceptedProtocol()); }
@Test public void handshakeHeaders() throws Exception { URI uri = new URI("ws://localhost/abc"); List<String> protocols = Collections.singletonList("abc"); this.headers.setSecWebSocketProtocol(protocols); this.headers.add("foo", "bar"); WebSocketSession session = this.wsClient.doHandshake(this.wsHandler, this.headers, uri).get(); assertEquals(1, session.getHandshakeHeaders().size()); assertEquals("bar", session.getHandshakeHeaders().getFirst("foo")); }
@Test public void openConnection() throws Exception { List<String> subprotocols = Arrays.asList("abc"); TestLifecycleWebSocketClient client = new TestLifecycleWebSocketClient(false); WebSocketHandler handler = new TextWebSocketHandler(); WebSocketConnectionManager manager = new WebSocketConnectionManager(client, handler , "/path/{id}", "123"); manager.setSubProtocols(subprotocols); manager.openConnection(); WebSocketHttpHeaders expectedHeaders = new WebSocketHttpHeaders(); expectedHeaders.setSecWebSocketProtocol(subprotocols); assertEquals(expectedHeaders, client.headers); assertEquals(new URI("/path/123"), client.uri); WebSocketHandlerDecorator loggingHandler = (WebSocketHandlerDecorator) client.webSocketHandler; assertEquals(LoggingWebSocketHandlerDecorator.class, loggingHandler.getClass()); assertSame(handler, loggingHandler.getDelegate()); }
@Test public void subProtocolNegotiation() throws Exception { WebSocketHttpHeaders headers = new WebSocketHttpHeaders(); headers.setSecWebSocketProtocol("foo"); URI url = new URI(getWsBaseUrl() + "/ws"); WebSocketSession session = this.webSocketClient.doHandshake(new TextWebSocketHandler(), headers, url).get(); assertEquals("foo", session.getAcceptedProtocol()); session.close(); }
@Test public void subProtocolCapableHandlerNoMatch() { given(this.upgradeStrategy.getSupportedVersions()).willReturn(new String[] {"13"}); this.servletRequest.setMethod("GET"); WebSocketHttpHeaders headers = new WebSocketHttpHeaders(this.request.getHeaders()); headers.setUpgrade("WebSocket"); headers.setConnection("Upgrade"); headers.setSecWebSocketVersion("13"); headers.setSecWebSocketKey("82/ZS2YHjEnUN97HLL8tbw=="); headers.setSecWebSocketProtocol("v10.stomp"); WebSocketHandler handler = new SubProtocolCapableHandler("v12.stomp", "v11.stomp"); Map<String, Object> attributes = Collections.<String, Object>emptyMap(); this.handshakeHandler.doHandshake(this.request, this.response, handler, attributes); verify(this.upgradeStrategy).upgrade(this.request, this.response, null, Collections.emptyList(), null, handler, attributes); }
@Test public void subProtocolCapableHandler() { given(this.upgradeStrategy.getSupportedVersions()).willReturn(new String[] {"13"}); this.servletRequest.setMethod("GET"); WebSocketHttpHeaders headers = new WebSocketHttpHeaders(this.request.getHeaders()); headers.setUpgrade("WebSocket"); headers.setConnection("Upgrade"); headers.setSecWebSocketVersion("13"); headers.setSecWebSocketKey("82/ZS2YHjEnUN97HLL8tbw=="); headers.setSecWebSocketProtocol("v11.stomp"); WebSocketHandler handler = new SubProtocolCapableHandler("v12.stomp", "v11.stomp"); Map<String, Object> attributes = Collections.<String, Object>emptyMap(); this.handshakeHandler.doHandshake(this.request, this.response, handler, attributes); verify(this.upgradeStrategy).upgrade(this.request, this.response, "v11.stomp", Collections.emptyList(), null, handler, attributes); }
@Test public void supportedSubProtocols() { this.handshakeHandler.setSupportedProtocols("stomp", "mqtt"); given(this.upgradeStrategy.getSupportedVersions()).willReturn(new String[] {"13"}); this.servletRequest.setMethod("GET"); WebSocketHttpHeaders headers = new WebSocketHttpHeaders(this.request.getHeaders()); headers.setUpgrade("WebSocket"); headers.setConnection("Upgrade"); headers.setSecWebSocketVersion("13"); headers.setSecWebSocketKey("82/ZS2YHjEnUN97HLL8tbw=="); headers.setSecWebSocketProtocol("STOMP"); WebSocketHandler handler = new TextWebSocketHandler(); Map<String, Object> attributes = Collections.emptyMap(); this.handshakeHandler.doHandshake(this.request, this.response, handler, attributes); verify(this.upgradeStrategy).upgrade(this.request, this.response, "STOMP", Collections.emptyList(), null, handler, attributes); }
@Override protected void openConnection() { logger.info("Connecting to WebSocket at " + getUri()); ClientWebSocketContainer.this.headers.setSecWebSocketProtocol(getSubProtocols()); ListenableFuture<WebSocketSession> future = this.client.doHandshake(ClientWebSocketContainer.this.webSocketHandler, ClientWebSocketContainer.this.headers, getUri()); future.addCallback(new ListenableFutureCallback<WebSocketSession>() { @Override public void onSuccess(WebSocketSession session) { ClientWebSocketContainer.this.clientSession = session; logger.info("Successfully connected"); ClientWebSocketContainer.this.connectionLatch.countDown(); } @Override public void onFailure(Throwable t) { logger.error("Failed to connect", t); ClientWebSocketContainer.this.openConnectionException = t; ClientWebSocketContainer.this.connectionLatch.countDown(); } }); }
/** * Set the sub-protocols to use. If configured, specified sub-protocols will be * requested in the handshake through the {@code Sec-WebSocket-Protocol} header. The * resulting WebSocket session will contain the protocol accepted by the server, if * any. */ public void setSubProtocols(List<String> protocols) { this.headers.setSecWebSocketProtocol(protocols); }
/** * Set the sub-protocols to use. If configured, specified sub-protocols will be * requested in the handshake through the {@code Sec-WebSocket-Protocol} header. The * resulting WebSocket session will contain the protocol accepted by the server, if * any. */ public void setSubProtocols(List<String> protocols) { this.headers.setSecWebSocketProtocol(protocols); }
@Override protected void openConnection() { logger.info("Connecting to WebSocket at " + getUri()); ClientWebSocketContainer.this.headers.setSecWebSocketProtocol(getSubProtocols()); ListenableFuture<WebSocketSession> future = this.client.doHandshake(ClientWebSocketContainer.this.webSocketHandler, ClientWebSocketContainer.this.headers, getUri()); future.addCallback(new ListenableFutureCallback<WebSocketSession>() { @Override public void onSuccess(WebSocketSession session) { ClientWebSocketContainer.this.clientSession = session; logger.info("Successfully connected"); ClientWebSocketContainer.this.connectionLatch.countDown(); } @Override public void onFailure(Throwable t) { logger.error("Failed to connect", t); ClientWebSocketContainer.this.openConnectionException = t; ClientWebSocketContainer.this.connectionLatch.countDown(); } }); }