From c553d681f14bb19fac3bc25d8ecec83ae7347071 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Fri, 28 Feb 2014 12:28:17 -0500 Subject: [PATCH] Add Servlet 3.1 methods to mock request Issue: SPR-11492 --- .../mock/web/MockHttpServletRequest.java | 18 ++++++++++++++++++ .../mock/web/MockHttpSession.java | 12 +++++++++++- .../mock/web/test/MockHttpServletRequest.java | 18 ++++++++++++++++++ .../mock/web/test/MockHttpSession.java | 14 ++++++++++++-- 4 files changed, 59 insertions(+), 3 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java index 8086616953..8e1384c09e 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java @@ -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; } diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockHttpSession.java b/spring-test/src/main/java/org/springframework/mock/web/MockHttpSession.java index eeaa274c9a..cae49f69fb 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockHttpSession.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockHttpSession.java @@ -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; diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java index 9230f0fbbd..997af6e09b 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java +++ b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java @@ -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; } diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpSession.java b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpSession.java index d378ff051a..5883ed063c 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpSession.java +++ b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpSession.java @@ -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;