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