Search in sources :

Example 11 with TextWebSocketHandler

use of org.springframework.web.socket.handler.TextWebSocketHandler in project spring-framework by spring-projects.

the class DefaultHandshakeHandlerTests method supportedSubProtocols.

@Test
public void supportedSubProtocols() throws Exception {
    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.<String, Object>emptyMap();
    this.handshakeHandler.doHandshake(this.request, this.response, handler, attributes);
    verify(this.upgradeStrategy).upgrade(this.request, this.response, "STOMP", Collections.<WebSocketExtension>emptyList(), null, handler, attributes);
}
Also used : WebSocketHttpHeaders(org.springframework.web.socket.WebSocketHttpHeaders) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) TextWebSocketHandler(org.springframework.web.socket.handler.TextWebSocketHandler) TextWebSocketHandler(org.springframework.web.socket.handler.TextWebSocketHandler) Test(org.junit.Test)

Example 12 with TextWebSocketHandler

use of org.springframework.web.socket.handler.TextWebSocketHandler in project spring-framework by spring-projects.

the class WebSocketServerSockJsSessionTests method afterSessionInitializedOpenFrameFirst.

@Test
@SuppressWarnings("resource")
public void afterSessionInitializedOpenFrameFirst() throws Exception {
    TextWebSocketHandler handler = new TextWebSocketHandler() {

        @Override
        public void afterConnectionEstablished(WebSocketSession session) throws Exception {
            session.sendMessage(new TextMessage("go go"));
        }
    };
    TestWebSocketServerSockJsSession session = new TestWebSocketServerSockJsSession(this.sockJsConfig, handler, null);
    session.initializeDelegateSession(this.webSocketSession);
    List<TextMessage> expected = Arrays.asList(new TextMessage("o"), new TextMessage("a[\"go go\"]"));
    assertEquals(expected, this.webSocketSession.getSentMessages());
}
Also used : TextWebSocketHandler(org.springframework.web.socket.handler.TextWebSocketHandler) TextMessage(org.springframework.web.socket.TextMessage) WebSocketSession(org.springframework.web.socket.WebSocketSession) TestWebSocketSession(org.springframework.web.socket.handler.TestWebSocketSession) TestWebSocketServerSockJsSession(org.springframework.web.socket.sockjs.transport.session.WebSocketServerSockJsSessionTests.TestWebSocketServerSockJsSession) Test(org.junit.Test)

Example 13 with TextWebSocketHandler

use of org.springframework.web.socket.handler.TextWebSocketHandler in project spring-framework by spring-projects.

the class JettyWebSocketClientTests method doHandshake.

@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());
}
Also used : WebSocketHttpHeaders(org.springframework.web.socket.WebSocketHttpHeaders) TextWebSocketHandler(org.springframework.web.socket.handler.TextWebSocketHandler) URI(java.net.URI) Test(org.junit.Test)

Example 14 with TextWebSocketHandler

use of org.springframework.web.socket.handler.TextWebSocketHandler in project spring-framework by spring-projects.

the class JettyWebSocketClientTests method doHandshakeWithTaskExecutor.

@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());
}
Also used : WebSocketHttpHeaders(org.springframework.web.socket.WebSocketHttpHeaders) SimpleAsyncTaskExecutor(org.springframework.core.task.SimpleAsyncTaskExecutor) TextWebSocketHandler(org.springframework.web.socket.handler.TextWebSocketHandler) URI(java.net.URI) Test(org.junit.Test)

Example 15 with TextWebSocketHandler

use of org.springframework.web.socket.handler.TextWebSocketHandler in project spring-framework by spring-projects.

the class WebSocketHandlerRegistrationTests method interceptorsWithAllowedOrigins.

@Test
public void interceptorsWithAllowedOrigins() {
    WebSocketHandler handler = new TextWebSocketHandler();
    HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
    this.registration.addHandler(handler, "/foo").addInterceptors(interceptor).setAllowedOrigins("http://mydomain1.com");
    List<Mapping> mappings = this.registration.getMappings();
    assertEquals(1, mappings.size());
    Mapping mapping = mappings.get(0);
    assertEquals(handler, mapping.webSocketHandler);
    assertEquals("/foo", mapping.path);
    assertEquals(2, mapping.interceptors.length);
    assertEquals(interceptor, mapping.interceptors[0]);
    assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
}
Also used : HttpSessionHandshakeInterceptor(org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) TextWebSocketHandler(org.springframework.web.socket.handler.TextWebSocketHandler) TextWebSocketHandler(org.springframework.web.socket.handler.TextWebSocketHandler) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)16 TextWebSocketHandler (org.springframework.web.socket.handler.TextWebSocketHandler)16 WebSocketHandler (org.springframework.web.socket.WebSocketHandler)12 WebSocketHttpHeaders (org.springframework.web.socket.WebSocketHttpHeaders)5 URI (java.net.URI)4 HttpSessionHandshakeInterceptor (org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor)4 HandshakeHandler (org.springframework.web.socket.server.HandshakeHandler)2 DefaultHandshakeHandler (org.springframework.web.socket.server.support.DefaultHandshakeHandler)2 SimpleAsyncTaskExecutor (org.springframework.core.task.SimpleAsyncTaskExecutor)1 TaskScheduler (org.springframework.scheduling.TaskScheduler)1 TextMessage (org.springframework.web.socket.TextMessage)1 WebSocketExtension (org.springframework.web.socket.WebSocketExtension)1 WebSocketSession (org.springframework.web.socket.WebSocketSession)1 LoggingWebSocketHandlerDecorator (org.springframework.web.socket.handler.LoggingWebSocketHandlerDecorator)1 TestWebSocketSession (org.springframework.web.socket.handler.TestWebSocketSession)1 WebSocketHandlerDecorator (org.springframework.web.socket.handler.WebSocketHandlerDecorator)1 SubProtocolWebSocketHandler (org.springframework.web.socket.messaging.SubProtocolWebSocketHandler)1 HandshakeInterceptor (org.springframework.web.socket.server.HandshakeInterceptor)1 OriginHandshakeInterceptor (org.springframework.web.socket.server.support.OriginHandshakeInterceptor)1 WebSocketTransportHandler (org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler)1