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 SummaryModifier 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.AbstractMockHttpServletRequestBuilderbuildRequest, createServletRequest, isMergeEnabled, merge, postProcessRequest, self
- 
Method Details- 
uriDescription copied from class:AbstractMockHttpServletRequestBuilderSpecify the URI using an absolute, fully constructedURI.- Overrides:
- uriin class- AbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
 
- 
uriDescription copied from class:AbstractMockHttpServletRequestBuilderSpecify the URI for the request using a URI template and URI variables.- Overrides:
- uriin class- AbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
 
- 
contextPathDescription 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 class- AbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- See Also:
 
- 
servletPathDescription 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 class- AbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- See Also:
 
- 
pathInfoDescription 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 class- AbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- See Also:
 
- 
secureDescription copied from class:AbstractMockHttpServletRequestBuilderSet the secure property of theServletRequestindicating use of a secure channel, such as HTTPS.- Overrides:
- securein class- AbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
- secure- whether the request is using a secure channel
 
- 
characterEncodingDescription copied from class:AbstractMockHttpServletRequestBuilderSet the character encoding of the request.- Overrides:
- characterEncodingin class- AbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
- encoding- the character encoding
- See Also:
 
- 
characterEncodingDescription copied from class:AbstractMockHttpServletRequestBuilderSet the character encoding of the request.- Overrides:
- characterEncodingin class- AbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
- encoding- the character encoding
 
- 
contentDescription 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 class- AbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
- content- the body content
 
- 
contentDescription 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 class- AbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
- content- the body content
 
- 
contentTypeDescription 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 class- AbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
- contentType- the content type
 
- 
contentTypeDescription 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 class- AbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
- contentType- the content type
 
- 
acceptDescription copied from class:AbstractMockHttpServletRequestBuilderSet the 'Accept' header to the given media type(s).- Overrides:
- acceptin class- AbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
- mediaTypes- one or more media types
 
- 
acceptDescription copied from class:AbstractMockHttpServletRequestBuilderSet theAcceptheader using raw String values, possibly not even well-formed (for testing purposes).- Overrides:
- acceptin class- AbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
- mediaTypes- one or more media types; internally joined as comma-separated String
 
- 
headerDescription copied from class:AbstractMockHttpServletRequestBuilderAdd a header to the request. Values are always added.- Overrides:
- headerin class- AbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
- name- the header name
- values- one or more header values
 
- 
headersDescription copied from class:AbstractMockHttpServletRequestBuilderAdd all headers to the request. Values are always added.- Overrides:
- headersin class- AbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
- httpHeaders- the headers and values to add
 
- 
paramDescription 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 class- AbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
- name- the parameter name
- values- one or more values
 
- Supply a URL with a query to 
- 
paramsDescription copied from class:AbstractMockHttpServletRequestBuilderVariant ofAbstractMockHttpServletRequestBuilder.param(String, String...)with aMultiValueMap.- Overrides:
- paramsin class- AbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
- params- the parameters to add
 
- 
queryParamDescription 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 class- AbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
- name- the parameter name
- values- one or more values
 
- 
queryParamsDescription 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 class- AbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
- params- the parameters to add
 
- 
formFieldDescription copied from class:AbstractMockHttpServletRequestBuilderAppend the given value(s) to the given form field and also add them to the request parameters map.- Overrides:
- formFieldin class- AbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
- name- the field name
- values- one or more values
 
- 
formFieldsDescription copied from class:AbstractMockHttpServletRequestBuilderVariant ofAbstractMockHttpServletRequestBuilder.formField(String, String...)with aMultiValueMap.- Overrides:
- formFieldsin class- AbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
- formFields- the form fields to add
 
- 
cookieDescription copied from class:AbstractMockHttpServletRequestBuilderAdd the given cookies to the request. Cookies are always added.- Overrides:
- cookiein class- AbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
- cookies- the cookies to add
 
- 
localeDescription copied from class:AbstractMockHttpServletRequestBuilderAdd the specified locales as preferred request locales.- Overrides:
- localein class- AbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
- locales- the locales to add
- See Also:
 
- 
localeDescription copied from class:AbstractMockHttpServletRequestBuilderSet the locale of the request, overriding any previous locales.- Overrides:
- localein class- AbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
- locale- the locale, or- nullto reset it
- See Also:
 
- 
requestAttrDescription copied from class:AbstractMockHttpServletRequestBuilderSet a request attribute.- Overrides:
- requestAttrin class- AbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
- name- the attribute name
- value- the attribute value
 
- 
sessionAttrDescription copied from class:AbstractMockHttpServletRequestBuilderSet a session attribute.- Overrides:
- sessionAttrin class- AbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
- name- the session attribute name
- value- the session attribute value
 
- 
sessionAttrsDescription copied from class:AbstractMockHttpServletRequestBuilderSet session attributes.- Overrides:
- sessionAttrsin class- AbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
- sessionAttributes- the session attributes
 
- 
flashAttrDescription copied from class:AbstractMockHttpServletRequestBuilderSet an "input" flash attribute.- Overrides:
- flashAttrin class- AbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
- name- the flash attribute name
- value- the flash attribute value
 
- 
flashAttrsDescription copied from class:AbstractMockHttpServletRequestBuilderSet flash attributes.- Overrides:
- flashAttrsin class- AbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
- flashAttributes- the flash attributes
 
- 
sessionDescription 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 class- AbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
- session- the HTTP session
 
- 
principalDescription copied from class:AbstractMockHttpServletRequestBuilderSet the principal of the request.- Overrides:
- principalin class- AbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
- principal- the principal
 
- 
remoteAddressDescription copied from class:AbstractMockHttpServletRequestBuilderSet the remote address of the request.- Overrides:
- remoteAddressin class- AbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
- remoteAddress- the remote address (IP)
 
- 
withDescription 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 interface- ConfigurableSmartRequestBuilder<MockHttpServletRequestBuilder>
- Overrides:
- within class- AbstractMockHttpServletRequestBuilder<MockHttpServletRequestBuilder>
- Parameters:
- postProcessor- a post-processor to add
 
 
-