Add WebSocket integration tests w/ Java configuration
Issue: SPR-10835
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.socket;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.runners.Parameterized.Parameter;
|
||||
import org.springframework.context.Lifecycle;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.socket.client.WebSocketClient;
|
||||
import org.springframework.web.socket.server.DefaultHandshakeHandler;
|
||||
import org.springframework.web.socket.server.HandshakeHandler;
|
||||
import org.springframework.web.socket.server.RequestUpgradeStrategy;
|
||||
import org.springframework.web.socket.server.support.JettyRequestUpgradeStrategy;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Base class for WebSocket integration tests.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public abstract class AbstractWebSocketIntegrationTests {
|
||||
|
||||
private static Map<Class<?>, Class<?>> upgradeStrategyConfigTypes = new HashMap<Class<?>, Class<?>>();
|
||||
|
||||
static {
|
||||
upgradeStrategyConfigTypes.put(JettyTestServer.class, JettyUpgradeStrategyConfig.class);
|
||||
}
|
||||
|
||||
@Parameter(0)
|
||||
public TestServer server;
|
||||
|
||||
@Parameter(1)
|
||||
public WebSocketClient webSocketClient;
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
if (this.webSocketClient instanceof Lifecycle) {
|
||||
((Lifecycle) this.webSocketClient).start();
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public void teardown() throws Exception {
|
||||
try {
|
||||
if (this.webSocketClient instanceof Lifecycle) {
|
||||
((Lifecycle) this.webSocketClient).stop();
|
||||
}
|
||||
}
|
||||
finally {
|
||||
this.server.stop();
|
||||
}
|
||||
}
|
||||
|
||||
protected String getWsBaseUrl() {
|
||||
return "ws://localhost:" + this.server.getPort();
|
||||
}
|
||||
|
||||
protected Class<?> getUpgradeStrategyConfigClass() {
|
||||
return upgradeStrategyConfigTypes.get(this.server.getClass());
|
||||
}
|
||||
|
||||
|
||||
static abstract class AbstractRequestUpgradeStrategyConfig {
|
||||
|
||||
@Bean
|
||||
public HandshakeHandler handshakeHandler() {
|
||||
return new DefaultHandshakeHandler(requestUpgradeStrategy());
|
||||
}
|
||||
|
||||
public abstract RequestUpgradeStrategy requestUpgradeStrategy();
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
static class JettyUpgradeStrategyConfig extends AbstractRequestUpgradeStrategyConfig {
|
||||
|
||||
@Bean
|
||||
public RequestUpgradeStrategy requestUpgradeStrategy() {
|
||||
return new JettyRequestUpgradeStrategy();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.socket;
|
||||
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.servlet.ServletContextHandler;
|
||||
import org.eclipse.jetty.servlet.ServletHolder;
|
||||
import org.springframework.util.SocketUtils;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
|
||||
|
||||
/**
|
||||
* Jetty based {@link TestServer}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class JettyTestServer implements TestServer {
|
||||
|
||||
private final Server jettyServer;
|
||||
|
||||
private final int port;
|
||||
|
||||
|
||||
public JettyTestServer() {
|
||||
this.port = SocketUtils.findAvailableTcpPort();
|
||||
this.jettyServer = new Server(this.port);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPort() {
|
||||
return this.port;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(WebApplicationContext cxt) {
|
||||
ServletContextHandler handler = new ServletContextHandler();
|
||||
handler.addServlet(new ServletHolder(new DispatcherServlet(cxt)), "/");
|
||||
this.jettyServer.setHandler(handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() throws Exception {
|
||||
this.jettyServer.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() throws Exception {
|
||||
if (this.jettyServer.isRunning()) {
|
||||
this.jettyServer.stop();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.socket;
|
||||
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
/**
|
||||
* Contract for a test server to use for integration tests.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public interface TestServer {
|
||||
|
||||
int getPort();
|
||||
|
||||
void init(WebApplicationContext cxt);
|
||||
|
||||
void start() throws Exception;
|
||||
|
||||
void stop() throws Exception;
|
||||
|
||||
}
|
||||
@@ -108,11 +108,9 @@ public class JettyWebSocketClientTests {
|
||||
factory.setCreator(new WebSocketCreator() {
|
||||
@Override
|
||||
public Object createWebSocket(UpgradeRequest req, UpgradeResponse resp) {
|
||||
|
||||
if (!CollectionUtils.isEmpty(req.getSubProtocols())) {
|
||||
resp.setAcceptedSubProtocol(req.getSubProtocols().get(0));
|
||||
}
|
||||
|
||||
JettyWebSocketSession session = new JettyWebSocketSession(null, null);
|
||||
return new JettyWebSocketHandlerAdapter(webSocketHandler, session);
|
||||
}
|
||||
|
||||
@@ -18,81 +18,101 @@ package org.springframework.web.socket.server.config;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.web.context.support.GenericWebApplicationContext;
|
||||
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
import org.springframework.web.socket.AbstractWebSocketIntegrationTests;
|
||||
import org.springframework.web.socket.JettyTestServer;
|
||||
import org.springframework.web.socket.WebSocketHandler;
|
||||
import org.springframework.web.socket.adapter.TextWebSocketHandlerAdapter;
|
||||
import org.springframework.web.socket.server.support.WebSocketHttpRequestHandler;
|
||||
import org.springframework.web.socket.sockjs.SockJsHttpRequestHandler;
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
import org.springframework.web.socket.client.jetty.JettyWebSocketClient;
|
||||
import org.springframework.web.socket.server.HandshakeHandler;
|
||||
import org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Matchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
|
||||
/**
|
||||
* Test fixture for {@link WebSocketConfigurationSupport}.
|
||||
* Test fixture for WebSocket Java config support.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class WebSocketConfigurationTests {
|
||||
@RunWith(Parameterized.class)
|
||||
public class WebSocketConfigurationTests extends AbstractWebSocketIntegrationTests {
|
||||
|
||||
private DelegatingWebSocketConfiguration config;
|
||||
|
||||
private GenericWebApplicationContext context;
|
||||
@Parameters
|
||||
public static Iterable<Object[]> arguments() {
|
||||
return Arrays.asList(new Object[][] {
|
||||
{ new JettyTestServer(), new JettyWebSocketClient()} });
|
||||
};
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.config = new DelegatingWebSocketConfiguration();
|
||||
this.context = new GenericWebApplicationContext();
|
||||
this.context.refresh();
|
||||
@Test
|
||||
public void registerWebSocketHandler() throws Exception {
|
||||
|
||||
AnnotationConfigWebApplicationContext cxt = new AnnotationConfigWebApplicationContext();
|
||||
cxt.register(TestWebSocketConfigurer.class, getUpgradeStrategyConfigClass());
|
||||
|
||||
this.server.init(cxt);
|
||||
this.server.start();
|
||||
|
||||
WebSocketHandler clientHandler = Mockito.mock(WebSocketHandler.class);
|
||||
WebSocketHandler serverHandler = cxt.getBean(WebSocketHandler.class);
|
||||
|
||||
this.webSocketClient.doHandshake(clientHandler, getWsBaseUrl() + "/ws");
|
||||
|
||||
verify(serverHandler).afterConnectionEstablished(any(WebSocketSession.class));
|
||||
verify(clientHandler).afterConnectionEstablished(any(WebSocketSession.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void webSocket() throws Exception {
|
||||
public void registerWebSocketHandlerWithSockJS() throws Exception {
|
||||
|
||||
final WebSocketHandler handler = new TextWebSocketHandlerAdapter();
|
||||
AnnotationConfigWebApplicationContext cxt = new AnnotationConfigWebApplicationContext();
|
||||
cxt.register(TestWebSocketConfigurer.class, getUpgradeStrategyConfigClass());
|
||||
|
||||
WebSocketConfigurer configurer = new WebSocketConfigurer() {
|
||||
@Override
|
||||
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
|
||||
registry.addHandler(handler, "/h1");
|
||||
}
|
||||
};
|
||||
this.server.init(cxt);
|
||||
this.server.start();
|
||||
|
||||
this.config.setConfigurers(Arrays.asList(configurer));
|
||||
SimpleUrlHandlerMapping hm = (SimpleUrlHandlerMapping) this.config.webSocketHandlerMapping();
|
||||
hm.setApplicationContext(this.context);
|
||||
WebSocketHandler clientHandler = Mockito.mock(WebSocketHandler.class);
|
||||
WebSocketHandler serverHandler = cxt.getBean(WebSocketHandler.class);
|
||||
|
||||
Object actual = hm.getUrlMap().get("/h1");
|
||||
this.webSocketClient.doHandshake(clientHandler, getWsBaseUrl() + "/sockjs/websocket");
|
||||
|
||||
assertNotNull(actual);
|
||||
assertEquals(WebSocketHttpRequestHandler.class, actual.getClass());
|
||||
assertEquals(1, hm.getUrlMap().size());
|
||||
verify(serverHandler).afterConnectionEstablished(any(WebSocketSession.class));
|
||||
verify(clientHandler).afterConnectionEstablished(any(WebSocketSession.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void webSocketWithSockJS() throws Exception {
|
||||
|
||||
final WebSocketHandler handler = new TextWebSocketHandlerAdapter();
|
||||
@Configuration
|
||||
@EnableWebSocket
|
||||
static class TestWebSocketConfigurer implements WebSocketConfigurer {
|
||||
|
||||
WebSocketConfigurer configurer = new WebSocketConfigurer() {
|
||||
@Override
|
||||
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
|
||||
registry.addHandler(handler, "/h1").withSockJS();
|
||||
}
|
||||
};
|
||||
@Autowired
|
||||
private HandshakeHandler handshakeHandler; // can't rely on classpath for server detection
|
||||
|
||||
this.config.setConfigurers(Arrays.asList(configurer));
|
||||
SimpleUrlHandlerMapping hm = (SimpleUrlHandlerMapping) this.config.webSocketHandlerMapping();
|
||||
hm.setApplicationContext(this.context);
|
||||
|
||||
Object actual = hm.getUrlMap().get("/h1/**");
|
||||
@Override
|
||||
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
|
||||
|
||||
assertNotNull(actual);
|
||||
assertEquals(SockJsHttpRequestHandler.class, actual.getClass());
|
||||
assertEquals(1, hm.getUrlMap().size());
|
||||
registry.addHandler(serverHandler(), "/ws")
|
||||
.setHandshakeHandler(this.handshakeHandler);
|
||||
|
||||
registry.addHandler(serverHandler(), "/sockjs").withSockJS()
|
||||
.setTransportHandlerOverrides(new WebSocketTransportHandler(this.handshakeHandler));
|
||||
}
|
||||
|
||||
@Bean
|
||||
public WebSocketHandler serverHandler() {
|
||||
return Mockito.mock(WebSocketHandler.class);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user