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 theAcceptheader 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 parametersmap.queryParams(MultiValueMap<String, String> params) Append to the query string and also add to therequest parametersmap.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 theServletRequestindicating 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 ofMockHttpServletRequestin 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:AbstractMockHttpServletRequestBuilderSpecify the URI using an absolute, fully constructedURI.- Overrides:
uriin classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
-
uri
Description copied from class:AbstractMockHttpServletRequestBuilderSpecify the URI for the request using a URI template and URI variables.- Overrides:
uriin classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
-
contextPath
Description copied from class:AbstractMockHttpServletRequestBuilderSpecify 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:
contextPathin classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>- See Also:
-
servletPath
Description copied from class:AbstractMockHttpServletRequestBuilderSpecify 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:
servletPathin classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>- See Also:
-
pathInfo
Description copied from class:AbstractMockHttpServletRequestBuilderSpecify 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:
pathInfoin classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>- See Also:
-
secure
Description copied from class:AbstractMockHttpServletRequestBuilderSet the secure property of theServletRequestindicating use of a secure channel, such as HTTPS.- Overrides:
securein classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>- Parameters:
secure- whether the request is using a secure channel
-
characterEncoding
Description copied from class:AbstractMockHttpServletRequestBuilderSet the character encoding of the request.- Overrides:
characterEncodingin classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>- Parameters:
encoding- the character encoding- See Also:
-
characterEncoding
Description copied from class:AbstractMockHttpServletRequestBuilderSet the character encoding of the request.- Overrides:
characterEncodingin classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>- Parameters:
encoding- the character encoding
-
content
Description copied from class:AbstractMockHttpServletRequestBuilderSet 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 parametersmap.- Overrides:
contentin classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>- Parameters:
content- the body content
-
content
Description copied from class:AbstractMockHttpServletRequestBuilderSet 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 parametersmap.- Overrides:
contentin classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>- Parameters:
content- the body content
-
contentType
Description copied from class:AbstractMockHttpServletRequestBuilderSet the 'Content-Type' header of the request.If content is provided and
contentTypeis set toapplication/x-www-form-urlencoded, the content will be parsed and used to populate therequest parametersmap.- Overrides:
contentTypein classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>- Parameters:
contentType- the content type
-
contentType
Description copied from class:AbstractMockHttpServletRequestBuilderSet the 'Content-Type' header of the request as a raw String value, possibly not even well-formed (for testing purposes).- Overrides:
contentTypein classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>- Parameters:
contentType- the content type
-
accept
Description copied from class:AbstractMockHttpServletRequestBuilderSet the 'Accept' header to the given media type(s).- Overrides:
acceptin classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>- Parameters:
mediaTypes- one or more media types
-
accept
Description copied from class:AbstractMockHttpServletRequestBuilderSet theAcceptheader using raw String values, possibly not even well-formed (for testing purposes).- Overrides:
acceptin classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>- Parameters:
mediaTypes- one or more media types; internally joined as comma-separated String
-
header
Description copied from class:AbstractMockHttpServletRequestBuilderAdd a header to the request. Values are always added.- Overrides:
headerin classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>- Parameters:
name- the header namevalues- one or more header values
-
headers
Description copied from class:AbstractMockHttpServletRequestBuilderAdd all headers to the request. Values are always added.- Overrides:
headersin classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>- Parameters:
httpHeaders- the headers and values to add
-
param
Description copied from class:AbstractMockHttpServletRequestBuilderAdd 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-urlencodedrequest. 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.contentwithAbstractMockHttpServletRequestBuilder.contentTypeapplication/x-www-form-urlencoded.
- Overrides:
paramin classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>- Parameters:
name- the parameter namevalues- one or more values
- Supply a URL with a query to
-
params
Description copied from class:AbstractMockHttpServletRequestBuilderVariant ofAbstractMockHttpServletRequestBuilder.param(String, String...)with aMultiValueMap.- Overrides:
paramsin classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>- Parameters:
params- the parameters to add
-
queryParam
Description copied from class:AbstractMockHttpServletRequestBuilderAppend to the query string and also add to therequest parametersmap. The parameter name and value are encoded when they are added to the query string.- Overrides:
queryParamin classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>- Parameters:
name- the parameter namevalues- one or more values
-
queryParams
Description copied from class:AbstractMockHttpServletRequestBuilderAppend to the query string and also add to therequest parametersmap. The parameter name and value are encoded when they are added to the query string.- Overrides:
queryParamsin classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>- Parameters:
params- the parameters to add
-
formField
Description copied from class:AbstractMockHttpServletRequestBuilderAppend the given value(s) to the given form field and also add them to the request parameters map.- Overrides:
formFieldin classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>- Parameters:
name- the field namevalues- one or more values
-
formFields
Description copied from class:AbstractMockHttpServletRequestBuilderVariant ofAbstractMockHttpServletRequestBuilder.formField(String, String...)with aMultiValueMap.- Overrides:
formFieldsin classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>- Parameters:
formFields- the form fields to add
-
cookie
Description copied from class:AbstractMockHttpServletRequestBuilderAdd the given cookies to the request. Cookies are always added.- Overrides:
cookiein classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>- Parameters:
cookies- the cookies to add
-
locale
Description copied from class:AbstractMockHttpServletRequestBuilderAdd the specified locales as preferred request locales.- Overrides:
localein classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>- Parameters:
locales- the locales to add- See Also:
-
locale
Description copied from class:AbstractMockHttpServletRequestBuilderSet the locale of the request, overriding any previous locales.- Overrides:
localein classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>- Parameters:
locale- the locale, ornullto reset it- See Also:
-
requestAttr
Description copied from class:AbstractMockHttpServletRequestBuilderSet a request attribute.- Overrides:
requestAttrin classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>- Parameters:
name- the attribute namevalue- the attribute value
-
sessionAttr
Description copied from class:AbstractMockHttpServletRequestBuilderSet a session attribute.- Overrides:
sessionAttrin classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>- Parameters:
name- the session attribute namevalue- the session attribute value
-
sessionAttrs
Description copied from class:AbstractMockHttpServletRequestBuilderSet session attributes.- Overrides:
sessionAttrsin classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>- Parameters:
sessionAttributes- the session attributes
-
flashAttr
Description copied from class:AbstractMockHttpServletRequestBuilderSet an "input" flash attribute.- Overrides:
flashAttrin classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>- Parameters:
name- the flash attribute namevalue- the flash attribute value
-
flashAttrs
Description copied from class:AbstractMockHttpServletRequestBuilderSet flash attributes.- Overrides:
flashAttrsin classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>- Parameters:
flashAttributes- the flash attributes
-
session
Description copied from class:AbstractMockHttpServletRequestBuilderSet 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:
sessionin classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>- Parameters:
session- the HTTP session
-
principal
Description copied from class:AbstractMockHttpServletRequestBuilderSet the principal of the request.- Overrides:
principalin classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>- Parameters:
principal- the principal
-
remoteAddress
Description copied from class:AbstractMockHttpServletRequestBuilderSet the remote address of the request.- Overrides:
remoteAddressin classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>- Parameters:
remoteAddress- the remote address (IP)
-
with
Description copied from class:AbstractMockHttpServletRequestBuilderAn extension point for further initialization ofMockHttpServletRequestin 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:
within interfaceConfigurableSmartRequestBuilder<MockHttpServletRequestBuilder>- Overrides:
within classAbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>- Parameters:
postProcessor- a post-processor to add
-