AbstractWebSocketIntegrationTests.java 5.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright 2002-2016 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.reactive.socket.server;

18 19 20
import java.io.File;

import org.apache.tomcat.websocket.server.WsContextListener;
21 22 23 24 25 26 27 28 29 30 31 32
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.bootstrap.HttpServer;
33
import org.springframework.http.server.reactive.bootstrap.ReactorHttpServer;
34
import org.springframework.http.server.reactive.bootstrap.JettyHttpServer;
35
import org.springframework.http.server.reactive.bootstrap.RxNettyHttpServer;
36
import org.springframework.http.server.reactive.bootstrap.TomcatHttpServer;
37
import org.springframework.http.server.reactive.bootstrap.UndertowHttpServer;
38 39 40 41
import org.springframework.util.SocketUtils;
import org.springframework.web.reactive.DispatcherHandler;
import org.springframework.web.reactive.socket.server.support.HandshakeWebSocketService;
import org.springframework.web.reactive.socket.server.support.WebSocketHandlerAdapter;
42
import org.springframework.web.reactive.socket.server.upgrade.ReactorNettyRequestUpgradeStrategy;
43
import org.springframework.web.reactive.socket.server.upgrade.JettyRequestUpgradeStrategy;
44
import org.springframework.web.reactive.socket.server.upgrade.RxNettyRequestUpgradeStrategy;
45
import org.springframework.web.reactive.socket.server.upgrade.TomcatRequestUpgradeStrategy;
46
import org.springframework.web.reactive.socket.server.upgrade.UndertowRequestUpgradeStrategy;
47 48

/**
49 50 51
 * Base class for WebSocket integration tests.
 * Sub-classes must implement {@link #getWebConfigClass()} to return Spring
 * config class with handler mappings to {@code WebSocketHandler}'s.
52 53 54 55 56
 *
 * @author Rossen Stoyanchev
 */
@RunWith(Parameterized.class)
@SuppressWarnings({"unused", "WeakerAccess"})
57
public abstract class AbstractWebSocketIntegrationTests {
58 59 60 61 62 63 64 65 66 67

	protected int port;

	@Parameter(0)
	public HttpServer server;

	@Parameter(1)
	public Class<?> handlerAdapterConfigClass;


68
	@Parameters(name = "server [{0}]")
69
	public static Object[][] arguments() {
70
		File base = new File(System.getProperty("java.io.tmpdir"));
71
		return new Object[][] {
72
				{new ReactorHttpServer(), ReactorNettyConfig.class},
73
				{new RxNettyHttpServer(), RxNettyConfig.class},
74
				{new TomcatHttpServer(base.getAbsolutePath(), WsContextListener.class), TomcatConfig.class},
75 76
				{new UndertowHttpServer(), UndertowConfig.class},
				{new JettyHttpServer(), JettyConfig.class}
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
		};
	}


	@Before
	public void setup() throws Exception {
		this.port = SocketUtils.findAvailableTcpPort();
		this.server.setPort(this.port);
		this.server.setHandler(createHttpHandler());
		this.server.afterPropertiesSet();
		this.server.start();
	}

	private HttpHandler createHttpHandler() {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
		context.register(DispatcherConfig.class, this.handlerAdapterConfigClass);
		context.register(getWebConfigClass());
		context.refresh();
		return DispatcherHandler.toHttpHandler(context);
	}

	protected abstract Class<?> getWebConfigClass();

	@After
	public void tearDown() throws Exception {
		this.server.stop();
	}


	@Configuration
	static class DispatcherConfig {

		@Bean
		public DispatcherHandler webHandler() {
			return new DispatcherHandler();
		}
	}

	static abstract class AbstractHandlerAdapterConfig {

		@Bean
		public WebSocketHandlerAdapter handlerAdapter() {
119
			return new WebSocketHandlerAdapter(webSocketService());
120 121
		}

122 123 124 125 126 127
		@Bean
		public WebSocketService webSocketService() {
			return new HandshakeWebSocketService(getUpgradeStrategy());
		}

		protected abstract RequestUpgradeStrategy getUpgradeStrategy();
128 129 130

	}

131 132 133 134
	@Configuration
	static class ReactorNettyConfig extends AbstractHandlerAdapterConfig {

		@Override
135
		protected RequestUpgradeStrategy getUpgradeStrategy() {
136 137 138 139
			return new ReactorNettyRequestUpgradeStrategy();
		}
	}

140 141 142 143
	@Configuration
	static class RxNettyConfig extends AbstractHandlerAdapterConfig {

		@Override
144
		protected RequestUpgradeStrategy getUpgradeStrategy() {
145 146 147 148
			return new RxNettyRequestUpgradeStrategy();
		}
	}

149 150 151 152 153 154 155 156 157
	@Configuration
	static class TomcatConfig extends AbstractHandlerAdapterConfig {

		@Override
		protected RequestUpgradeStrategy getUpgradeStrategy() {
			return new TomcatRequestUpgradeStrategy();
		}
	}

158 159 160 161 162 163 164 165 166
	@Configuration
	static class UndertowConfig extends AbstractHandlerAdapterConfig {

		@Override
		protected RequestUpgradeStrategy getUpgradeStrategy() {
			return new UndertowRequestUpgradeStrategy();
		}
	}

167 168 169 170 171 172 173 174 175
	@Configuration
	static class JettyConfig extends AbstractHandlerAdapterConfig {

		@Override
		protected RequestUpgradeStrategy getUpgradeStrategy() {
			return new JettyRequestUpgradeStrategy();
		}
	}

176
}