Improve HttpSessionHandshakeInterceptor

Use explicit flag whether to copy all attributes.
This commit is contained in:
Rossen Stoyanchev
2014-10-14 09:01:09 -04:00
parent de11cd8791
commit 3056301015
2 changed files with 99 additions and 36 deletions

View File

@@ -24,6 +24,7 @@ import java.util.Set;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.mock.web.test.MockHttpSession;
import org.springframework.mock.web.test.MockServletContext;
import org.springframework.web.socket.AbstractHttpRequestTests;
import org.springframework.web.socket.WebSocketHandler;
@@ -38,28 +39,29 @@ public class HttpSessionHandshakeInterceptorTests extends AbstractHttpRequestTes
@Test
public void copyAllAttributes() throws Exception {
public void defaultConstructor() throws Exception {
Map<String, Object> attributes = new HashMap<String, Object>();
WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
this.servletRequest.setSession(new MockHttpSession(null, "123"));
this.servletRequest.getSession().setAttribute("foo", "bar");
this.servletRequest.getSession().setAttribute("bar", "baz");
HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
interceptor.beforeHandshake(this.request, this.response, wsHandler, attributes);
assertEquals(2, attributes.size());
assertEquals(3, attributes.size());
assertEquals("bar", attributes.get("foo"));
assertEquals("baz", attributes.get("bar"));
assertEquals("123", attributes.get(HttpSessionHandshakeInterceptor.HTTP_SESSION_ID_ATTR_NAME));
}
@Test
public void copySelectedAttributes() throws Exception {
public void constructorWithAttributeNames() throws Exception {
Map<String, Object> attributes = new HashMap<String, Object>();
WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
this.servletRequest.setSession(new MockHttpSession(null, "123"));
this.servletRequest.getSession().setAttribute("foo", "bar");
this.servletRequest.getSession().setAttribute("bar", "baz");
@@ -67,29 +69,46 @@ public class HttpSessionHandshakeInterceptorTests extends AbstractHttpRequestTes
HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor(names);
interceptor.beforeHandshake(this.request, this.response, wsHandler, attributes);
assertEquals(2, attributes.size());
assertEquals("bar", attributes.get("foo"));
assertEquals("123", attributes.get(HttpSessionHandshakeInterceptor.HTTP_SESSION_ID_ATTR_NAME));
}
@Test
public void doNotCopyHttpSessionId() throws Exception {
Map<String, Object> attributes = new HashMap<String, Object>();
WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
this.servletRequest.setSession(new MockHttpSession(null, "123"));
this.servletRequest.getSession().setAttribute("foo", "bar");
HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
interceptor.setCopyHttpSessionId(false);
interceptor.beforeHandshake(this.request, this.response, wsHandler, attributes);
assertEquals(1, attributes.size());
assertEquals("bar", attributes.get("foo"));
}
@Test
public void copyHttpSessionId() throws Exception {
@Test
public void doNotCopyAttributes() throws Exception {
Map<String, Object> attributes = new HashMap<String, Object>();
WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
this.servletRequest.setSession(new MockHttpSession(null, "foo"));
this.servletRequest.setSession(new MockHttpSession(null, "123"));
this.servletRequest.getSession().setAttribute("foo", "bar");
HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
interceptor.setCopyHttpSessionId(true);
interceptor.setCopyAllAttributes(false);
interceptor.beforeHandshake(this.request, this.response, wsHandler, attributes);
assertEquals(1, attributes.size());
assertEquals("foo", attributes.get(HttpSessionHandshakeInterceptor.HTTP_SESSION_ID_ATTR_NAME));
assertEquals("123", attributes.get(HttpSessionHandshakeInterceptor.HTTP_SESSION_ID_ATTR_NAME));
}
@Test
public void doNotCauseSessionCreation() throws Exception {
Map<String, Object> attributes = new HashMap<String, Object>();
WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);