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

@@ -351,6 +351,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) {
@@ -1001,6 +1005,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

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -54,7 +54,7 @@ public class MockHttpSession implements HttpSession {
private static int nextId = 1;
private final String id;
private String id;
private final long creationTime = System.currentTimeMillis();
@@ -111,6 +111,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;