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 | Field and Description |
---|---|
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.List<java.util.Locale> |
locales |
private java.lang.String |
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.util.Map<java.lang.String,java.lang.Object> |
requestAttributes |
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)
Alternative to
MockHttpServletRequestBuilder(HttpMethod, String, Object...)
with a pre-built URI. |
MockHttpServletRequestBuilder(java.lang.String httpMethod,
java.net.URI url)
Alternative constructor for custom HTTP methods.
|
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 void |
addRequestParams(MockHttpServletRequest request,
MultiValueMap<java.lang.String,java.lang.String> map) |
private static void |
addToMap(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... 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. |
private MultiValueMap<java.lang.String,java.lang.String> |
parseFormData(MediaType mediaType) |
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 java.lang.String method
private final java.net.URI url
private java.lang.String contextPath
private java.lang.String servletPath
private java.lang.String pathInfo
private java.lang.Boolean secure
private java.security.Principal principal
private MockHttpSession session
private java.lang.String characterEncoding
private byte[] content
private java.lang.String contentType
private final MultiValueMap<java.lang.String,java.lang.Object> headers
private final MultiValueMap<java.lang.String,java.lang.String> parameters
private final java.util.List<Cookie> cookies
private final java.util.List<java.util.Locale> locales
private final java.util.Map<java.lang.String,java.lang.Object> requestAttributes
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 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 URI variablesMockHttpServletRequestBuilder(HttpMethod httpMethod, java.net.URI url)
MockHttpServletRequestBuilder(HttpMethod, String, Object...)
with a pre-built URI.httpMethod
- the HTTP method (GET, POST, etc)url
- the URLMockHttpServletRequestBuilder(java.lang.String httpMethod, java.net.URI url)
httpMethod
- the HTTP method (GET, POST, etc)url
- the URLpublic 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 "/".
javax.servlet.http.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 "/".
javax.servlet.http.HttpServletRequest#getServletPath()
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.
javax.servlet.http.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(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(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 void addRequestParams(MockHttpServletRequest request, MultiValueMap<java.lang.String,java.lang.String> map)
private MultiValueMap<java.lang.String,java.lang.String> parseFormData(MediaType mediaType)
private FlashMapManager getFlashMapManager(MockHttpServletRequest request)
public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request)
SmartRequestBuilder
RequestPostProcessor
s.postProcessRequest
in interface SmartRequestBuilder
request
- the request to initializeprivate static void addToMap(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)