public class MockHttpServletRequestBuilder extends Object implements RequestBuilder, Mergeable
MockHttpServletRequest
required as input to perform
requests in MockMvc
.
Application tests will typically access this builder through the static factory
methods in MockMvcBuilders
.
Modifier and Type | Method and Description |
---|---|
MockHttpServletRequestBuilder |
accept(MediaType... mediaTypes)
Set the 'Accept' header to the given media type(s).
|
MockHttpServletRequestBuilder |
accept(String... mediaTypes)
Set the 'Accept' header to the given media type(s).
|
MockHttpServletRequest |
buildRequest(ServletContext servletContext)
Build a
MockHttpServletRequest . |
MockHttpServletRequestBuilder |
characterEncoding(String encoding)
Set the character encoding of the request.
|
MockHttpServletRequestBuilder |
content(byte[] content)
Set the request body.
|
MockHttpServletRequestBuilder |
content(String content)
Set the request body as a UTF-8 String.
|
MockHttpServletRequestBuilder |
contentType(MediaType mediaType)
Set the 'Content-Type' header of the request.
|
MockHttpServletRequestBuilder |
contextPath(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 given
ServletContext . |
MockHttpServletRequestBuilder |
flashAttr(String name,
Object value)
Set an "input" flash attribute.
|
MockHttpServletRequestBuilder |
flashAttrs(Map<String,Object> flashAttributes)
Set flash attributes.
|
MockHttpServletRequestBuilder |
header(String name,
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(Locale locale)
Set the locale of the request.
|
Object |
merge(Object parent)
Merges the properties of the "parent" RequestBuilder accepting values
only if not already set in "this" instance.
|
MockHttpServletRequestBuilder |
param(String name,
String... values)
Add a request parameter to the
MockHttpServletRequest . |
MockHttpServletRequestBuilder |
pathInfo(String pathInfo)
Specify the portion of the requestURI that represents the pathInfo.
|
MockHttpServletRequestBuilder |
principal(Principal principal)
Set the principal of the request.
|
MockHttpServletRequestBuilder |
requestAttr(String name,
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(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(String name,
Object value)
Set a session attribute.
|
MockHttpServletRequestBuilder |
sessionAttrs(Map<String,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 param(String name, String... values)
MockHttpServletRequest
.
If called more than once, the new values are added.name
- the parameter namevalues
- one or more valuespublic MockHttpServletRequestBuilder header(String name, 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 mediaType)
mediaType
- the content typepublic MockHttpServletRequestBuilder accept(MediaType... mediaTypes)
mediaTypes
- one or more media typespublic MockHttpServletRequestBuilder accept(String... mediaTypes)
mediaTypes
- one or more media typespublic MockHttpServletRequestBuilder content(byte[] content)
content
- the body contentpublic MockHttpServletRequestBuilder content(String content)
content
- the body contentpublic MockHttpServletRequestBuilder cookie(Cookie... cookies)
cookies
- the cookies to addpublic MockHttpServletRequestBuilder locale(Locale locale)
locale
- the localepublic MockHttpServletRequestBuilder characterEncoding(String encoding)
encoding
- the character encodingpublic MockHttpServletRequestBuilder requestAttr(String name, Object value)
name
- the attribute namevalue
- the attribute valuepublic MockHttpServletRequestBuilder sessionAttr(String name, Object value)
name
- the session attribute namevalue
- the session attribute valuepublic MockHttpServletRequestBuilder sessionAttrs(Map<String,Object> sessionAttributes)
sessionAttributes
- the session attributespublic MockHttpServletRequestBuilder flashAttr(String name, Object value)
name
- the flash attribute namevalue
- the flash attribute valuepublic MockHttpServletRequestBuilder flashAttrs(Map<String,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(Principal principal)
principal
- the principalpublic MockHttpServletRequestBuilder contextPath(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(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(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.postProcessor
- a post-processor to addpublic boolean isMergeEnabled()
isMergeEnabled
in interface Mergeable
true
.public Object merge(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 given
ServletContext
. Can be overridden in subclasses.