Search in sources :

Example 6 with TextWebSocketHandler

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

the class WebSocketHandlerRegistrationTests method interceptorsPassedToSockJsRegistration.

@Test
public void interceptorsPassedToSockJsRegistration() {
    WebSocketHandler handler = new TextWebSocketHandler();
    HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
    this.registration.addHandler(handler, "/foo").addInterceptors(interceptor).setAllowedOrigins("http://mydomain1.com").withSockJS();
    List<Mapping> mappings = this.registration.getMappings();
    assertEquals(1, mappings.size());
    Mapping mapping = mappings.get(0);
    assertEquals(handler, mapping.webSocketHandler);
    assertEquals("/foo/**", mapping.path);
    assertNotNull(mapping.sockJsService);
    assertTrue(mapping.sockJsService.getAllowedOrigins().contains("http://mydomain1.com"));
    List<HandshakeInterceptor> interceptors = mapping.sockJsService.getHandshakeInterceptors();
    assertEquals(interceptor, interceptors.get(0));
    assertEquals(OriginHandshakeInterceptor.class, interceptors.get(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) HandshakeInterceptor(org.springframework.web.socket.server.HandshakeInterceptor) HttpSessionHandshakeInterceptor(org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor) OriginHandshakeInterceptor(org.springframework.web.socket.server.support.OriginHandshakeInterceptor) Test(org.junit.Test)

Example 7 with TextWebSocketHandler

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

the class WebSocketHandshakeTests method subProtocolNegotiation.

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

Example 8 with TextWebSocketHandler

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

the class WebSocketConnectionManagerTests method clientLifecycle.

@Test
public void clientLifecycle() throws Exception {
    TestLifecycleWebSocketClient client = new TestLifecycleWebSocketClient(false);
    WebSocketHandler handler = new TextWebSocketHandler();
    WebSocketConnectionManager manager = new WebSocketConnectionManager(client, handler, "/a");
    manager.startInternal();
    assertTrue(client.isRunning());
    manager.stopInternal();
    assertFalse(client.isRunning());
}
Also used : 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 9 with TextWebSocketHandler

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

the class WebSocketConnectionManagerTests method openConnection.

@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());
}
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) URI(java.net.URI) WebSocketHandlerDecorator(org.springframework.web.socket.handler.WebSocketHandlerDecorator) LoggingWebSocketHandlerDecorator(org.springframework.web.socket.handler.LoggingWebSocketHandlerDecorator) Test(org.junit.Test)

Example 10 with TextWebSocketHandler

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

the class SockJsWebSocketHandlerTests method getSubProtocolsNone.

@Test
public void getSubProtocolsNone() throws Exception {
    WebSocketHandler handler = new TextWebSocketHandler();
    TaskScheduler scheduler = mock(TaskScheduler.class);
    DefaultSockJsService service = new DefaultSockJsService(scheduler);
    WebSocketServerSockJsSession session = new WebSocketServerSockJsSession("1", service, handler, null);
    SockJsWebSocketHandler sockJsHandler = new SockJsWebSocketHandler(service, handler, session);
    assertNull(sockJsHandler.getSubProtocols());
}
Also used : WebSocketServerSockJsSession(org.springframework.web.socket.sockjs.transport.session.WebSocketServerSockJsSession) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) TextWebSocketHandler(org.springframework.web.socket.handler.TextWebSocketHandler) SubProtocolWebSocketHandler(org.springframework.web.socket.messaging.SubProtocolWebSocketHandler) TextWebSocketHandler(org.springframework.web.socket.handler.TextWebSocketHandler) TaskScheduler(org.springframework.scheduling.TaskScheduler) 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