Class AbstractMockHttpServletRequestBuilder<B extends AbstractMockHttpServletRequestBuilder<B>>

java.lang.Object
org.springframework.test.web.servlet.request.AbstractMockHttpServletRequestBuilder<B>
Type Parameters:
B - a self reference to the builder type
All Implemented Interfaces:
Mergeable, ConfigurableSmartRequestBuilder<B>, RequestBuilder, SmartRequestBuilder
Direct Known Subclasses:
AbstractMockMultipartHttpServletRequestBuilder, MockHttpServletRequestBuilder, MockMvcTester.MockMvcRequestBuilder

public abstract class AbstractMockHttpServletRequestBuilder<B extends AbstractMockHttpServletRequestBuilder<B>> extends Object implements ConfigurableSmartRequestBuilder<B>, Mergeable
Base builder for MockHttpServletRequest required as input to perform requests in MockMvc.
Since:
6.2
Author:
Rossen Stoyanchev, Juergen Hoeller, Arjen Poutsma, Sam Brannen, Kamill Sokol
  • Constructor Details

    • AbstractMockHttpServletRequestBuilder

      protected AbstractMockHttpServletRequestBuilder(HttpMethod httpMethod)
      Create a new instance using the specified HttpMethod.
      Parameters:
      httpMethod - the HTTP method (GET, POST, etc.)
  • Method Details

    • self

      protected B self()
    • uri

      public B uri(URI uri)
      Specify the URI using an absolute, fully constructed URI.
    • uri

      public B uri(String uriTemplate, Object... uriVariables)
      Specify the URI for the request using a URI template and URI variables.
    • contextPath

      public B contextPath(String contextPath)
      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 "/".

      See Also:
    • servletPath

      public B servletPath(String servletPath)
      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 "/".

      See Also:
    • pathInfo

      public B pathInfo(@Nullable String pathInfo)
      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.

      See Also:
    • secure

      public B secure(boolean secure)
      Set the secure property of the ServletRequest indicating use of a secure channel, such as HTTPS.
      Parameters:
      secure - whether the request is using a secure channel
    • characterEncoding

      public B characterEncoding(Charset encoding)
      Set the character encoding of the request.
      Parameters:
      encoding - the character encoding
      Since:
      5.3.10
      See Also:
    • characterEncoding

      public B characterEncoding(String encoding)
      Set the character encoding of the request.
      Parameters:
      encoding - the character encoding
    • content

      public B content(byte[] content)
      Set the request body.

      If content is provided and contentType(MediaType) is set to application/x-www-form-urlencoded, the content will be parsed and used to populate the request parameters map.

      Parameters:
      content - the body content
    • content

      public B content(String content)
      Set the request body as a UTF-8 String.

      If content is provided and contentType(MediaType) is set to application/x-www-form-urlencoded, the content will be parsed and used to populate the request parameters map.

      Parameters:
      content - the body content
    • contentType

      public B contentType(MediaType contentType)
      Set the 'Content-Type' header of the request.

      If content is provided and contentType is set to application/x-www-form-urlencoded, the content will be parsed and used to populate the request parameters map.

      Parameters:
      contentType - the content type
    • contentType

      public B contentType(String contentType)
      Set 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
    • accept

      public B accept(MediaType... mediaTypes)
      Set the 'Accept' header to the given media type(s).
      Parameters:
      mediaTypes - one or more media types
    • accept

      public B accept(String... mediaTypes)
      Set the Accept header 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
    • header

      public B header(String name, Object... values)
      Add a header to the request. Values are always added.
      Parameters:
      name - the header name
      values - one or more header values
    • headers

      public B headers(HttpHeaders httpHeaders)
      Add all headers to the request. Values are always added.
      Parameters:
      httpHeaders - the headers and values to add
    • param

      public B param(String name, String... values)
      Add a request parameter to MockHttpServletRequest.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:

      Parameters:
      name - the parameter name
      values - one or more values
    • params

      public B params(MultiValueMap<String,String> params)
      Parameters:
      params - the parameters to add
      Since:
      4.2.4
    • queryParam

      public B queryParam(String name, String... values)
      Append to the query string and also add to the request parameters map. 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
    • queryParams

      public B queryParams(MultiValueMap<String,String> params)
      Append to the query string and also add to the request parameters map. 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
    • formField

      public B formField(String name, String... values)
      Append 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
    • formFields

      public B formFields(MultiValueMap<String,String> formFields)
      Parameters:
      formFields - the form fields to add
      Since:
      6.1.7
    • cookie

      public B cookie(Cookie... cookies)
      Add the given cookies to the request. Cookies are always added.
      Parameters:
      cookies - the cookies to add
    • locale

      public B locale(Locale... locales)
      Add the specified locales as preferred request locales.
      Parameters:
      locales - the locales to add
      Since:
      4.3.6
      See Also:
    • locale

      public B locale(@Nullable Locale locale)
      Set the locale of the request, overriding any previous locales.
      Parameters:
      locale - the locale, or null to reset it
      See Also:
    • requestAttr

      public B requestAttr(String name, Object value)
      Set a request attribute.
      Parameters:
      name - the attribute name
      value - the attribute value
    • sessionAttr

      public B sessionAttr(String name, Object value)
      Set a session attribute.
      Parameters:
      name - the session attribute name
      value - the session attribute value
    • sessionAttrs

      public B sessionAttrs(Map<String,Object> sessionAttributes)
      Set session attributes.
      Parameters:
      sessionAttributes - the session attributes
    • flashAttr

      public B flashAttr(String name, Object value)
      Set an "input" flash attribute.
      Parameters:
      name - the flash attribute name
      value - the flash attribute value
    • flashAttrs

      public B flashAttrs(Map<String,Object> flashAttributes)
      Set flash attributes.
      Parameters:
      flashAttributes - the flash attributes
    • session

      public B session(MockHttpSession session)
      Set 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
    • principal

      public B principal(Principal principal)
      Set the principal of the request.
      Parameters:
      principal - the principal
    • remoteAddress

      public B remoteAddress(String remoteAddress)
      Set the remote address of the request.
      Parameters:
      remoteAddress - the remote address (IP)
      Since:
      6.0.10
    • with

      public B with(RequestPostProcessor postProcessor)
      An extension point for further initialization of 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.
      Specified by:
      with in interface ConfigurableSmartRequestBuilder<B extends AbstractMockHttpServletRequestBuilder<B>>
      Parameters:
      postProcessor - a post-processor to add
    • isMergeEnabled

      public boolean isMergeEnabled()
      Is merging enabled for this particular instance?
      Specified by:
      isMergeEnabled in interface Mergeable
      Returns:
      always returns true.
    • merge

      public Object merge(@Nullable Object parent)
      Merges the properties of the "parent" RequestBuilder accepting values only if not already set in "this" instance.
      Specified by:
      merge in interface Mergeable
      Parameters:
      parent - the parent RequestBuilder to inherit properties from
      Returns:
      the result of the merge
    • buildRequest

      public final MockHttpServletRequest buildRequest(ServletContext servletContext)
      Specified by:
      buildRequest in interface RequestBuilder
      Parameters:
      servletContext - the ServletContext to use to create the request
      Returns:
      the request
    • createServletRequest

      protected MockHttpServletRequest createServletRequest(ServletContext servletContext)
      Create a new MockHttpServletRequest based on the supplied ServletContext.

      Can be overridden in subclasses.

    • postProcessRequest

      public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request)
      Description copied from interface: SmartRequestBuilder
      Apply request post-processing. Typically, that means invoking one or more org.springframework.test.web.servlet.request.RequestPostProcessors.
      Specified by:
      postProcessRequest in interface SmartRequestBuilder
      Parameters:
      request - the request to initialize
      Returns:
      the request to use, either the one passed in or a wrapped one