HeaderSessionStrategy uses response.setHeader
Previously multiple headers might be outputed. This ensures that only a single header is sent back with the session id. Fixes #32
This commit is contained in:
@@ -60,12 +60,12 @@ public class HeaderHttpSessionStrategy implements HttpSessionStrategy {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onNewSession(Session session, HttpServletRequest request, HttpServletResponse response) {
|
public void onNewSession(Session session, HttpServletRequest request, HttpServletResponse response) {
|
||||||
response.addHeader(headerName, session.getId());
|
response.setHeader(headerName, session.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onInvalidateSession(HttpServletRequest request, HttpServletResponse response) {
|
public void onInvalidateSession(HttpServletRequest request, HttpServletResponse response) {
|
||||||
response.addHeader(headerName, "");
|
response.setHeader(headerName, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -11,81 +11,102 @@ import org.springframework.session.web.http.HeaderHttpSessionStrategy;
|
|||||||
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
|
// the header is set as apposed to added
|
||||||
public void onNewSessionCustomHeaderName() throws Exception {
|
@Test
|
||||||
setHeaderName("CUSTOM");
|
public void onNewSessionMulti() throws Exception {
|
||||||
strategy.onNewSession(session, request, response);
|
strategy.onNewSession(session, request, response);
|
||||||
assertThat(getSessionId()).isEqualTo(session.getId());
|
strategy.onNewSession(session, request, response);
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
assertThat(response.getHeaders(headerName).size()).isEqualTo(1);
|
||||||
public void onDeleteSession() throws Exception {
|
assertThat(response.getHeaders(headerName)).containsOnly(session.getId());
|
||||||
strategy.onInvalidateSession(request, response);
|
}
|
||||||
assertThat(getSessionId()).isEmpty();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onDeleteSessionCustomHeaderName() throws Exception {
|
public void onNewSessionCustomHeaderName() throws Exception {
|
||||||
setHeaderName("CUSTOM");
|
setHeaderName("CUSTOM");
|
||||||
strategy.onInvalidateSession(request, response);
|
strategy.onNewSession(session, request, response);
|
||||||
assertThat(getSessionId()).isEmpty();
|
assertThat(getSessionId()).isEqualTo(session.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test
|
||||||
public void setHeaderNameNull() throws Exception {
|
public void onDeleteSession() throws Exception {
|
||||||
strategy.setHeaderName(null);
|
strategy.onInvalidateSession(request, response);
|
||||||
}
|
assertThat(getSessionId()).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
public void setHeaderName(String headerName) {
|
|
||||||
strategy.setHeaderName(headerName);
|
|
||||||
this.headerName = headerName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setSessionId(String id) {
|
// the header is set as apposed to added
|
||||||
request.addHeader(headerName, id);
|
@Test
|
||||||
}
|
public void onDeleteSessionMulti() throws Exception {
|
||||||
|
strategy.onInvalidateSession(request, response);
|
||||||
|
strategy.onInvalidateSession(request, response);
|
||||||
|
|
||||||
public String getSessionId() {
|
assertThat(response.getHeaders(headerName).size()).isEqualTo(1);
|
||||||
return response.getHeader(headerName);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHeaderName(String headerName) {
|
||||||
|
strategy.setHeaderName(headerName);
|
||||||
|
this.headerName = headerName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSessionId(String id) {
|
||||||
|
request.addHeader(headerName, id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSessionId() {
|
||||||
|
return response.getHeader(headerName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user