Class AbstractMockHttpServletRequestBuilder<B extends AbstractMockHttpServletRequestBuilder<B>>
- Type Parameters:
- B- a self reference to the builder type
- All Implemented Interfaces:
- Mergeable,- ConfigurableSmartRequestBuilder<B>,- RequestBuilder,- SmartRequestBuilder
- Direct Known Subclasses:
- MockHttpServletRequestBuilder,- MockMultipartHttpServletRequestBuilder,- MockMvcTester.MockMvcRequestBuilder
MockHttpServletRequest required as input to
 perform requests in MockMvc.- Since:
- 6.2
- Author:
- Rossen Stoyanchev, Juergen Hoeller, Arjen Poutsma, Sam Brannen, Kamill Sokol
- 
Constructor SummaryConstructorsModifierConstructorDescriptionprotectedAbstractMockHttpServletRequestBuilder(HttpMethod httpMethod) Create a new instance using the specifiedHttpMethod.
- 
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).final MockHttpServletRequestbuildRequest(ServletContext servletContext) Build aMockHttpServletRequest.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.protected MockHttpServletRequestcreateServletRequest(ServletContext servletContext) Create a newMockHttpServletRequestbased on the suppliedServletContext.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 offormField(String, String...)with aMultiValueMap.Add a header to the request.headers(HttpHeaders httpHeaders) Add all headers to the request.booleanIs merging enabled for this particular instance?Set the locale of the request, overriding any previous locales.Add the specified locales as preferred request locales.Merges the properties of the "parent" RequestBuilder accepting values only if not already set in "this" instance.Add a request parameter toMockHttpServletRequest.getParameterMap().params(MultiValueMap<String, String> params) Variant ofparam(String, String...)with aMultiValueMap.Specify the portion of the requestURI that represents the pathInfo.postProcessRequest(MockHttpServletRequest request) Apply request post-processing.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.protected Bself()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.
- 
Constructor Details- 
AbstractMockHttpServletRequestBuilderCreate a new instance using the specifiedHttpMethod.- Parameters:
- httpMethod- the HTTP method (GET, POST, etc.)
 
 
- 
- 
Method Details- 
self
- 
uriSpecify the URI using an absolute, fully constructedURI.
- 
uriSpecify the URI for the request using a URI template and URI variables.
- 
contextPathSpecify 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 "/". - See Also:
 
- 
servletPathSpecify 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 "/".- See Also:
 
- 
pathInfoSpecify 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. - See Also:
 
- 
secureSet the secure property of theServletRequestindicating use of a secure channel, such as HTTPS.- Parameters:
- secure- whether the request is using a secure channel
 
- 
characterEncodingSet the character encoding of the request.- Parameters:
- encoding- the character encoding
- Since:
- 5.3.10
- See Also:
 
- 
characterEncodingSet the character encoding of the request.- Parameters:
- encoding- the character encoding
 
- 
contentSet the request body.If content is provided and contentType(MediaType)is set toapplication/x-www-form-urlencoded, the content will be parsed and used to populate therequest parametersmap.- Parameters:
- content- the body content
 
- 
contentSet the request body as a UTF-8 String.If content is provided and contentType(MediaType)is set toapplication/x-www-form-urlencoded, the content will be parsed and used to populate therequest parametersmap.- Parameters:
- content- the body content
 
- 
contentTypeSet 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.- Parameters:
- contentType- the content type
 
- 
contentTypeSet the 'Content-Type' header of the request as a raw String value, possibly not even well-formed (for testing purposes).- Parameters:
- contentType- the content type
- Since:
- 4.1.2
 
- 
acceptSet the 'Accept' header to the given media type(s).- Parameters:
- mediaTypes- one or more media types
 
- 
acceptSet theAcceptheader using raw String values, possibly not even well-formed (for testing purposes).- Parameters:
- mediaTypes- one or more media types; internally joined as comma-separated String
 
- 
headerAdd a header to the request. Values are always added.- Parameters:
- name- the header name
- values- one or more header values
 
- 
headersAdd all headers to the request. Values are always added.- Parameters:
- httpHeaders- the headers and values to add
 
- 
paramAdd 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 queryParam(java.lang.String, java.lang.String...)orqueryParams.
- Provide contentwithcontentTypeapplication/x-www-form-urlencoded.
 - Parameters:
- name- the parameter name
- values- one or more values
 
- Supply a URL with a query to 
- 
paramsVariant ofparam(String, String...)with aMultiValueMap.- Parameters:
- params- the parameters to add
- Since:
- 4.2.4
 
- 
queryParamAppend 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.- Parameters:
- name- the parameter name
- values- one or more values
- Since:
- 5.2.2
 
- 
queryParamsAppend 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.- Parameters:
- params- the parameters to add
- Since:
- 5.2.2
 
- 
formFieldAppend the given value(s) to the given form field and also add them to the request parameters map.- Parameters:
- name- the field name
- values- one or more values
- Since:
- 6.1.7
 
- 
formFieldsVariant offormField(String, String...)with aMultiValueMap.- Parameters:
- formFields- the form fields to add
- Since:
- 6.1.7
 
- 
cookieAdd the given cookies to the request. Cookies are always added.- Parameters:
- cookies- the cookies to add
 
- 
localeAdd the specified locales as preferred request locales.- Parameters:
- locales- the locales to add
- Since:
- 4.3.6
- See Also:
 
- 
localeSet the locale of the request, overriding any previous locales.- Parameters:
- locale- the locale, or- nullto reset it
- See Also:
 
- 
requestAttrSet a request attribute.- Parameters:
- name- the attribute name
- value- the attribute value
 
- 
sessionAttrSet a session attribute.- Parameters:
- name- the session attribute name
- value- the session attribute value
 
- 
sessionAttrsSet session attributes.- Parameters:
- sessionAttributes- the session attributes
 
- 
flashAttrSet an "input" flash attribute.- Parameters:
- name- the flash attribute name
- value- the flash attribute value
 
- 
flashAttrsSet flash attributes.- Parameters:
- flashAttributes- the flash attributes
 
- 
sessionSet the HTTP session to use, possibly re-used across requests.Individual attributes provided via sessionAttr(String, Object)override the content of the session provided here.- Parameters:
- session- the HTTP session
 
- 
principalSet the principal of the request.- Parameters:
- principal- the principal
 
- 
remoteAddressSet the remote address of the request.- Parameters:
- remoteAddress- the remote address (IP)
- Since:
- 6.0.10
 
- 
withAn 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<B extends AbstractMockHttpServletRequestBuilder<B>>
- Parameters:
- postProcessor- a post-processor to add
 
- 
isMergeEnabledpublic boolean isMergeEnabled()Is merging enabled for this particular instance?- Specified by:
- isMergeEnabledin interface- Mergeable
- Returns:
- always returns true.
 
- 
mergeMerges the properties of the "parent" RequestBuilder accepting values only if not already set in "this" instance.
- 
buildRequestBuild aMockHttpServletRequest.- Specified by:
- buildRequestin interface- RequestBuilder
- Parameters:
- servletContext- the- ServletContextto use to create the request
- Returns:
- the request
 
- 
createServletRequestCreate a newMockHttpServletRequestbased on the suppliedServletContext.Can be overridden in subclasses. 
- 
postProcessRequestDescription copied from interface:SmartRequestBuilderApply request post-processing. Typically, that means invoking one or moreorg.springframework.test.web.servlet.request.RequestPostProcessors.- Specified by:
- postProcessRequestin interface- SmartRequestBuilder
- Parameters:
- request- the request to initialize
- Returns:
- the request to use, either the one passed in or a wrapped one
 
 
-