Search in sources :

Example 36 with WebSocketHandler

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

the class DefaultHandshakeHandlerTests method supportedExtensions.

@Test
public void supportedExtensions() throws Exception {
    WebSocketExtension extension1 = new WebSocketExtension("ext1");
    WebSocketExtension extension2 = new WebSocketExtension("ext2");
    given(this.upgradeStrategy.getSupportedVersions()).willReturn(new String[] { "13" });
    given(this.upgradeStrategy.getSupportedExtensions(this.request)).willReturn(Arrays.asList(extension1));
    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.setSecWebSocketExtensions(Arrays.asList(extension1, extension2));
    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, null, Arrays.asList(extension1), null, handler, attributes);
}
Also used : WebSocketExtension(org.springframework.web.socket.WebSocketExtension) 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 37 with WebSocketHandler

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

the class PerConnectionWebSocketHandler method afterConnectionEstablished.

@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
    WebSocketHandler handler = this.provider.getHandler();
    this.handlers.put(session, handler);
    handler.afterConnectionEstablished(session);
}
Also used : WebSocketHandler(org.springframework.web.socket.WebSocketHandler)

Example 38 with WebSocketHandler

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

the class HttpReceivingTransportHandlerTests method handleRequest.

private void handleRequest(AbstractHttpReceivingTransportHandler transportHandler) throws Exception {
    WebSocketHandler wsHandler = mock(WebSocketHandler.class);
    AbstractSockJsSession session = new TestHttpSockJsSession("1", new StubSockJsServiceConfig(), wsHandler, null);
    transportHandler.initialize(new StubSockJsServiceConfig());
    transportHandler.handleRequest(this.request, this.response, wsHandler, session);
    assertEquals("text/plain;charset=UTF-8", this.response.getHeaders().getContentType().toString());
    verify(wsHandler).handleMessage(session, new TextMessage("x"));
}
Also used : TestHttpSockJsSession(org.springframework.web.socket.sockjs.transport.session.TestHttpSockJsSession) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) AbstractSockJsSession(org.springframework.web.socket.sockjs.transport.session.AbstractSockJsSession) StubSockJsServiceConfig(org.springframework.web.socket.sockjs.transport.session.StubSockJsServiceConfig) TextMessage(org.springframework.web.socket.TextMessage)

Example 39 with WebSocketHandler

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

the class HttpReceivingTransportHandlerTests method delegateMessageException.

@Test
public void delegateMessageException() throws Exception {
    StubSockJsServiceConfig sockJsConfig = new StubSockJsServiceConfig();
    this.servletRequest.setContent("[\"x\"]".getBytes("UTF-8"));
    WebSocketHandler wsHandler = mock(WebSocketHandler.class);
    TestHttpSockJsSession session = new TestHttpSockJsSession("1", sockJsConfig, wsHandler, null);
    session.delegateConnectionEstablished();
    willThrow(new Exception()).given(wsHandler).handleMessage(session, new TextMessage("x"));
    try {
        XhrReceivingTransportHandler transportHandler = new XhrReceivingTransportHandler();
        transportHandler.initialize(sockJsConfig);
        transportHandler.handleRequest(this.request, this.response, wsHandler, session);
        fail("Expected exception");
    } catch (SockJsMessageDeliveryException ex) {
        assertNull(session.getCloseStatus());
    }
}
Also used : TestHttpSockJsSession(org.springframework.web.socket.sockjs.transport.session.TestHttpSockJsSession) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) SockJsMessageDeliveryException(org.springframework.web.socket.sockjs.SockJsMessageDeliveryException) StubSockJsServiceConfig(org.springframework.web.socket.sockjs.transport.session.StubSockJsServiceConfig) SockJsMessageDeliveryException(org.springframework.web.socket.sockjs.SockJsMessageDeliveryException) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Example 40 with WebSocketHandler

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

the class HttpSessionHandshakeInterceptorTests method doNotCauseSessionCreation.

@Test
public void doNotCauseSessionCreation() throws Exception {
    Map<String, Object> attributes = new HashMap<>();
    WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
    HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
    interceptor.beforeHandshake(this.request, this.response, wsHandler, attributes);
    assertNull(this.servletRequest.getSession(false));
}
Also used : HashMap(java.util.HashMap) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) Test(org.junit.Test)

Aggregations

WebSocketHandler (org.springframework.web.socket.WebSocketHandler)44 Test (org.junit.Test)36 HashMap (java.util.HashMap)14 TextWebSocketHandler (org.springframework.web.socket.handler.TextWebSocketHandler)14 WebSocketHttpHeaders (org.springframework.web.socket.WebSocketHttpHeaders)5 SubProtocolWebSocketHandler (org.springframework.web.socket.messaging.SubProtocolWebSocketHandler)5 MockHttpSession (org.springframework.mock.web.test.MockHttpSession)4 TextMessage (org.springframework.web.socket.TextMessage)4 HandshakeHandler (org.springframework.web.socket.server.HandshakeHandler)4 HttpSessionHandshakeInterceptor (org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor)4 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)3 HandshakeInterceptor (org.springframework.web.socket.server.HandshakeInterceptor)3 DefaultHandshakeHandler (org.springframework.web.socket.server.support.DefaultHandshakeHandler)3 URI (java.net.URI)2 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)2 MessageHandler (org.springframework.messaging.MessageHandler)2 SimpAnnotationMethodMessageHandler (org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler)2 SimpleBrokerMessageHandler (org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler)2 StompBrokerRelayMessageHandler (org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler)2 DefaultUserDestinationResolver (org.springframework.messaging.simp.user.DefaultUserDestinationResolver)2