Polish WebSocketSession

Update methods available on WebSocketSession interface.
Introduce DelegatingWebSocketSession interface.
This commit is contained in:
Rossen Stoyanchev
2013-08-09 09:38:13 -04:00
parent 14ac023e01
commit 01feae0ad5
36 changed files with 735 additions and 590 deletions

View File

@@ -25,17 +25,17 @@ import org.springframework.web.socket.WebSocketHandler;
import static org.mockito.Mockito.*;
/**
* Test fixture for {@link JettyWebSocketListenerAdapter}.
* Test fixture for {@link JettyWebSocketHandlerAdapter}.
*
* @author Rossen Stoyanchev
*/
public class JettyWebSocketListenerAdapterTests {
public class JettyWebSocketHandlerAdapterTests {
private JettyWebSocketListenerAdapter adapter;
private JettyWebSocketHandlerAdapter adapter;
private WebSocketHandler webSocketHandler;
private JettyWebSocketSessionAdapter webSocketSession;
private JettyWebSocketSession webSocketSession;
private Session session;
@@ -44,8 +44,8 @@ public class JettyWebSocketListenerAdapterTests {
public void setup() {
this.session = mock(Session.class);
this.webSocketHandler = mock(WebSocketHandler.class);
this.webSocketSession = new JettyWebSocketSessionAdapter();
this.adapter = new JettyWebSocketListenerAdapter(this.webSocketHandler, this.webSocketSession);
this.webSocketSession = new JettyWebSocketSession(null);
this.adapter = new JettyWebSocketHandlerAdapter(this.webSocketHandler, this.webSocketSession);
}
@Test

View File

@@ -31,17 +31,17 @@ import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
/**
* Test fixture for {@link StandardEndpointAdapter}.
* Test fixture for {@link StandardWebSocketHandlerAdapter}.
*
* @author Rossen Stoyanchev
*/
public class StandardEndpointAdapterTests {
public class StandardWebSocketHandlerAdapterTests {
private StandardEndpointAdapter adapter;
private StandardWebSocketHandlerAdapter adapter;
private WebSocketHandler webSocketHandler;
private StandardWebSocketSessionAdapter webSocketSession;
private StandardWebSocketSession webSocketSession;
private Session session;
@@ -50,8 +50,8 @@ public class StandardEndpointAdapterTests {
public void setup() {
this.session = mock(Session.class);
this.webSocketHandler = mock(WebSocketHandler.class);
this.webSocketSession = new StandardWebSocketSessionAdapter();
this.adapter = new StandardEndpointAdapter(this.webSocketHandler, this.webSocketSession);
this.webSocketSession = new StandardWebSocketSession(null, null, null);
this.adapter = new StandardWebSocketHandlerAdapter(this.webSocketHandler, this.webSocketSession);
}
@Test

View File

@@ -27,12 +27,12 @@ import javax.websocket.ClientEndpointConfig;
import javax.websocket.Endpoint;
import javax.websocket.WebSocketContainer;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.http.HttpHeaders;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.adapter.StandardEndpointAdapter;
import org.springframework.web.socket.adapter.WebSocketHandlerAdapter;
import static org.junit.Assert.*;
@@ -45,40 +45,92 @@ import static org.mockito.Mockito.*;
*/
public class StandardWebSocketClientTests {
private StandardWebSocketClient wsClient;
private WebSocketContainer wsContainer;
private WebSocketHandler wsHandler;
private HttpHeaders headers;
@Before
public void setup() {
this.headers = new HttpHeaders();
this.wsHandler = new WebSocketHandlerAdapter();
this.wsContainer = mock(WebSocketContainer.class);
this.wsClient = new StandardWebSocketClient(this.wsContainer);
}
@Test
public void doHandshake() throws Exception {
public void localAddress() throws Exception {
URI uri = new URI("ws://example.com/abc");
WebSocketSession session = this.wsClient.doHandshake(this.wsHandler, this.headers, uri);
assertNotNull(session.getLocalAddress());
assertEquals(80, session.getLocalAddress().getPort());
}
@Test
public void localAddressWss() throws Exception {
URI uri = new URI("wss://example.com/abc");
WebSocketSession session = this.wsClient.doHandshake(this.wsHandler, this.headers, uri);
assertNotNull(session.getLocalAddress());
assertEquals(443, session.getLocalAddress().getPort());
}
@Test(expected=IllegalArgumentException.class)
public void localAddressNoScheme() throws Exception {
URI uri = new URI("example.com/abc");
this.wsClient.doHandshake(this.wsHandler, this.headers, uri);
}
@Test
public void remoteAddress() throws Exception {
URI uri = new URI("wss://example.com/abc");
WebSocketSession session = this.wsClient.doHandshake(this.wsHandler, this.headers, uri);
assertNotNull(session.getRemoteAddress());
assertEquals("example.com", session.getRemoteAddress().getHostName());
assertEquals(443, session.getLocalAddress().getPort());
}
@Test
public void headersWebSocketSession() throws Exception {
URI uri = new URI("ws://example.com/abc");
List<String> subprotocols = Arrays.asList("abc");
List<String> protocols = Arrays.asList("abc");
this.headers.setSecWebSocketProtocol(protocols);
this.headers.add("foo", "bar");
HttpHeaders headers = new HttpHeaders();
headers.setSecWebSocketProtocol(subprotocols);
headers.add("foo", "bar");
WebSocketSession session = this.wsClient.doHandshake(this.wsHandler, this.headers, uri);
WebSocketHandler handler = new WebSocketHandlerAdapter();
WebSocketContainer webSocketContainer = mock(WebSocketContainer.class);
StandardWebSocketClient client = new StandardWebSocketClient(webSocketContainer);
WebSocketSession session = client.doHandshake(handler, headers, uri);
assertEquals(Collections.singletonMap("foo", Arrays.asList("bar")), session.getHandshakeHeaders());
}
ArgumentCaptor<Endpoint> endpointArg = ArgumentCaptor.forClass(Endpoint.class);
ArgumentCaptor<ClientEndpointConfig> configArg = ArgumentCaptor.forClass(ClientEndpointConfig.class);
ArgumentCaptor<URI> uriArg = ArgumentCaptor.forClass(URI.class);
@Test
public void headersClientEndpointConfigurator() throws Exception {
verify(webSocketContainer).connectToServer(endpointArg.capture(), configArg.capture(), uriArg.capture());
URI uri = new URI("ws://example.com/abc");
List<String> protocols = Arrays.asList("abc");
this.headers.setSecWebSocketProtocol(protocols);
this.headers.add("foo", "bar");
assertNotNull(endpointArg.getValue());
assertEquals(StandardEndpointAdapter.class, endpointArg.getValue().getClass());
this.wsClient.doHandshake(this.wsHandler, this.headers, uri);
ClientEndpointConfig config = configArg.getValue();
assertEquals(subprotocols, config.getPreferredSubprotocols());
ArgumentCaptor<Endpoint> arg1 = ArgumentCaptor.forClass(Endpoint.class);
ArgumentCaptor<ClientEndpointConfig> arg2 = ArgumentCaptor.forClass(ClientEndpointConfig.class);
ArgumentCaptor<URI> arg3 = ArgumentCaptor.forClass(URI.class);
verify(this.wsContainer).connectToServer(arg1.capture(), arg2.capture(), arg3.capture());
ClientEndpointConfig endpointConfig = arg2.getValue();
assertEquals(protocols, endpointConfig.getPreferredSubprotocols());
Map<String, List<String>> map = new HashMap<>();
config.getConfigurator().beforeRequest(map);
endpointConfig.getConfigurator().beforeRequest(map);
assertEquals(Collections.singletonMap("foo", Arrays.asList("bar")), map);
assertEquals(uri, uriArg.getValue());
assertEquals(uri, session.getUri());
assertEquals("example.com", session.getRemoteHostName());
}
}

View File

@@ -33,8 +33,8 @@ import org.springframework.util.CollectionUtils;
import org.springframework.util.SocketUtils;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.adapter.JettyWebSocketListenerAdapter;
import org.springframework.web.socket.adapter.JettyWebSocketSessionAdapter;
import org.springframework.web.socket.adapter.JettyWebSocketHandlerAdapter;
import org.springframework.web.socket.adapter.JettyWebSocketSession;
import org.springframework.web.socket.adapter.TextWebSocketHandlerAdapter;
import static org.junit.Assert.*;
@@ -113,8 +113,8 @@ public class JettyWebSocketClientTests {
resp.setAcceptedSubProtocol(req.getSubProtocols().get(0));
}
JettyWebSocketSessionAdapter session = new JettyWebSocketSessionAdapter();
return new JettyWebSocketListenerAdapter(webSocketHandler, session);
JettyWebSocketSession session = new JettyWebSocketSession(null);
return new JettyWebSocketHandlerAdapter(webSocketHandler, session);
}
});
}

View File

@@ -17,9 +17,12 @@
package org.springframework.web.socket.sockjs.transport.session;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import org.springframework.http.HttpHeaders;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.sockjs.support.frame.SockJsFrame;
@@ -29,6 +32,14 @@ import org.springframework.web.socket.sockjs.support.frame.SockJsFrame;
*/
public class TestSockJsSession extends AbstractSockJsSession {
private HttpHeaders headers;
private Principal principal;
private InetSocketAddress localAddress;
private InetSocketAddress remoteAddress;
private boolean active;
private final List<SockJsFrame> sockJsFrames = new ArrayList<>();
@@ -48,12 +59,76 @@ public class TestSockJsSession extends AbstractSockJsSession {
super(sessionId, config, handler);
}
@Override
public HttpHeaders getHandshakeHeaders() {
return this.headers;
}
/**
* @return the headers
*/
public HttpHeaders getHeaders() {
return this.headers;
}
/**
* @param headers the headers to set
*/
public void setHeaders(HttpHeaders headers) {
this.headers = headers;
}
/**
* @return the principal
*/
@Override
public Principal getPrincipal() {
return this.principal;
}
/**
* @param principal the principal to set
*/
public void setPrincipal(Principal principal) {
this.principal = principal;
}
/**
* @return the localAddress
*/
@Override
public InetSocketAddress getLocalAddress() {
return this.localAddress;
}
/**
* @param remoteAddress the remoteAddress to set
*/
public void setLocalAddress(InetSocketAddress localAddress) {
this.localAddress = localAddress;
}
/**
* @return the remoteAddress
*/
@Override
public InetSocketAddress getRemoteAddress() {
return this.remoteAddress;
}
/**
* @param remoteAddress the remoteAddress to set
*/
public void setRemoteAddress(InetSocketAddress remoteAddress) {
this.remoteAddress = remoteAddress;
}
@Override
public String getAcceptedProtocol() {
return this.subProtocol;
}
@Override
public void setAcceptedProtocol(String protocol) {
this.subProtocol = protocol;
}

View File

@@ -27,7 +27,6 @@ import org.junit.Test;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.sockjs.transport.session.WebSocketServerSockJsSession;
import org.springframework.web.socket.sockjs.transport.session.WebSocketServerSockJsSessionTests.TestWebSocketServerSockJsSession;
import org.springframework.web.socket.support.TestWebSocketSession;
@@ -61,7 +60,7 @@ public class WebSocketServerSockJsSessionTests extends BaseAbstractSockJsSession
public void isActive() throws Exception {
assertFalse(this.session.isActive());
this.session.initWebSocketSession(this.webSocketSession);
this.session.afterSessionInitialized(this.webSocketSession);
assertTrue(this.session.isActive());
this.webSocketSession.setOpen(false);
@@ -69,9 +68,9 @@ public class WebSocketServerSockJsSessionTests extends BaseAbstractSockJsSession
}
@Test
public void initWebSocketSession() throws Exception {
public void afterSessionInitialized() throws Exception {
this.session.initWebSocketSession(this.webSocketSession);
this.session.afterSessionInitialized(this.webSocketSession);
assertEquals("Open frame not sent",
Collections.singletonList(new TextMessage("o")), this.webSocketSession.getSentMessages());
@@ -110,7 +109,7 @@ public class WebSocketServerSockJsSessionTests extends BaseAbstractSockJsSession
@Test
public void sendMessageInternal() throws Exception {
this.session.initWebSocketSession(this.webSocketSession);
this.session.afterSessionInitialized(this.webSocketSession);
this.session.sendMessageInternal("x");
assertEquals(Arrays.asList(new TextMessage("o"), new TextMessage("a[\"x\"]")),
@@ -122,7 +121,7 @@ public class WebSocketServerSockJsSessionTests extends BaseAbstractSockJsSession
@Test
public void disconnect() throws Exception {
this.session.initWebSocketSession(this.webSocketSession);
this.session.afterSessionInitialized(this.webSocketSession);
this.session.close(CloseStatus.NOT_ACCEPTABLE);
assertEquals(CloseStatus.NOT_ACCEPTABLE, this.webSocketSession.getCloseStatus());

View File

@@ -17,11 +17,13 @@
package org.springframework.web.socket.support;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import org.springframework.http.HttpHeaders;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketMessage;
import org.springframework.web.socket.WebSocketSession;
@@ -37,13 +39,11 @@ public class TestWebSocketSession implements WebSocketSession {
private URI uri;
private boolean secure;
private Principal principal;
private String remoteHostName;
private InetSocketAddress localAddress;
private String remoteAddress;
private InetSocketAddress remoteAddress;
private String protocol;
@@ -53,6 +53,8 @@ public class TestWebSocketSession implements WebSocketSession {
private CloseStatus status;
private HttpHeaders headers;
/**
* @return the id
@@ -84,19 +86,24 @@ public class TestWebSocketSession implements WebSocketSession {
this.uri = uri;
}
/**
* @return the secure
*/
@Override
public boolean isSecure() {
return this.secure;
public HttpHeaders getHandshakeHeaders() {
return this.headers;
}
/**
* @param secure the secure to set
* @return the headers
*/
public void setSecure(boolean secure) {
this.secure = secure;
public HttpHeaders getHeaders() {
return this.headers;
}
/**
* @param headers the headers to set
*/
public void setHeaders(HttpHeaders headers) {
this.headers = headers;
}
/**
@@ -115,32 +122,32 @@ public class TestWebSocketSession implements WebSocketSession {
}
/**
* @return the remoteHostName
* @return the localAddress
*/
@Override
public String getRemoteHostName() {
return this.remoteHostName;
public InetSocketAddress getLocalAddress() {
return this.localAddress;
}
/**
* @param remoteHostName the remoteHostName to set
* @param remoteAddress the remoteAddress to set
*/
public void setRemoteHostName(String remoteHostName) {
this.remoteHostName = remoteHostName;
public void setLocalAddress(InetSocketAddress localAddress) {
this.localAddress = localAddress;
}
/**
* @return the remoteAddress
*/
@Override
public String getRemoteAddress() {
public InetSocketAddress getRemoteAddress() {
return this.remoteAddress;
}
/**
* @param remoteAddress the remoteAddress to set
*/
public void setRemoteAddress(String remoteAddress) {
public void setRemoteAddress(InetSocketAddress remoteAddress) {
this.remoteAddress = remoteAddress;
}