public class MockHttpServletRequestBuilder extends java.lang.Object implements ConfigurableSmartRequestBuilder<MockHttpServletRequestBuilder>, Mergeable
MockHttpServletRequest
required as input to perform
requests in MockMvc
.
Application tests will typically access this builder through the static factory
methods in MockMvcRequestBuilders
.
Modifier and Type | Field and Description |
---|---|
private java.util.Map<java.lang.String,java.lang.Object> |
attributes |
private java.lang.String |
characterEncoding |
private byte[] |
content |
private java.lang.String |
contentType |
private java.lang.String |
contextPath |
private java.util.List<Cookie> |
cookies |
private java.util.Map<java.lang.String,java.lang.Object> |
flashAttributes |
private MultiValueMap<java.lang.String,java.lang.Object> |
headers |
private java.util.Locale |
locale |
private HttpMethod |
method |
private MultiValueMap<java.lang.String,java.lang.String> |
parameters |
private java.lang.String |
pathInfo |
private java.util.List<RequestPostProcessor> |
postProcessors |
private java.security.Principal |
principal |
private java.lang.Boolean |
secure |
private java.lang.String |
servletPath |
private MockHttpSession |
session |
private java.util.Map<java.lang.String,java.lang.Object> |
sessionAttributes |
private java.net.URI |
url |
Constructor and Description |
---|
MockHttpServletRequestBuilder(HttpMethod httpMethod,
java.lang.String url,
java.lang.Object... vars)
Package private constructor.
|
MockHttpServletRequestBuilder(HttpMethod httpMethod,
java.net.URI url)
Package private constructor.
|
Modifier and Type | Method and Description |
---|---|
MockHttpServletRequestBuilder |
accept(MediaType... mediaTypes)
Set the 'Accept' header to the given media type(s).
|
MockHttpServletRequestBuilder |
accept(java.lang.String... mediaTypes)
Set the 'Accept' header to the given media type(s).
|
private static void |
addAttributeToMap(java.util.Map<java.lang.String,java.lang.Object> map,
java.lang.String name,
java.lang.Object value) |
private static <T> void |
addToMultiValueMap(MultiValueMap<java.lang.String,T> map,
java.lang.String name,
T[] values) |
MockHttpServletRequest |
buildRequest(ServletContext servletContext)
Build a
MockHttpServletRequest . |
MockHttpServletRequestBuilder |
characterEncoding(java.lang.String encoding)
Set the character encoding of the request.
|
private boolean |
containsCookie(Cookie cookie) |
MockHttpServletRequestBuilder |
content(byte[] content)
Set the request body.
|
MockHttpServletRequestBuilder |
content(java.lang.String content)
Set the request body as a UTF-8 String.
|
MockHttpServletRequestBuilder |
contentType(MediaType contentType)
Set the 'Content-Type' header of the request.
|
MockHttpServletRequestBuilder |
contentType(java.lang.String contentType)
Set the 'Content-Type' header of the request.
|
MockHttpServletRequestBuilder |
contextPath(java.lang.String contextPath)
Specify the portion of the requestURI that represents the context path.
|
MockHttpServletRequestBuilder |
cookie(Cookie... cookies)
Add the given cookies to the request.
|
protected MockHttpServletRequest |
createServletRequest(ServletContext servletContext)
Create a new
MockHttpServletRequest based on the supplied
ServletContext . |
MockHttpServletRequestBuilder |
flashAttr(java.lang.String name,
java.lang.Object value)
Set an "input" flash attribute.
|
MockHttpServletRequestBuilder |
flashAttrs(java.util.Map<java.lang.String,java.lang.Object> flashAttributes)
Set flash attributes.
|
private FlashMapManager |
getFlashMapManager(MockHttpServletRequest request) |
MockHttpServletRequestBuilder |
header(java.lang.String name,
java.lang.Object... values)
Add a header to the request.
|
MockHttpServletRequestBuilder |
headers(HttpHeaders httpHeaders)
Add all headers to the request.
|
boolean |
isMergeEnabled()
Is merging enabled for this particular instance?
|
MockHttpServletRequestBuilder |
locale(java.util.Locale locale)
Set the locale of the request.
|
java.lang.Object |
merge(java.lang.Object parent)
Merges the properties of the "parent" RequestBuilder accepting values
only if not already set in "this" instance.
|
MockHttpServletRequestBuilder |
param(java.lang.String name,
java.lang.String... values)
Add a request parameter to the
MockHttpServletRequest . |
MockHttpServletRequestBuilder |
pathInfo(java.lang.String pathInfo)
Specify the portion of the requestURI that represents the pathInfo.
|
MockHttpServletRequest |
postProcessRequest(MockHttpServletRequest request)
Apply request post processing.
|
MockHttpServletRequestBuilder |
principal(java.security.Principal principal)
Set the principal of the request.
|
MockHttpServletRequestBuilder |
requestAttr(java.lang.String name,
java.lang.Object value)
Set a request attribute.
|
MockHttpServletRequestBuilder |
secure(boolean secure)
Set the secure property of the
ServletRequest indicating use of a
secure channel, such as HTTPS. |
MockHttpServletRequestBuilder |
servletPath(java.lang.String servletPath)
Specify the portion of the requestURI that represents the path to which
the Servlet is mapped.
|
MockHttpServletRequestBuilder |
session(MockHttpSession session)
Set the HTTP session to use, possibly re-used across requests.
|
MockHttpServletRequestBuilder |
sessionAttr(java.lang.String name,
java.lang.Object value)
Set a session attribute.
|
MockHttpServletRequestBuilder |
sessionAttrs(java.util.Map<java.lang.String,java.lang.Object> sessionAttributes)
Set session attributes.
|
private void |
updatePathRequestProperties(MockHttpServletRequest request,
java.lang.String requestUri)
Update the contextPath, servletPath, and pathInfo of the request.
|
MockHttpServletRequestBuilder |
with(RequestPostProcessor postProcessor)
An extension point for further initialization of
MockHttpServletRequest
in ways not built directly into the MockHttpServletRequestBuilder . |
private final HttpMethod method
private final java.net.URI url
private final MultiValueMap<java.lang.String,java.lang.Object> headers
private java.lang.String contentType
private byte[] content
private final MultiValueMap<java.lang.String,java.lang.String> parameters
private final java.util.List<Cookie> cookies
private java.util.Locale locale
private java.lang.String characterEncoding
private java.lang.Boolean secure
private java.security.Principal principal
private final java.util.Map<java.lang.String,java.lang.Object> attributes
private MockHttpSession session
private final java.util.Map<java.lang.String,java.lang.Object> sessionAttributes
private final java.util.Map<java.lang.String,java.lang.Object> flashAttributes
private java.lang.String contextPath
private java.lang.String servletPath
private java.lang.String pathInfo
private final java.util.List<RequestPostProcessor> postProcessors
MockHttpServletRequestBuilder(HttpMethod httpMethod, java.lang.String url, java.lang.Object... vars)
MockMvcRequestBuilders
.
Although this class cannot be extended, additional ways to initialize
the MockHttpServletRequest
can be plugged in via
with(RequestPostProcessor)
.
httpMethod
- the HTTP method (GET, POST, etc)url
- a URL template; the resulting URL will be encodedvars
- zero or more URL variablesMockHttpServletRequestBuilder(HttpMethod httpMethod, java.net.URI url)
MockMvcRequestBuilders
.
Although this class cannot be extended, additional ways to initialize
the MockHttpServletRequest
can be plugged in via
with(RequestPostProcessor)
.
httpMethod
- the HTTP method (GET, POST, etc)url
- the URLpublic MockHttpServletRequestBuilder param(java.lang.String name, java.lang.String... values)
MockHttpServletRequest
.
If called more than once, the new values are added.name
- the parameter namevalues
- one or more valuespublic MockHttpServletRequestBuilder header(java.lang.String name, java.lang.Object... values)
name
- the header namevalues
- one or more header valuespublic MockHttpServletRequestBuilder headers(HttpHeaders httpHeaders)
httpHeaders
- the headers and values to addpublic MockHttpServletRequestBuilder contentType(MediaType contentType)
contentType
- the content typepublic MockHttpServletRequestBuilder contentType(java.lang.String contentType)
contentType
- the content typepublic MockHttpServletRequestBuilder accept(MediaType... mediaTypes)
mediaTypes
- one or more media typespublic MockHttpServletRequestBuilder accept(java.lang.String... mediaTypes)
mediaTypes
- one or more media typespublic MockHttpServletRequestBuilder content(byte[] content)
content
- the body contentpublic MockHttpServletRequestBuilder content(java.lang.String content)
content
- the body contentpublic MockHttpServletRequestBuilder cookie(Cookie... cookies)
cookies
- the cookies to addpublic MockHttpServletRequestBuilder locale(java.util.Locale locale)
locale
- the localepublic MockHttpServletRequestBuilder characterEncoding(java.lang.String encoding)
encoding
- the character encodingpublic MockHttpServletRequestBuilder requestAttr(java.lang.String name, java.lang.Object value)
name
- the attribute namevalue
- the attribute valuepublic MockHttpServletRequestBuilder sessionAttr(java.lang.String name, java.lang.Object value)
name
- the session attribute namevalue
- the session attribute valuepublic MockHttpServletRequestBuilder sessionAttrs(java.util.Map<java.lang.String,java.lang.Object> sessionAttributes)
sessionAttributes
- the session attributespublic MockHttpServletRequestBuilder flashAttr(java.lang.String name, java.lang.Object value)
name
- the flash attribute namevalue
- the flash attribute valuepublic MockHttpServletRequestBuilder flashAttrs(java.util.Map<java.lang.String,java.lang.Object> flashAttributes)
flashAttributes
- the flash attributespublic MockHttpServletRequestBuilder session(MockHttpSession session)
Individual attributes provided via sessionAttr(String, Object)
override the content of the session provided here.
session
- the HTTP sessionpublic MockHttpServletRequestBuilder principal(java.security.Principal principal)
principal
- the principalpublic MockHttpServletRequestBuilder contextPath(java.lang.String contextPath)
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 "/".
public MockHttpServletRequestBuilder servletPath(java.lang.String servletPath)
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
mapped to "/main/*"
, tests can be written with the requestURI
"/accounts/1"
as opposed to "/main/accounts/1"
.
If specified here, the servletPath must start with a "/" and must not
end with a "/".
public MockHttpServletRequestBuilder pathInfo(java.lang.String pathInfo)
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 "/".
If specified, the pathInfo will be used as is.
public MockHttpServletRequestBuilder secure(boolean secure)
ServletRequest
indicating use of a
secure channel, such as HTTPS.secure
- whether the request is using a secure channelpublic MockHttpServletRequestBuilder with(RequestPostProcessor postProcessor)
MockHttpServletRequest
in ways not built directly into the MockHttpServletRequestBuilder
.
Implementation of this interface can have builder-style methods themselves
and be made accessible through static factory methods.with
in interface ConfigurableSmartRequestBuilder<MockHttpServletRequestBuilder>
postProcessor
- a post-processor to addpublic boolean isMergeEnabled()
isMergeEnabled
in interface Mergeable
true
.public java.lang.Object merge(java.lang.Object parent)
private boolean containsCookie(Cookie cookie)
public final MockHttpServletRequest buildRequest(ServletContext servletContext)
MockHttpServletRequest
.buildRequest
in interface RequestBuilder
servletContext
- the ServletContext
to use to create the requestprotected MockHttpServletRequest createServletRequest(ServletContext servletContext)
MockHttpServletRequest
based on the supplied
ServletContext
.
Can be overridden in subclasses.
private void updatePathRequestProperties(MockHttpServletRequest request, java.lang.String requestUri)
private FlashMapManager getFlashMapManager(MockHttpServletRequest request)
public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request)
SmartRequestBuilder
RequestPostProcessor
s.postProcessRequest
in interface SmartRequestBuilder
request
- the request to initializeprivate static <T> void addToMultiValueMap(MultiValueMap<java.lang.String,T> map, java.lang.String name, T[] values)
private static void addAttributeToMap(java.util.Map<java.lang.String,java.lang.Object> map, java.lang.String name, java.lang.Object value)