Add Servlet 3.1 methods to mock request

Issue: SPR-11492
This commit is contained in:
Rossen Stoyanchev
2014-02-28 12:28:17 -05:00
parent 3786993eb0
commit c553d681f1
4 changed files with 59 additions and 3 deletions

View File

@@ -348,6 +348,10 @@ public class MockHttpServletRequest implements HttpServletRequest {
return (this.content != null ? this.content.length : -1);
}
public long getContentLengthLong() {
return getContentLength();
}
public void setContentType(String contentType) {
this.contentType = contentType;
if (contentType != null) {
@@ -999,6 +1003,20 @@ public class MockHttpServletRequest implements HttpServletRequest {
return getSession(true);
}
/**
* The implementation of this (Servlet 3.1+) method calls
* {@link MockHttpSession#changeSessionId()} if the session is a mock session.
* Otherwise it simply returns the current session id.
* @since 4.0.3
*/
public String changeSessionId() {
Assert.isTrue(this.session != null, "The request does not have a session");
if (this.session instanceof MockHttpSession) {
return ((MockHttpSession) session).changeSessionId();
}
return this.session.getId();
}
public void setRequestedSessionIdValid(boolean requestedSessionIdValid) {
this.requestedSessionIdValid = requestedSessionIdValid;
}

View File

@@ -53,7 +53,7 @@ public class MockHttpSession implements HttpSession {
private static int nextId = 1;
private final String id;
private String id;
private final long creationTime = System.currentTimeMillis();
@@ -110,6 +110,16 @@ public class MockHttpSession implements HttpSession {
return this.id;
}
/**
* As of Servlet 3.1 the id of a session can be changed.
* @return the new session id.
* @since 4.0.3
*/
public String changeSessionId() {
this.id = Integer.toString(nextId++);
return this.id;
}
public void access() {
this.lastAccessedTime = System.currentTimeMillis();
this.isNew = false;