From a4e003ebf1bc40a4376174b273c5b9cfc8eb60f5 Mon Sep 17 00:00:00 2001 From: Rob Winch Date: Fri, 1 Aug 2014 15:16:02 -0500 Subject: [PATCH] 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 --- .../web/http/HeaderHttpSessionStrategy.java | 4 +- .../web/http/HeaderSessionStrategyTests.java | 149 ++++++++++-------- 2 files changed, 87 insertions(+), 66 deletions(-) diff --git a/spring-session/src/main/java/org/springframework/session/web/http/HeaderHttpSessionStrategy.java b/spring-session/src/main/java/org/springframework/session/web/http/HeaderHttpSessionStrategy.java index 92d64466..cd80c61c 100644 --- a/spring-session/src/main/java/org/springframework/session/web/http/HeaderHttpSessionStrategy.java +++ b/spring-session/src/main/java/org/springframework/session/web/http/HeaderHttpSessionStrategy.java @@ -60,12 +60,12 @@ public class HeaderHttpSessionStrategy implements HttpSessionStrategy { @Override public void onNewSession(Session session, HttpServletRequest request, HttpServletResponse response) { - response.addHeader(headerName, session.getId()); + response.setHeader(headerName, session.getId()); } @Override public void onInvalidateSession(HttpServletRequest request, HttpServletResponse response) { - response.addHeader(headerName, ""); + response.setHeader(headerName, ""); } /** diff --git a/spring-session/src/test/java/org/springframework/session/web/http/HeaderSessionStrategyTests.java b/spring-session/src/test/java/org/springframework/session/web/http/HeaderSessionStrategyTests.java index 6ac56790..455f5317 100644 --- a/spring-session/src/test/java/org/springframework/session/web/http/HeaderSessionStrategyTests.java +++ b/spring-session/src/test/java/org/springframework/session/web/http/HeaderSessionStrategyTests.java @@ -11,81 +11,102 @@ import org.springframework.session.web.http.HeaderHttpSessionStrategy; 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()); - } + // the header is set as apposed to added + @Test + public void onNewSessionMulti() throws Exception { + strategy.onNewSession(session, request, response); + strategy.onNewSession(session, request, response); - @Test - public void onDeleteSession() throws Exception { - strategy.onInvalidateSession(request, response); - assertThat(getSessionId()).isEmpty(); - } + assertThat(response.getHeaders(headerName).size()).isEqualTo(1); + assertThat(response.getHeaders(headerName)).containsOnly(session.getId()); + } - @Test - public void onDeleteSessionCustomHeaderName() throws Exception { - setHeaderName("CUSTOM"); - strategy.onInvalidateSession(request, response); - assertThat(getSessionId()).isEmpty(); - } + @Test + public void onNewSessionCustomHeaderName() throws Exception { + setHeaderName("CUSTOM"); + strategy.onNewSession(session, request, response); + assertThat(getSessionId()).isEqualTo(session.getId()); + } - @Test(expected = IllegalArgumentException.class) - public void setHeaderNameNull() throws Exception { - strategy.setHeaderName(null); - } + @Test + public void onDeleteSession() throws Exception { + strategy.onInvalidateSession(request, response); + assertThat(getSessionId()).isEmpty(); + } - public void setHeaderName(String headerName) { - strategy.setHeaderName(headerName); - this.headerName = headerName; - } - public void setSessionId(String id) { - request.addHeader(headerName, id); - } + // the header is set as apposed to added + @Test + public void onDeleteSessionMulti() throws Exception { + strategy.onInvalidateSession(request, response); + strategy.onInvalidateSession(request, response); - public String getSessionId() { - return response.getHeader(headerName); - } + assertThat(response.getHeaders(headerName).size()).isEqualTo(1); + 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); + } } \ No newline at end of file