Search in sources :

Example 1 with PathSpec

use of org.eclipse.jetty.websocket.server.pathmap.PathSpec in project spark by perwendel.

the class WebSocketServletContextHandlerFactoryTest method testCreate_whenNoIdleTimeoutIsPresent.

@Test
public void testCreate_whenNoIdleTimeoutIsPresent() throws Exception {
    Map<String, WebSocketHandlerWrapper> webSocketHandlers = new HashMap<>();
    webSocketHandlers.put(webSocketPath, new WebSocketHandlerClassWrapper(WebSocketTestHandler.class));
    servletContextHandler = WebSocketServletContextHandlerFactory.create(webSocketHandlers, Optional.empty());
    WebSocketUpgradeFilter webSocketUpgradeFilter = (WebSocketUpgradeFilter) servletContextHandler.getAttribute("org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter");
    assertNotNull("Should return a WebSocketUpgradeFilter because we configured it to have one", webSocketUpgradeFilter);
    PathMappings.MappedResource<WebSocketCreator> mappedResource = webSocketUpgradeFilter.getMappings().getMatch("/websocket");
    WebSocketCreatorFactory.SparkWebSocketCreator sc = (WebSocketCreatorFactory.SparkWebSocketCreator) mappedResource.getResource();
    PathSpec pathSpec = (PathSpec) mappedResource.getPathSpec();
    assertEquals("Should return the WebSocket path specified when contexst handler was created", webSocketPath, pathSpec.getPathSpec());
    assertTrue("Should return true because handler should be an instance of the one we passed when it was created", sc.getHandler() instanceof WebSocketTestHandler);
}
Also used : WebSocketUpgradeFilter(org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter) HashMap(java.util.HashMap) PathSpec(org.eclipse.jetty.websocket.server.pathmap.PathSpec) PathMappings(org.eclipse.jetty.websocket.server.pathmap.PathMappings) WebSocketCreator(org.eclipse.jetty.websocket.servlet.WebSocketCreator) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with PathSpec

use of org.eclipse.jetty.websocket.server.pathmap.PathSpec in project spark by perwendel.

the class WebSocketServletContextHandlerFactoryTest method testCreate_whenTimeoutIsPresent.

@Test
public void testCreate_whenTimeoutIsPresent() throws Exception {
    final Integer timeout = Integer.valueOf(1000);
    Map<String, WebSocketHandlerWrapper> webSocketHandlers = new HashMap<>();
    webSocketHandlers.put(webSocketPath, new WebSocketHandlerClassWrapper(WebSocketTestHandler.class));
    servletContextHandler = WebSocketServletContextHandlerFactory.create(webSocketHandlers, Optional.of(timeout));
    WebSocketUpgradeFilter webSocketUpgradeFilter = (WebSocketUpgradeFilter) servletContextHandler.getAttribute("org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter");
    assertNotNull("Should return a WebSocketUpgradeFilter because we configured it to have one", webSocketUpgradeFilter);
    WebSocketServerFactory webSocketServerFactory = webSocketUpgradeFilter.getFactory();
    assertEquals("Timeout value should be the same as the timeout specified when context handler was created", timeout.longValue(), webSocketServerFactory.getPolicy().getIdleTimeout());
    PathMappings.MappedResource<WebSocketCreator> mappedResource = webSocketUpgradeFilter.getMappings().getMatch("/websocket");
    WebSocketCreatorFactory.SparkWebSocketCreator sc = (WebSocketCreatorFactory.SparkWebSocketCreator) mappedResource.getResource();
    PathSpec pathSpec = (PathSpec) mappedResource.getPathSpec();
    assertEquals("Should return the WebSocket path specified when context handler was created", webSocketPath, pathSpec.getPathSpec());
    assertTrue("Should return true because handler should be an instance of the one we passed when it was created", sc.getHandler() instanceof WebSocketTestHandler);
}
Also used : WebSocketUpgradeFilter(org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter) WebSocketServerFactory(org.eclipse.jetty.websocket.server.WebSocketServerFactory) HashMap(java.util.HashMap) PathSpec(org.eclipse.jetty.websocket.server.pathmap.PathSpec) PathMappings(org.eclipse.jetty.websocket.server.pathmap.PathMappings) WebSocketCreator(org.eclipse.jetty.websocket.servlet.WebSocketCreator) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

HashMap (java.util.HashMap)2 WebSocketUpgradeFilter (org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter)2 PathMappings (org.eclipse.jetty.websocket.server.pathmap.PathMappings)2 PathSpec (org.eclipse.jetty.websocket.server.pathmap.PathSpec)2 WebSocketCreator (org.eclipse.jetty.websocket.servlet.WebSocketCreator)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 WebSocketServerFactory (org.eclipse.jetty.websocket.server.WebSocketServerFactory)1