WebSocketIntegrationTests.java 5.1 KB
Newer Older
1
/*
2
 * Copyright 2002-2015 the original author or authors.
3 4 5 6 7
 *
 * 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
 *
S
Sam Brannen 已提交
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9 10 11 12 13 14 15 16 17 18
 *
 * 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;

S
Sam Brannen 已提交
19
import java.net.URI;
20
import java.util.ArrayList;
S
Sam Brannen 已提交
21
import java.util.Arrays;
22 23 24
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
S
Sam Brannen 已提交
25

26 27 28
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
29 30
import org.junit.runners.Parameterized.Parameters;

31 32 33 34
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.client.jetty.JettyWebSocketClient;
J
Juergen Hoeller 已提交
35
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
36 37 38
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
39
import org.springframework.web.socket.handler.AbstractWebSocketHandler;
J
Juergen Hoeller 已提交
40 41
import org.springframework.web.socket.handler.TextWebSocketHandler;
import org.springframework.web.socket.server.support.DefaultHandshakeHandler;
42

43
import static org.junit.Assert.*;
44 45 46 47 48 49 50 51 52

/**
 * Client and server-side WebSocket integration tests.
 *
 * @author Rossen Stoyanchev
 */
@RunWith(Parameterized.class)
public class WebSocketIntegrationTests extends  AbstractWebSocketIntegrationTests {

53
	@Parameters(name = "server [{0}], client [{1}]")
54
	public static Iterable<Object[]> arguments() {
J
Juergen Hoeller 已提交
55
		return Arrays.asList(new Object[][] {
56
				{new JettyWebSocketTestServer(), new JettyWebSocketClient()},
57 58
				{new TomcatWebSocketTestServer(), new StandardWebSocketClient()},
				{new UndertowTestServer(), new JettyWebSocketClient()}
59
		});
J
Juergen Hoeller 已提交
60
	}
61 62 63 64


	@Override
	protected Class<?>[] getAnnotatedConfigClasses() {
65
		return new Class<?>[] { TestConfig.class };
66 67 68 69 70 71
	}

	@Test
	public void subProtocolNegotiation() throws Exception {
		WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
		headers.setSecWebSocketProtocol("foo");
72 73
		URI url = new URI(getWsBaseUrl() + "/ws");
		WebSocketSession session = this.webSocketClient.doHandshake(new TextWebSocketHandler(), headers, url).get();
74
		assertEquals("foo", session.getAcceptedProtocol());
75 76 77 78 79 80 81 82 83 84
		session.close();
	}

	// SPR-12727

	@Test
	public void unsolicitedPongWithEmptyPayload() throws Exception {

		String url = getWsBaseUrl() + "/ws";
		WebSocketSession session = this.webSocketClient.doHandshake(new AbstractWebSocketHandler() {}, url).get();
85 86 87 88

		TestWebSocketHandler serverHandler = this.wac.getBean(TestWebSocketHandler.class);
		serverHandler.setWaitMessageCount(1);

89 90 91 92 93 94
		session.sendMessage(new PongMessage());

		serverHandler.await();
		assertNull(serverHandler.getTransportError());
		assertEquals(1, serverHandler.getReceivedMessages().size());
		assertEquals(PongMessage.class, serverHandler.getReceivedMessages().get(0).getClass());
95 96 97 98 99
	}


	@Configuration
	@EnableWebSocket
100
	static class TestConfig implements WebSocketConfigurer {
101 102

		@Autowired
J
Juergen Hoeller 已提交
103
		private DefaultHandshakeHandler handshakeHandler;  // can't rely on classpath for server detection
104 105 106 107

		@Override
		public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
			this.handshakeHandler.setSupportedProtocols("foo", "bar", "baz");
108
			registry.addHandler(handler(), "/ws").setHandshakeHandler(this.handshakeHandler);
109 110 111
		}

		@Bean
112 113 114 115 116 117
		public TestWebSocketHandler handler() {
			return new TestWebSocketHandler();
		}

	}

118
	@SuppressWarnings("rawtypes")
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
	private static class TestWebSocketHandler extends AbstractWebSocketHandler {

		private List<WebSocketMessage> receivedMessages = new ArrayList<>();

		private int waitMessageCount;

		private final CountDownLatch latch = new CountDownLatch(1);

		private Throwable transportError;


		public void setWaitMessageCount(int waitMessageCount) {
			this.waitMessageCount = waitMessageCount;
		}

		public List<WebSocketMessage> getReceivedMessages() {
			return this.receivedMessages;
		}

		public Throwable getTransportError() {
			return this.transportError;
		}

		@Override
		public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
			this.receivedMessages.add(message);
			if (this.receivedMessages.size() >= this.waitMessageCount) {
				this.latch.countDown();
			}
		}

		@Override
		public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
			this.transportError = exception;
			this.latch.countDown();
		}

		public void await() throws InterruptedException {
			this.latch.await(5, TimeUnit.SECONDS);
158 159 160 161
		}
	}

}