Search in sources :

Example 11 with WebSocketSession

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

the class AbstractSockJsIntegrationTests method fallbackAfterTransportFailure.

@Test
public void fallbackAfterTransportFailure() throws Exception {
    this.testFilter.sendErrorMap.put("/websocket", 200);
    this.testFilter.sendErrorMap.put("/xhr_streaming", 500);
    TestClientHandler handler = new TestClientHandler();
    initSockJsClient(createWebSocketTransport(), createXhrTransport());
    WebSocketSession session = this.sockJsClient.doHandshake(handler, this.baseUrl + "/echo").get();
    assertEquals("Fallback didn't occur", XhrClientSockJsSession.class, session.getClass());
    TextMessage message = new TextMessage("message1");
    session.sendMessage(message);
    handler.awaitMessage(message, 5000);
}
Also used : TextMessage(org.springframework.web.socket.TextMessage) WebSocketSession(org.springframework.web.socket.WebSocketSession) Test(org.junit.Test)

Example 12 with WebSocketSession

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

the class DefaultTransportRequestTests method connect.

@Test
public void connect() throws Exception {
    DefaultTransportRequest request = createTransportRequest(this.webSocketTransport, TransportType.WEBSOCKET);
    request.connect(null, this.connectFuture);
    WebSocketSession session = mock(WebSocketSession.class);
    this.webSocketTransport.getConnectCallback().onSuccess(session);
    assertSame(session, this.connectFuture.get());
}
Also used : WebSocketSession(org.springframework.web.socket.WebSocketSession) Test(org.junit.Test)

Example 13 with WebSocketSession

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

the class RestTemplateXhrTransportTests method connectFailure.

@Test
public void connectFailure() throws Exception {
    final HttpServerErrorException expected = new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR);
    RestOperations restTemplate = mock(RestOperations.class);
    given(restTemplate.execute((URI) any(), eq(HttpMethod.POST), any(), any())).willThrow(expected);
    final CountDownLatch latch = new CountDownLatch(1);
    connect(restTemplate).addCallback(new ListenableFutureCallback<WebSocketSession>() {

        @Override
        public void onSuccess(WebSocketSession result) {
        }

        @Override
        public void onFailure(Throwable ex) {
            if (ex == expected) {
                latch.countDown();
            }
        }
    });
    verifyNoMoreInteractions(this.webSocketHandler);
}
Also used : RestOperations(org.springframework.web.client.RestOperations) CountDownLatch(java.util.concurrent.CountDownLatch) HttpServerErrorException(org.springframework.web.client.HttpServerErrorException) WebSocketSession(org.springframework.web.socket.WebSocketSession) Test(org.junit.Test)

Example 14 with WebSocketSession

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

the class AbstractSockJsIntegrationTests method fallbackAfterConnectTimeout.

@Test(timeout = 5000)
public void fallbackAfterConnectTimeout() throws Exception {
    TestClientHandler clientHandler = new TestClientHandler();
    this.testFilter.sleepDelayMap.put("/xhr_streaming", 10000L);
    this.testFilter.sendErrorMap.put("/xhr_streaming", 503);
    initSockJsClient(createXhrTransport());
    this.sockJsClient.setConnectTimeoutScheduler(this.wac.getBean(ThreadPoolTaskScheduler.class));
    WebSocketSession clientSession = sockJsClient.doHandshake(clientHandler, this.baseUrl + "/echo").get();
    assertEquals("Fallback didn't occur", XhrClientSockJsSession.class, clientSession.getClass());
    TextMessage message = new TextMessage("message1");
    clientSession.sendMessage(message);
    clientHandler.awaitMessage(message, 5000);
    clientSession.close();
}
Also used : TextMessage(org.springframework.web.socket.TextMessage) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) WebSocketSession(org.springframework.web.socket.WebSocketSession) Test(org.junit.Test)

Example 15 with WebSocketSession

use of org.springframework.web.socket.WebSocketSession 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)

Aggregations

WebSocketSession (org.springframework.web.socket.WebSocketSession)34 Test (org.junit.Test)24 TextMessage (org.springframework.web.socket.TextMessage)11 URI (java.net.URI)8 TestWebSocketSession (org.springframework.web.socket.handler.TestWebSocketSession)3 IOException (java.io.IOException)2 Callable (java.util.concurrent.Callable)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)2 ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)2 ListenableFutureTask (org.springframework.util.concurrent.ListenableFutureTask)2 SettableListenableFuture (org.springframework.util.concurrent.SettableListenableFuture)2 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)2 WebSocketHandler (org.springframework.web.socket.WebSocketHandler)2 SubProtocolWebSocketHandler (org.springframework.web.socket.messaging.SubProtocolWebSocketHandler)2 WebSocketHttpRequestHandler (org.springframework.web.socket.server.support.WebSocketHttpRequestHandler)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 InetSocketAddress (java.net.InetSocketAddress)1 Principal (java.security.Principal)1 ArrayList (java.util.ArrayList)1