To tabs for all source
This commit is contained in:
@@ -30,98 +30,98 @@ import java.net.ServerSocket;
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration
|
||||
public class RedisOperationsSessionRepositoryITests {
|
||||
private RedisServer redisServer;
|
||||
private RedisServer redisServer;
|
||||
|
||||
@Autowired
|
||||
private SessionRepository repository;
|
||||
@Autowired
|
||||
private SessionRepository repository;
|
||||
|
||||
@Before
|
||||
public void setup() throws IOException {
|
||||
redisServer = new RedisServer(getPort());
|
||||
redisServer.start();
|
||||
}
|
||||
@Before
|
||||
public void setup() throws IOException {
|
||||
redisServer = new RedisServer(getPort());
|
||||
redisServer.start();
|
||||
}
|
||||
|
||||
@After
|
||||
public void shutdown() throws InterruptedException {
|
||||
redisServer.stop();
|
||||
}
|
||||
@After
|
||||
public void shutdown() throws InterruptedException {
|
||||
redisServer.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void saves() {
|
||||
Session toSave = repository.createSession();
|
||||
toSave.setAttribute("a", "b");
|
||||
Authentication toSaveToken = new UsernamePasswordAuthenticationToken("user","password", AuthorityUtils.createAuthorityList("ROLE_USER"));
|
||||
SecurityContext toSaveContext = SecurityContextHolder.createEmptyContext();
|
||||
toSaveContext.setAuthentication(toSaveToken);
|
||||
toSave.setAttribute("SPRING_SECURITY_CONTEXT", toSaveContext);
|
||||
@Test
|
||||
public void saves() {
|
||||
Session toSave = repository.createSession();
|
||||
toSave.setAttribute("a", "b");
|
||||
Authentication toSaveToken = new UsernamePasswordAuthenticationToken("user","password", AuthorityUtils.createAuthorityList("ROLE_USER"));
|
||||
SecurityContext toSaveContext = SecurityContextHolder.createEmptyContext();
|
||||
toSaveContext.setAuthentication(toSaveToken);
|
||||
toSave.setAttribute("SPRING_SECURITY_CONTEXT", toSaveContext);
|
||||
|
||||
repository.save(toSave);
|
||||
repository.save(toSave);
|
||||
|
||||
Session session = repository.getSession(toSave.getId());
|
||||
Session session = repository.getSession(toSave.getId());
|
||||
|
||||
assertThat(session.getId()).isEqualTo(toSave.getId());
|
||||
assertThat(session.getAttributeNames()).isEqualTo(session.getAttributeNames());
|
||||
assertThat(session.getAttribute("a")).isEqualTo(toSave.getAttribute("a"));
|
||||
assertThat(session.getId()).isEqualTo(toSave.getId());
|
||||
assertThat(session.getAttributeNames()).isEqualTo(session.getAttributeNames());
|
||||
assertThat(session.getAttribute("a")).isEqualTo(toSave.getAttribute("a"));
|
||||
|
||||
SecurityContext context = (SecurityContext) session.getAttribute("SPRING_SECURITY_CONTEXT");
|
||||
SecurityContext context = (SecurityContext) session.getAttribute("SPRING_SECURITY_CONTEXT");
|
||||
|
||||
repository.delete(toSave.getId());
|
||||
repository.delete(toSave.getId());
|
||||
|
||||
assertThat(repository.getSession(toSave.getId())).isNull();
|
||||
}
|
||||
assertThat(repository.getSession(toSave.getId())).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void putAllOnSingleAttrDoesNotRemoveOld() {
|
||||
Session toSave = repository.createSession();
|
||||
toSave.setAttribute("a", "b");
|
||||
@Test
|
||||
public void putAllOnSingleAttrDoesNotRemoveOld() {
|
||||
Session toSave = repository.createSession();
|
||||
toSave.setAttribute("a", "b");
|
||||
|
||||
repository.save(toSave);
|
||||
toSave = repository.getSession(toSave.getId());
|
||||
repository.save(toSave);
|
||||
toSave = repository.getSession(toSave.getId());
|
||||
|
||||
toSave.setAttribute("1", "2");
|
||||
toSave.setAttribute("1", "2");
|
||||
|
||||
repository.save(toSave);
|
||||
toSave = repository.getSession(toSave.getId());
|
||||
repository.save(toSave);
|
||||
toSave = repository.getSession(toSave.getId());
|
||||
|
||||
Session session = repository.getSession(toSave.getId());
|
||||
assertThat(session.getAttributeNames().size()).isEqualTo(2);
|
||||
assertThat(session.getAttribute("a")).isEqualTo("b");
|
||||
assertThat(session.getAttribute("1")).isEqualTo("2");
|
||||
}
|
||||
Session session = repository.getSession(toSave.getId());
|
||||
assertThat(session.getAttributeNames().size()).isEqualTo(2);
|
||||
assertThat(session.getAttribute("a")).isEqualTo("b");
|
||||
assertThat(session.getAttribute("1")).isEqualTo("2");
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class Config {
|
||||
@Bean
|
||||
public JedisConnectionFactory connectionFactory() throws Exception {
|
||||
JedisConnectionFactory factory = new JedisConnectionFactory();
|
||||
factory.setPort(getPort());
|
||||
factory.setUsePool(false);
|
||||
return factory;
|
||||
}
|
||||
@Configuration
|
||||
static class Config {
|
||||
@Bean
|
||||
public JedisConnectionFactory connectionFactory() throws Exception {
|
||||
JedisConnectionFactory factory = new JedisConnectionFactory();
|
||||
factory.setPort(getPort());
|
||||
factory.setUsePool(false);
|
||||
return factory;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RedisTemplate<String,Session> redisTemplate(RedisConnectionFactory connectionFactory) {
|
||||
RedisTemplate<String, Session> template = new RedisTemplate<String, Session>();
|
||||
template.setKeySerializer(new StringRedisSerializer());
|
||||
template.setHashKeySerializer(new StringRedisSerializer());
|
||||
template.setConnectionFactory(connectionFactory);
|
||||
return template;
|
||||
}
|
||||
@Bean
|
||||
public RedisTemplate<String,Session> redisTemplate(RedisConnectionFactory connectionFactory) {
|
||||
RedisTemplate<String, Session> template = new RedisTemplate<String, Session>();
|
||||
template.setKeySerializer(new StringRedisSerializer());
|
||||
template.setHashKeySerializer(new StringRedisSerializer());
|
||||
template.setConnectionFactory(connectionFactory);
|
||||
return template;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RedisOperationsSessionRepository sessionRepository(RedisTemplate<String, Session> redisTemplate) {
|
||||
return new RedisOperationsSessionRepository(redisTemplate);
|
||||
}
|
||||
}
|
||||
@Bean
|
||||
public RedisOperationsSessionRepository sessionRepository(RedisTemplate<String, Session> redisTemplate) {
|
||||
return new RedisOperationsSessionRepository(redisTemplate);
|
||||
}
|
||||
}
|
||||
|
||||
private static Integer availablePort;
|
||||
private static Integer availablePort;
|
||||
|
||||
private static int getPort() throws IOException {
|
||||
if(availablePort == null) {
|
||||
ServerSocket socket = new ServerSocket(0);
|
||||
availablePort = socket.getLocalPort();
|
||||
socket.close();
|
||||
}
|
||||
return availablePort;
|
||||
}
|
||||
private static int getPort() throws IOException {
|
||||
if(availablePort == null) {
|
||||
ServerSocket socket = new ServerSocket(0);
|
||||
availablePort = socket.getLocalPort();
|
||||
socket.close();
|
||||
}
|
||||
return availablePort;
|
||||
}
|
||||
}
|
||||
@@ -30,20 +30,20 @@ import java.util.concurrent.TimeUnit;
|
||||
* @author Rob Winch
|
||||
*/
|
||||
public class RedisOperationsSessionRepository implements SessionRepository<RedisOperationsSessionRepository.RedisSession> {
|
||||
private final String BOUNDED_HASH_KEY_PREFIX = "spring-security-sessions:";
|
||||
private final String CREATION_TIME_ATTR = "creationTime";
|
||||
private final String MAX_INACTIVE_ATTR = "maxInactiveInterval";
|
||||
private final String LAST_ACCESSED_ATTR = "lastAccessedTime";
|
||||
private final String SESSION_ATTR_PREFIX = "sessionAttr:";
|
||||
private final String BOUNDED_HASH_KEY_PREFIX = "spring-security-sessions:";
|
||||
private final String CREATION_TIME_ATTR = "creationTime";
|
||||
private final String MAX_INACTIVE_ATTR = "maxInactiveInterval";
|
||||
private final String LAST_ACCESSED_ATTR = "lastAccessedTime";
|
||||
private final String SESSION_ATTR_PREFIX = "sessionAttr:";
|
||||
|
||||
|
||||
private final RedisOperations<String,Session> redisTemplate;
|
||||
private final RedisOperations<String,Session> redisTemplate;
|
||||
|
||||
private Integer defaultMaxInactiveInterval;
|
||||
|
||||
public RedisOperationsSessionRepository(RedisOperations<String, Session> redisTemplate) {
|
||||
this.redisTemplate = redisTemplate;
|
||||
}
|
||||
public RedisOperationsSessionRepository(RedisOperations<String, Session> redisTemplate) {
|
||||
this.redisTemplate = redisTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the maximum inactive interval in seconds between requests before newly created sessions will be
|
||||
@@ -56,130 +56,130 @@ public class RedisOperationsSessionRepository implements SessionRepository<Redis
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(RedisSession session) {
|
||||
session.saveDelta();
|
||||
}
|
||||
public void save(RedisSession session) {
|
||||
session.saveDelta();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Session getSession(String id) {
|
||||
Map<Object, Object> entries = getOperations(id).entries();
|
||||
if(entries.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
MapSession loaded = new MapSession();
|
||||
loaded.setId(id);
|
||||
for(Map.Entry<Object,Object> entry : entries.entrySet()) {
|
||||
String key = (String) entry.getKey();
|
||||
if(CREATION_TIME_ATTR.equals(key)) {
|
||||
loaded.setCreationTime((Long) entry.getValue());
|
||||
} else if(MAX_INACTIVE_ATTR.equals(key)) {
|
||||
loaded.setMaxInactiveInterval((Integer) entry.getValue());
|
||||
} else if(LAST_ACCESSED_ATTR.equals(key)) {
|
||||
loaded.setLastAccessedTime((Long) entry.getValue());
|
||||
} else if(key.startsWith(SESSION_ATTR_PREFIX)) {
|
||||
loaded.setAttribute(key.substring(SESSION_ATTR_PREFIX.length()), entry.getValue());
|
||||
}
|
||||
}
|
||||
return new RedisSession(loaded);
|
||||
}
|
||||
@Override
|
||||
public Session getSession(String id) {
|
||||
Map<Object, Object> entries = getOperations(id).entries();
|
||||
if(entries.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
MapSession loaded = new MapSession();
|
||||
loaded.setId(id);
|
||||
for(Map.Entry<Object,Object> entry : entries.entrySet()) {
|
||||
String key = (String) entry.getKey();
|
||||
if(CREATION_TIME_ATTR.equals(key)) {
|
||||
loaded.setCreationTime((Long) entry.getValue());
|
||||
} else if(MAX_INACTIVE_ATTR.equals(key)) {
|
||||
loaded.setMaxInactiveInterval((Integer) entry.getValue());
|
||||
} else if(LAST_ACCESSED_ATTR.equals(key)) {
|
||||
loaded.setLastAccessedTime((Long) entry.getValue());
|
||||
} else if(key.startsWith(SESSION_ATTR_PREFIX)) {
|
||||
loaded.setAttribute(key.substring(SESSION_ATTR_PREFIX.length()), entry.getValue());
|
||||
}
|
||||
}
|
||||
return new RedisSession(loaded);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String sessionId) {
|
||||
String key = getKey(sessionId);
|
||||
this.redisTemplate.delete(key);
|
||||
}
|
||||
@Override
|
||||
public void delete(String sessionId) {
|
||||
String key = getKey(sessionId);
|
||||
this.redisTemplate.delete(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public RedisSession createSession() {
|
||||
@Override
|
||||
public RedisSession createSession() {
|
||||
RedisSession redisSession = new RedisSession();
|
||||
if(defaultMaxInactiveInterval != null) {
|
||||
redisSession.setMaxInactiveInterval(defaultMaxInactiveInterval);
|
||||
}
|
||||
return redisSession;
|
||||
}
|
||||
return redisSession;
|
||||
}
|
||||
|
||||
private String getKey(String sessionId) {
|
||||
return BOUNDED_HASH_KEY_PREFIX + sessionId;
|
||||
}
|
||||
private String getKey(String sessionId) {
|
||||
return BOUNDED_HASH_KEY_PREFIX + sessionId;
|
||||
}
|
||||
|
||||
private BoundHashOperations<String, Object, Object> getOperations(String sessionId) {
|
||||
String key = getKey(sessionId);
|
||||
return this.redisTemplate.boundHashOps(key);
|
||||
}
|
||||
private BoundHashOperations<String, Object, Object> getOperations(String sessionId) {
|
||||
String key = getKey(sessionId);
|
||||
return this.redisTemplate.boundHashOps(key);
|
||||
}
|
||||
|
||||
class RedisSession implements Session {
|
||||
private final MapSession cached;
|
||||
private Map<String, Object> delta = new HashMap<String,Object>();
|
||||
class RedisSession implements Session {
|
||||
private final MapSession cached;
|
||||
private Map<String, Object> delta = new HashMap<String,Object>();
|
||||
|
||||
private RedisSession() {
|
||||
this(new MapSession());
|
||||
delta.put(CREATION_TIME_ATTR, getCreationTime());
|
||||
delta.put(MAX_INACTIVE_ATTR, getMaxInactiveInterval());
|
||||
delta.put(LAST_ACCESSED_ATTR, getLastAccessedTime());
|
||||
}
|
||||
private RedisSession() {
|
||||
this(new MapSession());
|
||||
delta.put(CREATION_TIME_ATTR, getCreationTime());
|
||||
delta.put(MAX_INACTIVE_ATTR, getMaxInactiveInterval());
|
||||
delta.put(LAST_ACCESSED_ATTR, getLastAccessedTime());
|
||||
}
|
||||
|
||||
private RedisSession(MapSession cached) {
|
||||
this.cached = cached;
|
||||
}
|
||||
private RedisSession(MapSession cached) {
|
||||
this.cached = cached;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLastAccessedTime(long lastAccessedTime) {
|
||||
cached.setLastAccessedTime(lastAccessedTime);
|
||||
delta.put(LAST_ACCESSED_ATTR, getLastAccessedTime());
|
||||
}
|
||||
@Override
|
||||
public void setLastAccessedTime(long lastAccessedTime) {
|
||||
cached.setLastAccessedTime(lastAccessedTime);
|
||||
delta.put(LAST_ACCESSED_ATTR, getLastAccessedTime());
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getCreationTime() {
|
||||
return cached.getCreationTime();
|
||||
}
|
||||
@Override
|
||||
public long getCreationTime() {
|
||||
return cached.getCreationTime();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return cached.getId();
|
||||
}
|
||||
@Override
|
||||
public String getId() {
|
||||
return cached.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLastAccessedTime() {
|
||||
return cached.getLastAccessedTime();
|
||||
}
|
||||
@Override
|
||||
public long getLastAccessedTime() {
|
||||
return cached.getLastAccessedTime();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMaxInactiveInterval(int interval) {
|
||||
cached.setMaxInactiveInterval(interval);
|
||||
delta.put(MAX_INACTIVE_ATTR, getMaxInactiveInterval());
|
||||
}
|
||||
@Override
|
||||
public void setMaxInactiveInterval(int interval) {
|
||||
cached.setMaxInactiveInterval(interval);
|
||||
delta.put(MAX_INACTIVE_ATTR, getMaxInactiveInterval());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxInactiveInterval() {
|
||||
return cached.getMaxInactiveInterval();
|
||||
}
|
||||
@Override
|
||||
public int getMaxInactiveInterval() {
|
||||
return cached.getMaxInactiveInterval();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getAttribute(String attributeName) {
|
||||
return cached.getAttribute(attributeName);
|
||||
}
|
||||
@Override
|
||||
public Object getAttribute(String attributeName) {
|
||||
return cached.getAttribute(attributeName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getAttributeNames() {
|
||||
return cached.getAttributeNames();
|
||||
}
|
||||
@Override
|
||||
public Set<String> getAttributeNames() {
|
||||
return cached.getAttributeNames();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAttribute(String attributeName, Object attributeValue) {
|
||||
cached.setAttribute(attributeName, attributeValue);
|
||||
delta.put(SESSION_ATTR_PREFIX + attributeName, attributeValue);
|
||||
}
|
||||
@Override
|
||||
public void setAttribute(String attributeName, Object attributeValue) {
|
||||
cached.setAttribute(attributeName, attributeValue);
|
||||
delta.put(SESSION_ATTR_PREFIX + attributeName, attributeValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeAttribute(String attributeName) {
|
||||
cached.removeAttribute(attributeName);
|
||||
delta.put(SESSION_ATTR_PREFIX + attributeName, null);
|
||||
}
|
||||
@Override
|
||||
public void removeAttribute(String attributeName) {
|
||||
cached.removeAttribute(attributeName);
|
||||
delta.put(SESSION_ATTR_PREFIX + attributeName, null);
|
||||
}
|
||||
|
||||
private void saveDelta() {
|
||||
getOperations(getId()).putAll(delta);
|
||||
getOperations(getId()).expire(getMaxInactiveInterval(), TimeUnit.SECONDS);
|
||||
delta.clear();
|
||||
}
|
||||
}
|
||||
private void saveDelta() {
|
||||
getOperations(getId()).putAll(delta);
|
||||
getOperations(getId()).expire(getMaxInactiveInterval(), TimeUnit.SECONDS);
|
||||
delta.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,98 +9,98 @@ import static org.fest.assertions.Assertions.assertThat;
|
||||
|
||||
public class MapSessionTests {
|
||||
|
||||
private MapSession session;
|
||||
private MapSession session;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
session = new MapSession();
|
||||
}
|
||||
@Before
|
||||
public void setup() {
|
||||
session = new MapSession();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void constructorNullSession() {
|
||||
new MapSession(null);
|
||||
}
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void constructorNullSession() {
|
||||
new MapSession(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure conforms to the javadoc of {@link Session}
|
||||
*/
|
||||
@Test
|
||||
public void setAttributeNullObjectRemoves() {
|
||||
String attr = "attr";
|
||||
session.setAttribute(attr, new Object());
|
||||
session.setAttribute(attr, null);
|
||||
assertThat(session.getAttributeNames()).isEmpty();
|
||||
}
|
||||
/**
|
||||
* Ensure conforms to the javadoc of {@link Session}
|
||||
*/
|
||||
@Test
|
||||
public void setAttributeNullObjectRemoves() {
|
||||
String attr = "attr";
|
||||
session.setAttribute(attr, new Object());
|
||||
session.setAttribute(attr, null);
|
||||
assertThat(session.getAttributeNames()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsNonSessionFalse() {
|
||||
assertThat(session.equals(new Object())).isFalse();
|
||||
}
|
||||
@Test
|
||||
public void equalsNonSessionFalse() {
|
||||
assertThat(session.equals(new Object())).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsCustomSession() {
|
||||
CustomSession other = new CustomSession();
|
||||
session.setId(other.getId());
|
||||
assertThat(session.equals(other)).isTrue();
|
||||
}
|
||||
@Test
|
||||
public void equalsCustomSession() {
|
||||
CustomSession other = new CustomSession();
|
||||
session.setId(other.getId());
|
||||
assertThat(session.equals(other)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hashCodeEqualsIdHashCode() {
|
||||
session.setId("constantId");
|
||||
assertThat(session.hashCode()).isEqualTo(session.getId().hashCode());
|
||||
}
|
||||
@Test
|
||||
public void hashCodeEqualsIdHashCode() {
|
||||
session.setId("constantId");
|
||||
assertThat(session.hashCode()).isEqualTo(session.getId().hashCode());
|
||||
}
|
||||
|
||||
static class CustomSession implements Session {
|
||||
static class CustomSession implements Session {
|
||||
|
||||
@Override
|
||||
public void setLastAccessedTime(long lastAccessedTime) {
|
||||
@Override
|
||||
public void setLastAccessedTime(long lastAccessedTime) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getCreationTime() {
|
||||
return 0;
|
||||
}
|
||||
@Override
|
||||
public long getCreationTime() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return "id";
|
||||
}
|
||||
@Override
|
||||
public String getId() {
|
||||
return "id";
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLastAccessedTime() {
|
||||
return 0;
|
||||
}
|
||||
@Override
|
||||
public long getLastAccessedTime() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMaxInactiveInterval(int interval) {
|
||||
@Override
|
||||
public void setMaxInactiveInterval(int interval) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxInactiveInterval() {
|
||||
return 0;
|
||||
}
|
||||
@Override
|
||||
public int getMaxInactiveInterval() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getAttribute(String attributeName) {
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public Object getAttribute(String attributeName) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getAttributeNames() {
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public Set<String> getAttributeNames() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAttribute(String attributeName, Object attributeValue) {
|
||||
@Override
|
||||
public void setAttribute(String attributeName, Object attributeValue) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeAttribute(String attributeName) {
|
||||
@Override
|
||||
public void removeAttribute(String attributeName) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,99 +12,99 @@ import org.springframework.session.Session;
|
||||
import javax.servlet.http.Cookie;
|
||||
|
||||
public class CookieHttpSessionStrategyTests {
|
||||
private MockHttpServletRequest request;
|
||||
private MockHttpServletResponse response;
|
||||
private MockHttpServletRequest request;
|
||||
private MockHttpServletResponse response;
|
||||
|
||||
private CookieHttpSessionStrategy strategy;
|
||||
private String cookieName;
|
||||
private Session session;
|
||||
private CookieHttpSessionStrategy strategy;
|
||||
private String cookieName;
|
||||
private Session session;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
cookieName = "SESSION";
|
||||
session = new MapSession();
|
||||
request = new MockHttpServletRequest();
|
||||
response = new MockHttpServletResponse();
|
||||
strategy = new CookieHttpSessionStrategy();
|
||||
}
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
cookieName = "SESSION";
|
||||
session = new MapSession();
|
||||
request = new MockHttpServletRequest();
|
||||
response = new MockHttpServletResponse();
|
||||
strategy = new CookieHttpSessionStrategy();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRequestedSessionIdNull() throws Exception {
|
||||
assertThat(strategy.getRequestedSessionId(request)).isNull();
|
||||
}
|
||||
@Test
|
||||
public void getRequestedSessionIdNull() throws Exception {
|
||||
assertThat(strategy.getRequestedSessionId(request)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRequestedSessionIdNotNull() throws Exception {
|
||||
setSessionId(session.getId());
|
||||
assertThat(strategy.getRequestedSessionId(request)).isEqualTo(session.getId());
|
||||
}
|
||||
@Test
|
||||
public void getRequestedSessionIdNotNull() throws Exception {
|
||||
setSessionId(session.getId());
|
||||
assertThat(strategy.getRequestedSessionId(request)).isEqualTo(session.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRequestedSessionIdNotNullCustomCookieName() throws Exception {
|
||||
setCookieName("CUSTOM");
|
||||
setSessionId(session.getId());
|
||||
assertThat(strategy.getRequestedSessionId(request)).isEqualTo(session.getId());
|
||||
}
|
||||
@Test
|
||||
public void getRequestedSessionIdNotNullCustomCookieName() throws Exception {
|
||||
setCookieName("CUSTOM");
|
||||
setSessionId(session.getId());
|
||||
assertThat(strategy.getRequestedSessionId(request)).isEqualTo(session.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onNewSession() throws Exception {
|
||||
strategy.onNewSession(session, request, response);
|
||||
assertThat(getSessionId()).isEqualTo(session.getId());
|
||||
}
|
||||
@Test
|
||||
public void onNewSession() throws Exception {
|
||||
strategy.onNewSession(session, request, response);
|
||||
assertThat(getSessionId()).isEqualTo(session.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onNewSessionCookiePath() throws Exception {
|
||||
request.setContextPath("/somethingunique");
|
||||
strategy.onNewSession(session, request, response);
|
||||
@Test
|
||||
public void onNewSessionCookiePath() throws Exception {
|
||||
request.setContextPath("/somethingunique");
|
||||
strategy.onNewSession(session, request, response);
|
||||
|
||||
Cookie sessionCookie = response.getCookie(cookieName);
|
||||
assertThat(sessionCookie.getPath()).isEqualTo(request.getContextPath() + "/");
|
||||
}
|
||||
Cookie sessionCookie = response.getCookie(cookieName);
|
||||
assertThat(sessionCookie.getPath()).isEqualTo(request.getContextPath() + "/");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onNewSessionCustomCookieName() throws Exception {
|
||||
setCookieName("CUSTOM");
|
||||
strategy.onNewSession(session, request, response);
|
||||
assertThat(getSessionId()).isEqualTo(session.getId());
|
||||
}
|
||||
@Test
|
||||
public void onNewSessionCustomCookieName() throws Exception {
|
||||
setCookieName("CUSTOM");
|
||||
strategy.onNewSession(session, request, response);
|
||||
assertThat(getSessionId()).isEqualTo(session.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onDeleteSession() throws Exception {
|
||||
strategy.onInvalidateSession(request, response);
|
||||
assertThat(getSessionId()).isEmpty();
|
||||
}
|
||||
@Test
|
||||
public void onDeleteSession() throws Exception {
|
||||
strategy.onInvalidateSession(request, response);
|
||||
assertThat(getSessionId()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onDeleteSessionCookiePath() throws Exception {
|
||||
request.setContextPath("/somethingunique");
|
||||
strategy.onInvalidateSession(request, response);
|
||||
@Test
|
||||
public void onDeleteSessionCookiePath() throws Exception {
|
||||
request.setContextPath("/somethingunique");
|
||||
strategy.onInvalidateSession(request, response);
|
||||
|
||||
Cookie sessionCookie = response.getCookie(cookieName);
|
||||
assertThat(sessionCookie.getPath()).isEqualTo(request.getContextPath() + "/");
|
||||
}
|
||||
Cookie sessionCookie = response.getCookie(cookieName);
|
||||
assertThat(sessionCookie.getPath()).isEqualTo(request.getContextPath() + "/");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onDeleteSessionCustomCookieName() throws Exception {
|
||||
setCookieName("CUSTOM");
|
||||
strategy.onInvalidateSession(request, response);
|
||||
assertThat(getSessionId()).isEmpty();
|
||||
}
|
||||
@Test
|
||||
public void onDeleteSessionCustomCookieName() throws Exception {
|
||||
setCookieName("CUSTOM");
|
||||
strategy.onInvalidateSession(request, response);
|
||||
assertThat(getSessionId()).isEmpty();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void setCookieNameNull() throws Exception {
|
||||
strategy.setCookieName(null);
|
||||
}
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void setCookieNameNull() throws Exception {
|
||||
strategy.setCookieName(null);
|
||||
}
|
||||
|
||||
public void setCookieName(String cookieName) {
|
||||
strategy.setCookieName(cookieName);
|
||||
this.cookieName = cookieName;
|
||||
}
|
||||
public void setCookieName(String cookieName) {
|
||||
strategy.setCookieName(cookieName);
|
||||
this.cookieName = cookieName;
|
||||
}
|
||||
|
||||
public void setSessionId(String id) {
|
||||
request.setCookies(new Cookie(cookieName, id));
|
||||
}
|
||||
public void setSessionId(String id) {
|
||||
request.setCookies(new Cookie(cookieName, id));
|
||||
}
|
||||
|
||||
public String getSessionId() {
|
||||
return response.getCookie(cookieName).getValue();
|
||||
}
|
||||
public String getSessionId() {
|
||||
return response.getCookie(cookieName).getValue();
|
||||
}
|
||||
}
|
||||
@@ -10,81 +10,81 @@ import org.springframework.session.Session;
|
||||
import static org.fest.assertions.Assertions.assertThat;
|
||||
|
||||
public class HeaderSessionStrategyTests {
|
||||
private MockHttpServletRequest request;
|
||||
private MockHttpServletResponse response;
|
||||
private MockHttpServletRequest request;
|
||||
private MockHttpServletResponse response;
|
||||
|
||||
private HeaderHttpSessionStrategy strategy;
|
||||
private String headerName;
|
||||
private Session session;
|
||||
private HeaderHttpSessionStrategy strategy;
|
||||
private String headerName;
|
||||
private Session session;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
headerName = "x-auth-token";
|
||||
session = new MapSession();
|
||||
request = new MockHttpServletRequest();
|
||||
response = new MockHttpServletResponse();
|
||||
strategy = new HeaderHttpSessionStrategy();
|
||||
}
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
headerName = "x-auth-token";
|
||||
session = new MapSession();
|
||||
request = new MockHttpServletRequest();
|
||||
response = new MockHttpServletResponse();
|
||||
strategy = new HeaderHttpSessionStrategy();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRequestedSessionIdNull() throws Exception {
|
||||
assertThat(strategy.getRequestedSessionId(request)).isNull();
|
||||
}
|
||||
@Test
|
||||
public void getRequestedSessionIdNull() throws Exception {
|
||||
assertThat(strategy.getRequestedSessionId(request)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRequestedSessionIdNotNull() throws Exception {
|
||||
setSessionId(session.getId());
|
||||
assertThat(strategy.getRequestedSessionId(request)).isEqualTo(session.getId());
|
||||
}
|
||||
@Test
|
||||
public void getRequestedSessionIdNotNull() throws Exception {
|
||||
setSessionId(session.getId());
|
||||
assertThat(strategy.getRequestedSessionId(request)).isEqualTo(session.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRequestedSessionIdNotNullCustomHeaderName() throws Exception {
|
||||
setHeaderName("CUSTOM");
|
||||
setSessionId(session.getId());
|
||||
assertThat(strategy.getRequestedSessionId(request)).isEqualTo(session.getId());
|
||||
}
|
||||
@Test
|
||||
public void getRequestedSessionIdNotNullCustomHeaderName() throws Exception {
|
||||
setHeaderName("CUSTOM");
|
||||
setSessionId(session.getId());
|
||||
assertThat(strategy.getRequestedSessionId(request)).isEqualTo(session.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onNewSession() throws Exception {
|
||||
strategy.onNewSession(session, request, response);
|
||||
assertThat(getSessionId()).isEqualTo(session.getId());
|
||||
}
|
||||
@Test
|
||||
public void onNewSession() throws Exception {
|
||||
strategy.onNewSession(session, request, response);
|
||||
assertThat(getSessionId()).isEqualTo(session.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onNewSessionCustomHeaderName() throws Exception {
|
||||
setHeaderName("CUSTOM");
|
||||
strategy.onNewSession(session, request, response);
|
||||
assertThat(getSessionId()).isEqualTo(session.getId());
|
||||
}
|
||||
@Test
|
||||
public void onNewSessionCustomHeaderName() throws Exception {
|
||||
setHeaderName("CUSTOM");
|
||||
strategy.onNewSession(session, request, response);
|
||||
assertThat(getSessionId()).isEqualTo(session.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onDeleteSession() throws Exception {
|
||||
strategy.onInvalidateSession(request, response);
|
||||
assertThat(getSessionId()).isEmpty();
|
||||
}
|
||||
@Test
|
||||
public void onDeleteSession() throws Exception {
|
||||
strategy.onInvalidateSession(request, response);
|
||||
assertThat(getSessionId()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onDeleteSessionCustomHeaderName() throws Exception {
|
||||
setHeaderName("CUSTOM");
|
||||
strategy.onInvalidateSession(request, response);
|
||||
assertThat(getSessionId()).isEmpty();
|
||||
}
|
||||
@Test
|
||||
public void onDeleteSessionCustomHeaderName() throws Exception {
|
||||
setHeaderName("CUSTOM");
|
||||
strategy.onInvalidateSession(request, response);
|
||||
assertThat(getSessionId()).isEmpty();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void setHeaderNameNull() throws Exception {
|
||||
strategy.setHeaderName(null);
|
||||
}
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void setHeaderNameNull() throws Exception {
|
||||
strategy.setHeaderName(null);
|
||||
}
|
||||
|
||||
public void setHeaderName(String headerName) {
|
||||
strategy.setHeaderName(headerName);
|
||||
this.headerName = headerName;
|
||||
}
|
||||
public void setHeaderName(String headerName) {
|
||||
strategy.setHeaderName(headerName);
|
||||
this.headerName = headerName;
|
||||
}
|
||||
|
||||
public void setSessionId(String id) {
|
||||
request.addHeader(headerName, id);
|
||||
}
|
||||
public void setSessionId(String id) {
|
||||
request.addHeader(headerName, id);
|
||||
}
|
||||
|
||||
public String getSessionId() {
|
||||
return response.getHeader(headerName);
|
||||
}
|
||||
public String getSessionId() {
|
||||
return response.getHeader(headerName);
|
||||
}
|
||||
}
|
||||
@@ -18,56 +18,56 @@ import java.util.List;
|
||||
import static org.fest.assertions.Assertions.*;
|
||||
|
||||
public class OncePerRequestFilterTests {
|
||||
private MockHttpServletRequest request;
|
||||
private MockHttpServletResponse response;
|
||||
private MockFilterChain chain;
|
||||
private OncePerRequestFilter filter;
|
||||
private HttpServlet servlet;
|
||||
private MockHttpServletRequest request;
|
||||
private MockHttpServletResponse response;
|
||||
private MockFilterChain chain;
|
||||
private OncePerRequestFilter filter;
|
||||
private HttpServlet servlet;
|
||||
|
||||
|
||||
private List<OncePerRequestFilter> invocations;
|
||||
private List<OncePerRequestFilter> invocations;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
servlet = new HttpServlet() {};
|
||||
request = new MockHttpServletRequest();
|
||||
response = new MockHttpServletResponse();
|
||||
chain = new MockFilterChain();
|
||||
invocations = new ArrayList<OncePerRequestFilter>();
|
||||
filter = new OncePerRequestFilter() {
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
|
||||
invocations.add(this);
|
||||
filterChain.doFilter(request, response);
|
||||
}
|
||||
};
|
||||
}
|
||||
@Before
|
||||
public void setup() {
|
||||
servlet = new HttpServlet() {};
|
||||
request = new MockHttpServletRequest();
|
||||
response = new MockHttpServletResponse();
|
||||
chain = new MockFilterChain();
|
||||
invocations = new ArrayList<OncePerRequestFilter>();
|
||||
filter = new OncePerRequestFilter() {
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
|
||||
invocations.add(this);
|
||||
filterChain.doFilter(request, response);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doFilterOnce() throws ServletException, IOException {
|
||||
filter.doFilter(request, response, chain);
|
||||
@Test
|
||||
public void doFilterOnce() throws ServletException, IOException {
|
||||
filter.doFilter(request, response, chain);
|
||||
|
||||
assertThat(invocations).containsOnly(filter);
|
||||
}
|
||||
assertThat(invocations).containsOnly(filter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doFilterMultiOnlyIvokesOnce() throws ServletException, IOException {
|
||||
filter.doFilter(request, response, new MockFilterChain(servlet, filter));
|
||||
@Test
|
||||
public void doFilterMultiOnlyIvokesOnce() throws ServletException, IOException {
|
||||
filter.doFilter(request, response, new MockFilterChain(servlet, filter));
|
||||
|
||||
assertThat(invocations).containsOnly(filter);
|
||||
}
|
||||
assertThat(invocations).containsOnly(filter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doFilterOtherSubclassInvoked() throws ServletException, IOException {
|
||||
OncePerRequestFilter filter2 = new OncePerRequestFilter() {
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
|
||||
invocations.add(this);
|
||||
filterChain.doFilter(request, response);
|
||||
}
|
||||
};
|
||||
filter.doFilter(request, response, new MockFilterChain(servlet, filter2));
|
||||
@Test
|
||||
public void doFilterOtherSubclassInvoked() throws ServletException, IOException {
|
||||
OncePerRequestFilter filter2 = new OncePerRequestFilter() {
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
|
||||
invocations.add(this);
|
||||
filterChain.doFilter(request, response);
|
||||
}
|
||||
};
|
||||
filter.doFilter(request, response, new MockFilterChain(servlet, filter2));
|
||||
|
||||
assertThat(invocations).containsOnly(filter, filter2);
|
||||
}
|
||||
assertThat(invocations).containsOnly(filter, filter2);
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user