Class MockHttpServletRequestBuilder
- All Implemented Interfaces:
Mergeable
,ConfigurableSmartRequestBuilder<MockHttpServletRequestBuilder>
,RequestBuilder
,SmartRequestBuilder
- Direct Known Subclasses:
MockMultipartHttpServletRequestBuilder
MockHttpServletRequest
required as input to
perform requests in MockMvc
.
Application tests will typically access this builder through the static
factory methods in MockMvcRequestBuilders
.
This class is not open for extension. To apply custom initialization to
the created MockHttpServletRequest
, please use the
with(RequestPostProcessor)
extension point.
- Since:
- 3.2
- Author:
- Rossen Stoyanchev, Juergen Hoeller, Arjen Poutsma, Sam Brannen, Kamill Sokol
-
Method Summary
Modifier and TypeMethodDescriptionSet theAccept
header using raw String values, possibly not even well-formed (for testing purposes).Set the 'Accept' header to the given media type(s).characterEncoding
(String encoding) Set the character encoding of the request.characterEncoding
(Charset encoding) Set the character encoding of the request.content
(byte[] content) Set the request body.Set the request body as a UTF-8 String.contentType
(String contentType) Set the 'Content-Type' header of the request as a raw String value, possibly not even well-formed (for testing purposes).contentType
(MediaType contentType) Set the 'Content-Type' header of the request.contextPath
(String contextPath) Specify the portion of the requestURI that represents the context path.Add the given cookies to the request.Set an "input" flash attribute.flashAttrs
(Map<String, Object> flashAttributes) Set flash attributes.Append the given value(s) to the given form field and also add them to the request parameters map.formFields
(MultiValueMap<String, String> formFields) Variant ofAbstractMockHttpServletRequestBuilder.formField(String, String...)
with aMultiValueMap
.Add a header to the request.headers
(HttpHeaders httpHeaders) Add all headers to the request.Set the locale of the request, overriding any previous locales.Add the specified locales as preferred request locales.Add a request parameter toMockHttpServletRequest.getParameterMap()
.params
(MultiValueMap<String, String> params) Variant ofAbstractMockHttpServletRequestBuilder.param(String, String...)
with aMultiValueMap
.Specify the portion of the requestURI that represents the pathInfo.Set the principal of the request.queryParam
(String name, String... values) Append to the query string and also add to therequest parameters
map.queryParams
(MultiValueMap<String, String> params) Append to the query string and also add to therequest parameters
map.remoteAddress
(String remoteAddress) Set the remote address of the request.requestAttr
(String name, Object value) Set a request attribute.secure
(boolean secure) Set the secure property of theServletRequest
indicating use of a secure channel, such as HTTPS.servletPath
(String servletPath) Specify the portion of the requestURI that represents the path to which the Servlet is mapped.session
(MockHttpSession session) Set the HTTP session to use, possibly re-used across requests.sessionAttr
(String name, Object value) Set a session attribute.sessionAttrs
(Map<String, Object> sessionAttributes) Set session attributes.Specify the URI for the request using a URI template and URI variables.Specify the URI using an absolute, fully constructedURI
.with
(RequestPostProcessor postProcessor) An extension point for further initialization ofMockHttpServletRequest
in ways not built directly into theMockHttpServletRequestBuilder
.Methods inherited from class org.springframework.test.web.servlet.request.AbstractMockHttpServletRequestBuilder
buildRequest, createServletRequest, isMergeEnabled, merge, postProcessRequest, self
-
Method Details
-
uri
Description copied from class:AbstractMockHttpServletRequestBuilder
Specify the URI using an absolute, fully constructedURI
.- Overrides:
uri
in classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
-
uri
Description copied from class:AbstractMockHttpServletRequestBuilder
Specify the URI for the request using a URI template and URI variables.- Overrides:
uri
in classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
-
contextPath
Description copied from class:AbstractMockHttpServletRequestBuilder
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.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 "/".
- Overrides:
contextPath
in classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- See Also:
-
servletPath
Description copied from class:AbstractMockHttpServletRequestBuilder
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.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 "/".- Overrides:
servletPath
in classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- See Also:
-
pathInfo
Description copied from class:AbstractMockHttpServletRequestBuilder
Specify the portion of the requestURI that represents the 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.
- Overrides:
pathInfo
in classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- See Also:
-
secure
Description copied from class:AbstractMockHttpServletRequestBuilder
Set the secure property of theServletRequest
indicating use of a secure channel, such as HTTPS.- Overrides:
secure
in classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
secure
- whether the request is using a secure channel
-
characterEncoding
Description copied from class:AbstractMockHttpServletRequestBuilder
Set the character encoding of the request.- Overrides:
characterEncoding
in classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
encoding
- the character encoding- See Also:
-
characterEncoding
Description copied from class:AbstractMockHttpServletRequestBuilder
Set the character encoding of the request.- Overrides:
characterEncoding
in classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
encoding
- the character encoding
-
content
Description copied from class:AbstractMockHttpServletRequestBuilder
Set the request body.If content is provided and
AbstractMockHttpServletRequestBuilder.contentType(MediaType)
is set toapplication/x-www-form-urlencoded
, the content will be parsed and used to populate therequest parameters
map.- Overrides:
content
in classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
content
- the body content
-
content
Description copied from class:AbstractMockHttpServletRequestBuilder
Set the request body as a UTF-8 String.If content is provided and
AbstractMockHttpServletRequestBuilder.contentType(MediaType)
is set toapplication/x-www-form-urlencoded
, the content will be parsed and used to populate therequest parameters
map.- Overrides:
content
in classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
content
- the body content
-
contentType
Description copied from class:AbstractMockHttpServletRequestBuilder
Set the 'Content-Type' header of the request.If content is provided and
contentType
is set toapplication/x-www-form-urlencoded
, the content will be parsed and used to populate therequest parameters
map.- Overrides:
contentType
in classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
contentType
- the content type
-
contentType
Description copied from class:AbstractMockHttpServletRequestBuilder
Set the 'Content-Type' header of the request as a raw String value, possibly not even well-formed (for testing purposes).- Overrides:
contentType
in classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
contentType
- the content type
-
accept
Description copied from class:AbstractMockHttpServletRequestBuilder
Set the 'Accept' header to the given media type(s).- Overrides:
accept
in classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
mediaTypes
- one or more media types
-
accept
Description copied from class:AbstractMockHttpServletRequestBuilder
Set theAccept
header using raw String values, possibly not even well-formed (for testing purposes).- Overrides:
accept
in classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
mediaTypes
- one or more media types; internally joined as comma-separated String
-
header
Description copied from class:AbstractMockHttpServletRequestBuilder
Add a header to the request. Values are always added.- Overrides:
header
in classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
name
- the header namevalues
- one or more header values
-
headers
Description copied from class:AbstractMockHttpServletRequestBuilder
Add all headers to the request. Values are always added.- Overrides:
headers
in classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
httpHeaders
- the headers and values to add
-
param
Description copied from class:AbstractMockHttpServletRequestBuilder
Add a request parameter toMockHttpServletRequest.getParameterMap()
.In the Servlet API, a request parameter may be parsed from the query string and/or from the body of an
application/x-www-form-urlencoded
request. This method simply adds to the request parameter map. You may also use add Servlet request parameters by specifying the query or form data through one of the following:- Supply a URL with a query to
MockMvcRequestBuilders
. - Add query params via
AbstractMockHttpServletRequestBuilder.queryParam(java.lang.String, java.lang.String...)
orAbstractMockHttpServletRequestBuilder.queryParams
. - Provide
AbstractMockHttpServletRequestBuilder.content
withAbstractMockHttpServletRequestBuilder.contentType
application/x-www-form-urlencoded
.
- Overrides:
param
in classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
name
- the parameter namevalues
- one or more values
- Supply a URL with a query to
-
params
Description copied from class:AbstractMockHttpServletRequestBuilder
Variant ofAbstractMockHttpServletRequestBuilder.param(String, String...)
with aMultiValueMap
.- Overrides:
params
in classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
params
- the parameters to add
-
queryParam
Description copied from class:AbstractMockHttpServletRequestBuilder
Append to the query string and also add to therequest parameters
map. The parameter name and value are encoded when they are added to the query string.- Overrides:
queryParam
in classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
name
- the parameter namevalues
- one or more values
-
queryParams
Description copied from class:AbstractMockHttpServletRequestBuilder
Append to the query string and also add to therequest parameters
map. The parameter name and value are encoded when they are added to the query string.- Overrides:
queryParams
in classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
params
- the parameters to add
-
formField
Description copied from class:AbstractMockHttpServletRequestBuilder
Append the given value(s) to the given form field and also add them to the request parameters map.- Overrides:
formField
in classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
name
- the field namevalues
- one or more values
-
formFields
Description copied from class:AbstractMockHttpServletRequestBuilder
Variant ofAbstractMockHttpServletRequestBuilder.formField(String, String...)
with aMultiValueMap
.- Overrides:
formFields
in classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
formFields
- the form fields to add
-
cookie
Description copied from class:AbstractMockHttpServletRequestBuilder
Add the given cookies to the request. Cookies are always added.- Overrides:
cookie
in classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
cookies
- the cookies to add
-
locale
Description copied from class:AbstractMockHttpServletRequestBuilder
Add the specified locales as preferred request locales.- Overrides:
locale
in classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
locales
- the locales to add- See Also:
-
locale
Description copied from class:AbstractMockHttpServletRequestBuilder
Set the locale of the request, overriding any previous locales.- Overrides:
locale
in classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
locale
- the locale, ornull
to reset it- See Also:
-
requestAttr
Description copied from class:AbstractMockHttpServletRequestBuilder
Set a request attribute.- Overrides:
requestAttr
in classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
name
- the attribute namevalue
- the attribute value
-
sessionAttr
Description copied from class:AbstractMockHttpServletRequestBuilder
Set a session attribute.- Overrides:
sessionAttr
in classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
name
- the session attribute namevalue
- the session attribute value
-
sessionAttrs
Description copied from class:AbstractMockHttpServletRequestBuilder
Set session attributes.- Overrides:
sessionAttrs
in classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
sessionAttributes
- the session attributes
-
flashAttr
Description copied from class:AbstractMockHttpServletRequestBuilder
Set an "input" flash attribute.- Overrides:
flashAttr
in classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
name
- the flash attribute namevalue
- the flash attribute value
-
flashAttrs
Description copied from class:AbstractMockHttpServletRequestBuilder
Set flash attributes.- Overrides:
flashAttrs
in classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
flashAttributes
- the flash attributes
-
session
Description copied from class:AbstractMockHttpServletRequestBuilder
Set the HTTP session to use, possibly re-used across requests.Individual attributes provided via
AbstractMockHttpServletRequestBuilder.sessionAttr(String, Object)
override the content of the session provided here.- Overrides:
session
in classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
session
- the HTTP session
-
principal
Description copied from class:AbstractMockHttpServletRequestBuilder
Set the principal of the request.- Overrides:
principal
in classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
principal
- the principal
-
remoteAddress
Description copied from class:AbstractMockHttpServletRequestBuilder
Set the remote address of the request.- Overrides:
remoteAddress
in classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
remoteAddress
- the remote address (IP)
-
with
Description copied from class:AbstractMockHttpServletRequestBuilder
An extension point for further initialization ofMockHttpServletRequest
in ways not built directly into theMockHttpServletRequestBuilder
. Implementation of this interface can have builder-style methods themselves and be made accessible through static factory methods.- Specified by:
with
in interfaceConfigurableSmartRequestBuilder<MockHttpServletRequestBuilder>
- Overrides:
with
in classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
postProcessor
- a post-processor to add
-