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&nbsp;AbstractMockHttpservletRequestBuilder(HttpMethod&nbsp;httpMethod)
      Create a new instance using the specified HttpMethod.
      Parameters:
      httpMethod - the HTTP method (GET, POsT, etc.)
  • Method Details

    • self

      protected&nbsp;B&nbsp;self()
    • uri

      public&nbsp;B&nbsp;uri(URI&nbsp;uri)
      specify the URI using an absolute, fully constructed URI.
    • uri

      public&nbsp;B&nbsp;uri(string&nbsp;uriTemplate, @Nullable Object...&nbsp;uriVariables)
      specify the URI for the request using a URI template and URI variables.
    • contextPath

      public&nbsp;B&nbsp;contextPath(string&nbsp;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:
      • HttpservletRequest.getContextPath()
    • servletPath

      public&nbsp;B&nbsp;servletPath(string&nbsp;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:
      • HttpservletRequest.getservletPath()
    • pathInfo

      public&nbsp;B&nbsp;pathInfo(@Nullable string&nbsp;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:
      • HttpservletRequest.getPathInfo()
    • secure

      public&nbsp;B&nbsp;secure(boolean&nbsp;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&nbsp;B&nbsp;characterEncoding(Charset&nbsp;encoding)
      set the character encoding of the request.
      Parameters:
      encoding - the character encoding
      since:
      5.3.10
      see Also:
    • characterEncoding

      public&nbsp;B&nbsp;characterEncoding(string&nbsp;encoding)
      set the character encoding of the request.
      Parameters:
      encoding - the character encoding
    • content

      public&nbsp;B&nbsp;content(byte[]&nbsp;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&nbsp;B&nbsp;content(string&nbsp;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&nbsp;B&nbsp;contentType(MediaType&nbsp;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&nbsp;B&nbsp;contentType(string&nbsp;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&nbsp;B&nbsp;accept(MediaType...&nbsp;mediaTypes)
      set the 'Accept' header to the given media type(s).
      Parameters:
      mediaTypes - one or more media types
    • accept

      public&nbsp;B&nbsp;accept(string...&nbsp;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
    • acceptCharset

      public&nbsp;B&nbsp;acceptCharset(Charset...&nbsp;acceptableCharsets)
      set the list of acceptable charsets, as specified by the Accept-Charset header.
      Parameters:
      acceptableCharsets - the acceptable charsets
      since:
      7.0
    • ifModifiedsince

      public&nbsp;B&nbsp;ifModifiedsince(ZonedDateTime&nbsp;ifModifiedsince)
      set the value of the If-Modified-since header.
      Parameters:
      ifModifiedsince - the new value of the header
      since:
      7.0
    • ifNoneMatch

      public&nbsp;B&nbsp;ifNoneMatch(string...&nbsp;ifNoneMatches)
      set the values of the If-None-Match header.
      Parameters:
      ifNoneMatches - the new value of the header
      since:
      7.0
    • header

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

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

      public&nbsp;B&nbsp;headers(Consumer<HttpHeaders>&nbsp;headersConsumer)
      Provides access to every header declared so far with the possibility to add, replace, or remove values.
      Parameters:
      headersConsumer - the consumer to provide access to
      Returns:
      this builder
      since:
      7.0
    • param

      public&nbsp;B&nbsp;param(string&nbsp;name, string...&nbsp;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&nbsp;B&nbsp;params(MultiValueMap<string,string>&nbsp;params)
      Parameters:
      params - the parameters to add
      since:
      4.2.4
    • queryParam

      public&nbsp;B&nbsp;queryParam(string&nbsp;name, string...&nbsp;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&nbsp;B&nbsp;queryParams(MultiValueMap<string,string>&nbsp;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&nbsp;B&nbsp;formField(string&nbsp;name, string...&nbsp;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&nbsp;B&nbsp;formFields(MultiValueMap<string,string>&nbsp;formFields)
      Parameters:
      formFields - the form fields to add
      since:
      6.1.7
    • cookie

      public&nbsp;B&nbsp;cookie(jakarta.servlet.http.Cookie...&nbsp;cookies)
      Add the given cookies to the request. Cookies are always added.
      Parameters:
      cookies - the cookies to add
    • locale

      public&nbsp;B&nbsp;locale(Locale...&nbsp;locales)
      Add the specified locales as preferred request locales.
      Parameters:
      locales - the locales to add
      since:
      4.3.6
      see Also:
    • locale

      public&nbsp;B&nbsp;locale(@Nullable Locale&nbsp;locale)
      set the locale of the request, overriding any previous locales.
      Parameters:
      locale - the locale, or null to reset it
      see Also:
    • apiVersion

      public&nbsp;B&nbsp;apiVersion(Object&nbsp;version)
      set an API version for the request. The version is inserted into the request by the configured ApiVersionInserter.
      Parameters:
      version - the API version of the request; this can be a string or some Object that can be formatted the inserter, e.g. through an ApiVersionFormatter.
      since:
      7.0
    • apiVersionInserter

      public&nbsp;B&nbsp;apiVersionInserter(@Nullable ApiVersionInserter&nbsp;versionInserter)
      Configure an ApiVersionInserter to abstract how an API version specified via apiVersion(Object) is inserted into the request. An inserter may typically be set once (more centrally) via ConfigurableMockMvcBuilder.defaultRequest(RequestBuilder), or ConfigurableMockMvcBuilder.apiVersionInserter(ApiVersionInserter).

      ApiVersionInserter exposes shortcut methods for several built-in inserter implementation types. see the class-level Javadoc of ApiVersionInserter for a list of choices.

      Parameters:
      versionInserter - the inserter to use
      since:
      7.0
    • requestAttr

      public&nbsp;B&nbsp;requestAttr(string&nbsp;name, Object&nbsp;value)
      set a request attribute.
      Parameters:
      name - the attribute name
      value - the attribute value
    • sessionAttr

      public&nbsp;B&nbsp;sessionAttr(string&nbsp;name, Object&nbsp;value)
      set a session attribute.
      Parameters:
      name - the session attribute name
      value - the session attribute value
    • sessionAttrs

      public&nbsp;B&nbsp;sessionAttrs(Map<string,Object>&nbsp;sessionAttributes)
      set session attributes.
      Parameters:
      sessionAttributes - the session attributes
    • flashAttr

      public&nbsp;B&nbsp;flashAttr(string&nbsp;name, Object&nbsp;value)
      set an "input" flash attribute.
      Parameters:
      name - the flash attribute name
      value - the flash attribute value
    • flashAttrs

      public&nbsp;B&nbsp;flashAttrs(Map<string,Object>&nbsp;flashAttributes)
      set flash attributes.
      Parameters:
      flashAttributes - the flash attributes
    • session

      public&nbsp;B&nbsp;session(MockHttpsession&nbsp;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&nbsp;B&nbsp;principal(Principal&nbsp;principal)
      set the principal of the request.
      Parameters:
      principal - the principal
    • remoteAddress

      public&nbsp;B&nbsp;remoteAddress(string&nbsp;remoteAddress)
      set the remote address of the request.
      Parameters:
      remoteAddress - the remote address (IP)
      since:
      6.0.10
    • with

      public&nbsp;B&nbsp;with(RequestPostProcessor&nbsp;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&nbsp;in interface&nbsp;ConfigurablesmartRequestBuilder<B extends AbstractMockHttpservletRequestBuilder<B>>
      Parameters:
      postProcessor - a post-processor to add
    • isMergeEnabled

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

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

      public final&nbsp;MockHttpservletRequest&nbsp;buildRequest(jakarta.servlet.servletContext&nbsp;servletContext)
      specified by:
      buildRequest&nbsp;in interface&nbsp;RequestBuilder
      Parameters:
      servletContext - the servletContext to use to create the request
      Returns:
      the request
    • createservletRequest

      protected&nbsp;MockHttpservletRequest&nbsp;createservletRequest(jakarta.servlet.servletContext&nbsp;servletContext)
      Create a new MockHttpservletRequest based on the supplied servletContext.

      Can be overridden in subclasses.

    • postProcessRequest

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