|
|
|
|
@@ -1,5 +1,5 @@
|
|
|
|
|
/*
|
|
|
|
|
* Copyright 2002-2012 the original author or authors.
|
|
|
|
|
* Copyright 2002-2013 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.
|
|
|
|
|
@@ -69,16 +69,17 @@ import org.springframework.web.util.UriUtils;
|
|
|
|
|
*
|
|
|
|
|
* @author Rossen Stoyanchev
|
|
|
|
|
* @author Arjen Poutsma
|
|
|
|
|
* @since 3.2
|
|
|
|
|
*/
|
|
|
|
|
* @since 3.2
|
|
|
|
|
*/
|
|
|
|
|
public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable {
|
|
|
|
|
|
|
|
|
|
static final boolean servlet3Present = ClassUtils.hasMethod(ServletRequest.class, "startAsync");
|
|
|
|
|
|
|
|
|
|
private final UriComponents uriComponents;
|
|
|
|
|
|
|
|
|
|
private final HttpMethod method;
|
|
|
|
|
|
|
|
|
|
private final UriComponents uriComponents;
|
|
|
|
|
|
|
|
|
|
private final MultiValueMap<String, Object> headers = new LinkedMultiValueMap<String, Object>();
|
|
|
|
|
|
|
|
|
|
private String contentType;
|
|
|
|
|
@@ -93,10 +94,10 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
|
|
|
|
|
private String characterEncoding;
|
|
|
|
|
|
|
|
|
|
private Principal principal;
|
|
|
|
|
|
|
|
|
|
private Boolean secure;
|
|
|
|
|
|
|
|
|
|
private Principal principal;
|
|
|
|
|
|
|
|
|
|
private final Map<String, Object> attributes = new LinkedHashMap<String, Object>();
|
|
|
|
|
|
|
|
|
|
private MockHttpSession session;
|
|
|
|
|
@@ -111,34 +112,28 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
|
|
|
|
|
private String pathInfo = ValueConstants.DEFAULT_NONE;
|
|
|
|
|
|
|
|
|
|
private final List<RequestPostProcessor> postProcessors =
|
|
|
|
|
new ArrayList<RequestPostProcessor>();
|
|
|
|
|
private final List<RequestPostProcessor> postProcessors = new ArrayList<RequestPostProcessor>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Package private constructor. To get an instance, use static factory
|
|
|
|
|
* methods in {@link MockMvcRequestBuilders}.
|
|
|
|
|
*
|
|
|
|
|
* <p>Although this class cannot be extended, additional ways to initialize
|
|
|
|
|
* the {@code MockHttpServletRequest} can be plugged in via
|
|
|
|
|
* {@link #with(RequestPostProcessor)}.
|
|
|
|
|
*
|
|
|
|
|
* @param urlTemplate a URL template; the resulting URL will be encoded
|
|
|
|
|
* @param urlVariables zero or more URL variables
|
|
|
|
|
*/
|
|
|
|
|
MockHttpServletRequestBuilder(HttpMethod httpMethod, String urlTemplate, Object... urlVariables) {
|
|
|
|
|
|
|
|
|
|
Assert.notNull(urlTemplate, "uriTemplate is required");
|
|
|
|
|
Assert.notNull(httpMethod, "httpMethod is required");
|
|
|
|
|
|
|
|
|
|
this.uriComponents = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(urlVariables).encode();
|
|
|
|
|
Assert.notNull(urlTemplate, "uriTemplate is required");
|
|
|
|
|
this.method = httpMethod;
|
|
|
|
|
this.uriComponents = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(urlVariables).encode();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add a request parameter to the {@link MockHttpServletRequest}.
|
|
|
|
|
* If called more than once, the new values are added.
|
|
|
|
|
*
|
|
|
|
|
* @param name the parameter name
|
|
|
|
|
* @param values one or more values
|
|
|
|
|
*/
|
|
|
|
|
@@ -149,7 +144,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add a header to the request. Values are always added.
|
|
|
|
|
*
|
|
|
|
|
* @param name the header name
|
|
|
|
|
* @param values one or more header values
|
|
|
|
|
*/
|
|
|
|
|
@@ -160,7 +154,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add all headers to the request. Values are always added.
|
|
|
|
|
*
|
|
|
|
|
* @param httpHeaders the headers and values to add
|
|
|
|
|
*/
|
|
|
|
|
public MockHttpServletRequestBuilder headers(HttpHeaders httpHeaders) {
|
|
|
|
|
@@ -173,7 +166,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the 'Content-Type' header of the request.
|
|
|
|
|
*
|
|
|
|
|
* @param mediaType the content type
|
|
|
|
|
*/
|
|
|
|
|
public MockHttpServletRequestBuilder contentType(MediaType mediaType) {
|
|
|
|
|
@@ -185,7 +177,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the 'Accept' header to the given media type(s).
|
|
|
|
|
*
|
|
|
|
|
* @param mediaTypes one or more media types
|
|
|
|
|
*/
|
|
|
|
|
public MockHttpServletRequestBuilder accept(MediaType... mediaTypes) {
|
|
|
|
|
@@ -196,7 +187,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the request body.
|
|
|
|
|
*
|
|
|
|
|
* @param content the body content
|
|
|
|
|
*/
|
|
|
|
|
public MockHttpServletRequestBuilder content(byte[] content) {
|
|
|
|
|
@@ -206,7 +196,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the request body as a UTF-8 String.
|
|
|
|
|
*
|
|
|
|
|
* @param content the body content
|
|
|
|
|
*/
|
|
|
|
|
public MockHttpServletRequestBuilder content(String content) {
|
|
|
|
|
@@ -221,7 +210,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add the given cookies to the request. Cookies are always added.
|
|
|
|
|
*
|
|
|
|
|
* @param cookies the cookies to add
|
|
|
|
|
*/
|
|
|
|
|
public MockHttpServletRequestBuilder cookie(Cookie... cookies) {
|
|
|
|
|
@@ -233,7 +221,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the locale of the request.
|
|
|
|
|
*
|
|
|
|
|
* @param locale the locale
|
|
|
|
|
*/
|
|
|
|
|
public MockHttpServletRequestBuilder locale(Locale locale) {
|
|
|
|
|
@@ -243,7 +230,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the character encoding of the request.
|
|
|
|
|
*
|
|
|
|
|
* @param encoding the character encoding
|
|
|
|
|
*/
|
|
|
|
|
public MockHttpServletRequestBuilder characterEncoding(String encoding) {
|
|
|
|
|
@@ -253,7 +239,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set a request attribute.
|
|
|
|
|
*
|
|
|
|
|
* @param name the attribute name
|
|
|
|
|
* @param value the attribute value
|
|
|
|
|
*/
|
|
|
|
|
@@ -264,7 +249,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set a session attribute.
|
|
|
|
|
*
|
|
|
|
|
* @param name the session attribute name
|
|
|
|
|
* @param value the session attribute value
|
|
|
|
|
*/
|
|
|
|
|
@@ -275,7 +259,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set session attributes.
|
|
|
|
|
*
|
|
|
|
|
* @param sessionAttributes the session attributes
|
|
|
|
|
*/
|
|
|
|
|
public MockHttpServletRequestBuilder sessionAttrs(Map<String, Object> sessionAttributes) {
|
|
|
|
|
@@ -288,7 +271,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set an "input" flash attribute.
|
|
|
|
|
*
|
|
|
|
|
* @param name the flash attribute name
|
|
|
|
|
* @param value the flash attribute value
|
|
|
|
|
*/
|
|
|
|
|
@@ -299,7 +281,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set flash attributes.
|
|
|
|
|
*
|
|
|
|
|
* @param flashAttributes the flash attributes
|
|
|
|
|
*/
|
|
|
|
|
public MockHttpServletRequestBuilder flashAttrs(Map<String, Object> flashAttributes) {
|
|
|
|
|
@@ -312,10 +293,8 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the HTTP session to use, possibly re-used across requests.
|
|
|
|
|
*
|
|
|
|
|
* <p>Individual attributes provided via {@link #sessionAttr(String, Object)}
|
|
|
|
|
* override the content of the session provided here.
|
|
|
|
|
*
|
|
|
|
|
* @param session the HTTP session
|
|
|
|
|
*/
|
|
|
|
|
public MockHttpServletRequestBuilder session(MockHttpSession session) {
|
|
|
|
|
@@ -326,7 +305,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the principal of the request.
|
|
|
|
|
*
|
|
|
|
|
* @param principal the principal
|
|
|
|
|
*/
|
|
|
|
|
public MockHttpServletRequestBuilder principal(Principal principal) {
|
|
|
|
|
@@ -337,16 +315,12 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Specify the portion of the requestURI that represents the context path.
|
|
|
|
|
* The context path, if specified, must match to the start of the request
|
|
|
|
|
* URI.
|
|
|
|
|
*
|
|
|
|
|
* The context path, if specified, must match to the start of the request URI.
|
|
|
|
|
* <p>In most cases, tests can be written by omitting the context path from
|
|
|
|
|
* the requestURI. This is because most applications don't actually depend
|
|
|
|
|
* on the name under which they're deployed. If specified here, the context
|
|
|
|
|
* path must start with a "/" and must not end with a "/".
|
|
|
|
|
*
|
|
|
|
|
* @see <a
|
|
|
|
|
* href="http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getContextPath%28%29">HttpServletRequest.getContextPath()</a>
|
|
|
|
|
* @see <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getContextPath%28%29">HttpServletRequest.getContextPath()</a>
|
|
|
|
|
*/
|
|
|
|
|
public MockHttpServletRequestBuilder contextPath(String contextPath) {
|
|
|
|
|
if (StringUtils.hasText(contextPath)) {
|
|
|
|
|
@@ -361,7 +335,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
* Specify the portion of the requestURI that represents the path to which
|
|
|
|
|
* the Servlet is mapped. This is typically a portion of the requestURI
|
|
|
|
|
* after the context path.
|
|
|
|
|
*
|
|
|
|
|
* <p>In most cases, tests can be written by omitting the servlet path from
|
|
|
|
|
* the requestURI. This is because most applications don't actually depend
|
|
|
|
|
* on the prefix to which a servlet is mapped. For example if a Servlet is
|
|
|
|
|
@@ -369,9 +342,7 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
* {@code "/accounts/1"} as opposed to {@code "/main/accounts/1"}.
|
|
|
|
|
* If specified here, the servletPath must start with a "/" and must not
|
|
|
|
|
* end with a "/".
|
|
|
|
|
*
|
|
|
|
|
* @see <a
|
|
|
|
|
* href="http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getServletPath%28%29">HttpServletRequest.getServletPath()</a>
|
|
|
|
|
* @see <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getServletPath%28%29">HttpServletRequest.getServletPath()</a>
|
|
|
|
|
*/
|
|
|
|
|
public MockHttpServletRequestBuilder servletPath(String servletPath) {
|
|
|
|
|
if (StringUtils.hasText(servletPath)) {
|
|
|
|
|
@@ -384,16 +355,12 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Specify the portion of the requestURI that represents the pathInfo.
|
|
|
|
|
*
|
|
|
|
|
* <p>If left unspecified (recommended), the pathInfo will be automatically
|
|
|
|
|
* derived by removing the contextPath and the servletPath from the
|
|
|
|
|
* requestURI and using any remaining part. If specified here, the pathInfo
|
|
|
|
|
* must start with a "/".
|
|
|
|
|
*
|
|
|
|
|
* <p>If specified, the pathInfo will be used as is.
|
|
|
|
|
*
|
|
|
|
|
* @see <a
|
|
|
|
|
* href="http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getPathInfo%28%29">HttpServletRequest.getServletPath()</a>
|
|
|
|
|
* @see <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getPathInfo%28%29">HttpServletRequest.getServletPath()</a>
|
|
|
|
|
*/
|
|
|
|
|
public MockHttpServletRequestBuilder pathInfo(String pathInfo) {
|
|
|
|
|
if (StringUtils.hasText(pathInfo)) {
|
|
|
|
|
@@ -406,7 +373,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
/**
|
|
|
|
|
* Set the secure property of the {@link ServletRequest} indicating use of a
|
|
|
|
|
* secure channel, such as HTTPS.
|
|
|
|
|
*
|
|
|
|
|
* @param secure whether the request is using a secure channel
|
|
|
|
|
*/
|
|
|
|
|
public MockHttpServletRequestBuilder secure(boolean secure){
|
|
|
|
|
@@ -419,7 +385,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
* in ways not built directly into the {@code MockHttpServletRequestBuilder}.
|
|
|
|
|
* Implementation of this interface can have builder-style methods themselves
|
|
|
|
|
* and be made accessible through static factory methods.
|
|
|
|
|
*
|
|
|
|
|
* @param postProcessor a post-processor to add
|
|
|
|
|
*/
|
|
|
|
|
public MockHttpServletRequestBuilder with(RequestPostProcessor postProcessor) {
|
|
|
|
|
@@ -439,7 +404,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
/**
|
|
|
|
|
* Merges the properties of the "parent" RequestBuilder accepting values
|
|
|
|
|
* only if not already set in "this" instance.
|
|
|
|
|
*
|
|
|
|
|
* @param parent the parent {@code RequestBuilder} to inherit properties from
|
|
|
|
|
* @return the result of the merge
|
|
|
|
|
*/
|
|
|
|
|
@@ -450,7 +414,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
if (!(parent instanceof MockHttpServletRequestBuilder)) {
|
|
|
|
|
throw new IllegalArgumentException("Cannot merge with [" + parent.getClass().getName() + "]");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MockHttpServletRequestBuilder parentBuilder = (MockHttpServletRequestBuilder) parent;
|
|
|
|
|
|
|
|
|
|
for (String headerName : parentBuilder.headers.keySet()) {
|
|
|
|
|
@@ -462,7 +425,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
if (this.contentType == null) {
|
|
|
|
|
this.contentType = parentBuilder.contentType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.content == null) {
|
|
|
|
|
this.content = parentBuilder.content;
|
|
|
|
|
}
|
|
|
|
|
@@ -472,7 +434,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
this.parameters.put(paramName, parentBuilder.parameters.get(paramName));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (Cookie cookie : parentBuilder.cookies) {
|
|
|
|
|
if (!containsCookie(cookie)) {
|
|
|
|
|
this.cookies.add(cookie);
|
|
|
|
|
@@ -482,18 +443,16 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
if (this.locale == null) {
|
|
|
|
|
this.locale = parentBuilder.locale;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.characterEncoding == null) {
|
|
|
|
|
this.characterEncoding = parentBuilder.characterEncoding;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.principal == null) {
|
|
|
|
|
this.principal = parentBuilder.principal;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.secure == null) {
|
|
|
|
|
this.secure = parentBuilder.secure;
|
|
|
|
|
}
|
|
|
|
|
if (this.principal == null) {
|
|
|
|
|
this.principal = parentBuilder.principal;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (String attributeName : parentBuilder.attributes.keySet()) {
|
|
|
|
|
if (!this.attributes.containsKey(attributeName)) {
|
|
|
|
|
@@ -535,8 +494,8 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean containsCookie(Cookie cookie) {
|
|
|
|
|
for (Cookie c : this.cookies) {
|
|
|
|
|
if (ObjectUtils.nullSafeEquals(c.getName(), cookie.getName())) {
|
|
|
|
|
for (Cookie cookieToCheck : this.cookies) {
|
|
|
|
|
if (ObjectUtils.nullSafeEquals(cookieToCheck.getName(), cookie.getName())) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -547,12 +506,10 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
* Build a {@link MockHttpServletRequest}.
|
|
|
|
|
*/
|
|
|
|
|
public final MockHttpServletRequest buildRequest(ServletContext servletContext) {
|
|
|
|
|
|
|
|
|
|
MockHttpServletRequest request = createServletRequest(servletContext);
|
|
|
|
|
|
|
|
|
|
String requestUri = this.uriComponents.getPath();
|
|
|
|
|
request.setRequestURI(requestUri);
|
|
|
|
|
|
|
|
|
|
updatePathRequestProperties(request, requestUri);
|
|
|
|
|
|
|
|
|
|
if (this.uriComponents.getScheme() != null) {
|
|
|
|
|
@@ -566,7 +523,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
request.setMethod(this.method.name());
|
|
|
|
|
|
|
|
|
|
for (String name : this.headers.keySet()) {
|
|
|
|
|
for (Object value : this.headers.get(name)) {
|
|
|
|
|
request.addHeader(name, value);
|
|
|
|
|
@@ -598,31 +554,26 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
|
|
|
|
|
request.setContentType(this.contentType);
|
|
|
|
|
request.setContent(this.content);
|
|
|
|
|
|
|
|
|
|
request.setCookies(this.cookies.toArray(new Cookie[this.cookies.size()]));
|
|
|
|
|
|
|
|
|
|
if (this.locale != null) {
|
|
|
|
|
request.addPreferredLocale(this.locale);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
request.setCharacterEncoding(this.characterEncoding);
|
|
|
|
|
|
|
|
|
|
request.setUserPrincipal(this.principal);
|
|
|
|
|
|
|
|
|
|
if (this.secure != null) {
|
|
|
|
|
request.setSecure(this.secure);
|
|
|
|
|
}
|
|
|
|
|
request.setUserPrincipal(this.principal);
|
|
|
|
|
|
|
|
|
|
for (String name : this.attributes.keySet()) {
|
|
|
|
|
request.setAttribute(name, this.attributes.get(name));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set session before session and flash attributes
|
|
|
|
|
|
|
|
|
|
if (this.session != null) {
|
|
|
|
|
request.setSession(this.session);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (String name : this.sessionAttributes.keySet()) {
|
|
|
|
|
request.getSession().setAttribute(name, this.sessionAttributes.get(name));
|
|
|
|
|
}
|
|
|
|
|
@@ -634,21 +585,22 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
flashMapManager.saveOutputFlashMap(flashMap, request, new MockHttpServletResponse());
|
|
|
|
|
|
|
|
|
|
// Apply post-processors at the very end
|
|
|
|
|
|
|
|
|
|
for (RequestPostProcessor postProcessor : this.postProcessors) {
|
|
|
|
|
request = postProcessor.postProcessRequest(request);
|
|
|
|
|
Assert.notNull(request, "Post-processor [" + postProcessor.getClass().getName() + "] returned null");
|
|
|
|
|
if (request == null) {
|
|
|
|
|
throw new IllegalStateException("Post-processor [" + postProcessor.getClass().getName() + "] returned null");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return request;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a new {@link MockHttpServletRequest} based on the given
|
|
|
|
|
* {@link ServletContext}. Can be overridden in sub-classes.
|
|
|
|
|
* Create a new {@link MockHttpServletRequest} based on the given
|
|
|
|
|
* {@link ServletContext}. Can be overridden in subclasses.
|
|
|
|
|
*/
|
|
|
|
|
protected MockHttpServletRequest createServletRequest(ServletContext servletContext) {
|
|
|
|
|
return servlet3Present ? createServlet3Request(servletContext) : new MockHttpServletRequest(servletContext);
|
|
|
|
|
return (servlet3Present ? createServlet3Request(servletContext) : new MockHttpServletRequest(servletContext));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private MockHttpServletRequest createServlet3Request(ServletContext servletContext) {
|
|
|
|
|
@@ -658,8 +610,8 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
Constructor<?> constructor = clazz.getConstructor(ServletContext.class);
|
|
|
|
|
return (MockHttpServletRequest) BeanUtils.instantiateClass(constructor, servletContext);
|
|
|
|
|
}
|
|
|
|
|
catch (Throwable t) {
|
|
|
|
|
throw new IllegalStateException("Failed to instantiate MockHttpServletRequest", t);
|
|
|
|
|
catch (Throwable ex) {
|
|
|
|
|
throw new IllegalStateException("Failed to instantiate MockHttpServletRequest", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -667,22 +619,16 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
* Update the contextPath, servletPath, and pathInfo of the request.
|
|
|
|
|
*/
|
|
|
|
|
private void updatePathRequestProperties(MockHttpServletRequest request, String requestUri) {
|
|
|
|
|
|
|
|
|
|
Assert.isTrue(requestUri.startsWith(this.contextPath),
|
|
|
|
|
"requestURI [" + requestUri + "] does not start with contextPath [" + this.contextPath + "]");
|
|
|
|
|
|
|
|
|
|
request.setContextPath(this.contextPath);
|
|
|
|
|
request.setServletPath(this.servletPath);
|
|
|
|
|
|
|
|
|
|
if (ValueConstants.DEFAULT_NONE.equals(this.pathInfo)) {
|
|
|
|
|
|
|
|
|
|
Assert.isTrue(requestUri.startsWith(this.contextPath + this.servletPath),
|
|
|
|
|
"Invalid servletPath [" + this.servletPath + "] for requestURI [" + requestUri + "]");
|
|
|
|
|
|
|
|
|
|
String extraPath = requestUri.substring(this.contextPath.length() + this.servletPath.length());
|
|
|
|
|
this.pathInfo = (StringUtils.hasText(extraPath)) ? extraPath : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
request.setPathInfo(this.pathInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -694,10 +640,12 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
|
|
|
|
flashMapManager = wac.getBean(DispatcherServlet.FLASH_MAP_MANAGER_BEAN_NAME, FlashMapManager.class);
|
|
|
|
|
}
|
|
|
|
|
catch (IllegalStateException ex) {
|
|
|
|
|
// ignore
|
|
|
|
|
}
|
|
|
|
|
catch (NoSuchBeanDefinitionException ex) {
|
|
|
|
|
// ignore
|
|
|
|
|
}
|
|
|
|
|
return (flashMapManager != null) ? flashMapManager : new SessionFlashMapManager();
|
|
|
|
|
return (flashMapManager != null ? flashMapManager : new SessionFlashMapManager());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static <T> void addToMultiValueMap(MultiValueMap<String, T> map, String name, T[] values) {
|
|
|
|
|
|