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
.
Although this class cannot be extended, additional ways to initialize the
MockHttpServletRequest
can be plugged in via with(RequestPostProcessor)
.
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).
|
MockHttpServletRequest |
buildRequest(ServletContext servletContext)
Build a
MockHttpServletRequest . |
MockHttpServletRequestBuilder |
characterEncoding(java.lang.String encoding)
Set the character encoding of the request.
|
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.
|
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... locales)
Add the specified locales as preferred request locales.
|
MockHttpServletRequestBuilder |
locale(java.util.Locale locale)
Set the locale of the request, overriding any previous locales.
|
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 |
params(MultiValueMap<java.lang.String,java.lang.String> params)
Add a map of request parameters to the
MockHttpServletRequest ,
for example when testing a form submission. |
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.
|
MockHttpServletRequestBuilder |
with(RequestPostProcessor postProcessor)
An extension point for further initialization of
MockHttpServletRequest
in ways not built directly into the MockHttpServletRequestBuilder . |
public 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 "/".
HttpServletRequest.getContextPath()
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 "/".
HttpServletRequest.getServletPath()
public MockHttpServletRequestBuilder pathInfo(@Nullable 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.
HttpServletRequest.getPathInfo()
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 characterEncoding(java.lang.String encoding)
encoding
- the character encodingpublic MockHttpServletRequestBuilder content(byte[] content)
content
- the body contentpublic MockHttpServletRequestBuilder content(java.lang.String content)
content
- the body contentpublic 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 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 param(java.lang.String name, java.lang.String... values)
MockHttpServletRequest
.
If called more than once, new values get added to existing ones.
name
- the parameter namevalues
- one or more valuespublic MockHttpServletRequestBuilder params(MultiValueMap<java.lang.String,java.lang.String> params)
MockHttpServletRequest
,
for example when testing a form submission.
If called more than once, new values get added to existing ones.
params
- the parameters to addpublic MockHttpServletRequestBuilder cookie(Cookie... cookies)
cookies
- the cookies to addpublic MockHttpServletRequestBuilder locale(java.util.Locale... locales)
locales
- the locales to addlocale(Locale)
public MockHttpServletRequestBuilder locale(@Nullable java.util.Locale locale)
locale
- the locale, or null
to reset itlocale(Locale...)
public 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 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(@Nullable java.lang.Object parent)
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.
public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request)
SmartRequestBuilder
RequestPostProcessor
s.postProcessRequest
in interface SmartRequestBuilder
request
- the request to initialize