类 MockHttpServletRequestBuilder

java.lang.Object
cn.taketoday.test.web.servlet.request.MockHttpServletRequestBuilder
所有已实现的接口:
cn.taketoday.beans.Mergeable, ConfigurableSmartRequestBuilder<MockHttpServletRequestBuilder>, RequestBuilder, SmartRequestBuilder
直接已知子类:
MockMultipartHttpServletRequestBuilder

public class MockHttpServletRequestBuilder extends Object implements ConfigurableSmartRequestBuilder<MockHttpServletRequestBuilder>, cn.taketoday.beans.Mergeable
Default builder for 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.

从以下版本开始:
4.0
作者:
Rossen Stoyanchev, Juergen Hoeller, Arjen Poutsma, Sam Brannen, Kamill Sokol
  • 字段详细资料

    • method

      private final String method
    • url

      private final URI url
    • contextPath

      private String contextPath
    • servletPath

      private String servletPath
    • pathInfo

      @Nullable private String pathInfo
    • secure

      @Nullable private Boolean secure
    • principal

      @Nullable private Principal principal
    • remoteAddress

      @Nullable private String remoteAddress
    • session

      @Nullable private MockHttpSession session
    • characterEncoding

      @Nullable private String characterEncoding
    • content

      @Nullable private byte[] content
    • contentType

      @Nullable private String contentType
    • headers

      private final cn.taketoday.util.MultiValueMap<String,Object> headers
    • parameters

      private final cn.taketoday.util.MultiValueMap<String,String> parameters
    • queryParams

      private final cn.taketoday.util.MultiValueMap<String,String> queryParams
    • cookies

      private final List<jakarta.servlet.http.Cookie> cookies
    • locales

      private final List<Locale> locales
    • requestAttributes

      private final Map<String,Object> requestAttributes
    • sessionAttributes

      private final Map<String,Object> sessionAttributes
    • flashAttributes

      private final Map<String,Object> flashAttributes
    • postProcessors

      private final List<RequestPostProcessor> postProcessors
  • 构造器详细资料

    • MockHttpServletRequestBuilder

      MockHttpServletRequestBuilder(cn.taketoday.http.HttpMethod httpMethod, String url, Object... vars)
      Package private constructor. To get an instance, use static factory methods in MockMvcRequestBuilders.

      Although this class cannot be extended, additional ways to initialize the MockHttpServletRequest can be plugged in via with(RequestPostProcessor).

      参数:
      httpMethod - the HTTP method (GET, POST, etc)
      url - a URL template; the resulting URL will be encoded
      vars - zero or more URI variables
    • MockHttpServletRequestBuilder

      MockHttpServletRequestBuilder(cn.taketoday.http.HttpMethod httpMethod, URI url)
      参数:
      httpMethod - the HTTP method (GET, POST, etc)
      url - the URL
    • MockHttpServletRequestBuilder

      MockHttpServletRequestBuilder(String httpMethod, URI url)
      Alternative constructor for custom HTTP methods.
      参数:
      httpMethod - the HTTP method (GET, POST, etc)
      url - the URL
  • 方法详细资料

    • initUri

      private static URI initUri(String url, Object[] vars)
    • contextPath

      public MockHttpServletRequestBuilder 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 "/".

      另请参阅:
      • HttpServletRequest.getContextPath()
    • servletPath

      public MockHttpServletRequestBuilder 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 "/".

      另请参阅:
      • HttpServletRequest.getServletPath()
    • pathInfo

      public MockHttpServletRequestBuilder 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.

      另请参阅:
      • HttpServletRequest.getPathInfo()
    • secure

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

      public MockHttpServletRequestBuilder characterEncoding(Charset encoding)
      Set the character encoding of the request.
      参数:
      encoding - the character encoding
      另请参阅:
    • characterEncoding

      public MockHttpServletRequestBuilder characterEncoding(String encoding)
      Set the character encoding of the request.
      参数:
      encoding - the character encoding
    • content

      public MockHttpServletRequestBuilder 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.

      参数:
      content - the body content
    • content

      public MockHttpServletRequestBuilder 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.

      参数:
      content - the body content
    • contentType

      public MockHttpServletRequestBuilder contentType(cn.taketoday.http.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.

      参数:
      contentType - the content type
    • contentType

      public MockHttpServletRequestBuilder 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 - the content type
    • accept

      public MockHttpServletRequestBuilder accept(cn.taketoday.http.MediaType... mediaTypes)
      Set the 'Accept' header to the given media type(s).
      参数:
      mediaTypes - one or more media types
    • accept

      public MockHttpServletRequestBuilder accept(String... mediaTypes)
      Set the Accept header using raw String values, possibly not even well-formed (for testing purposes).
      参数:
      mediaTypes - one or more media types; internally joined as comma-separated String
    • header

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

      public MockHttpServletRequestBuilder headers(cn.taketoday.http.HttpHeaders httpHeaders)
      Add all headers to the request. Values are always added.
      参数:
      httpHeaders - the headers and values to add
    • param

      public MockHttpServletRequestBuilder 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:

      参数:
      name - the parameter name
      values - one or more values
    • params

      public MockHttpServletRequestBuilder params(cn.taketoday.util.MultiValueMap<String,String> params)
      Variant of param(String, String...) with a MultiValueMap.
      参数:
      params - the parameters to add
      从以下版本开始:
      4.0.4
    • queryParam

      public MockHttpServletRequestBuilder 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.
      参数:
      name - the parameter name
      values - one or more values
      从以下版本开始:
      4.0
    • queryParams

      public MockHttpServletRequestBuilder queryParams(cn.taketoday.util.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.
      参数:
      params - the parameters to add
      从以下版本开始:
      4.0
    • cookie

      public MockHttpServletRequestBuilder cookie(jakarta.servlet.http.Cookie... cookies)
      Add the given cookies to the request. Cookies are always added.
      参数:
      cookies - the cookies to add
    • locale

      public MockHttpServletRequestBuilder locale(Locale... locales)
      Add the specified locales as preferred request locales.
      参数:
      locales - the locales to add
      另请参阅:
    • locale

      public MockHttpServletRequestBuilder locale(@Nullable Locale locale)
      Set the locale of the request, overriding any previous locales.
      参数:
      locale - the locale, or null to reset it
      另请参阅:
    • requestAttr

      public MockHttpServletRequestBuilder requestAttr(String name, Object value)
      Set a request attribute.
      参数:
      name - the attribute name
      value - the attribute value
    • sessionAttr

      public MockHttpServletRequestBuilder sessionAttr(String name, Object value)
      Set a session attribute.
      参数:
      name - the session attribute name
      value - the session attribute value
    • sessionAttrs

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

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

      public MockHttpServletRequestBuilder flashAttrs(Map<String,Object> flashAttributes)
      Set flash attributes.
      参数:
      flashAttributes - the flash attributes
    • 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.

      参数:
      session - the HTTP session
    • principal

      public MockHttpServletRequestBuilder principal(Principal principal)
      Set the principal of the request.
      参数:
      principal - the principal
    • remoteAddress

      public MockHttpServletRequestBuilder remoteAddress(String remoteAddress)
      Set the remote address of the request.
      参数:
      remoteAddress - the remote address (IP)
    • with

      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.
      指定者:
      with 在接口中 ConfigurableSmartRequestBuilder<MockHttpServletRequestBuilder>
      参数:
      postProcessor - a post-processor to add
    • isMergeEnabled

      public boolean isMergeEnabled()
      指定者:
      isMergeEnabled 在接口中 cn.taketoday.beans.Mergeable
      返回:
      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.
      指定者:
      merge 在接口中 cn.taketoday.beans.Mergeable
      参数:
      parent - the parent RequestBuilder to inherit properties from
      返回:
      the result of the merge
    • containsCookie

      private boolean containsCookie(jakarta.servlet.http.Cookie cookie)
    • buildRequest

      public final MockHttpServletRequest buildRequest(jakarta.servlet.ServletContext servletContext)
      指定者:
      buildRequest 在接口中 RequestBuilder
      参数:
      servletContext - the ServletContext to use to create the request
      返回:
      the request
    • createServletRequest

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

      Can be overridden in subclasses.

    • updatePathRequestProperties

      private void updatePathRequestProperties(MockHttpServletRequest request, String requestUri)
      Update the contextPath, servletPath, and pathInfo of the request.
    • addRequestParams

      private void addRequestParams(MockHttpServletRequest request, cn.taketoday.util.MultiValueMap<String,String> map)
    • parseFormData

      private cn.taketoday.util.MultiValueMap<String,String> parseFormData(cn.taketoday.http.MediaType mediaType)
    • postProcessRequest

      public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request)
      从接口复制的说明: SmartRequestBuilder
      Apply request post-processing. Typically, that means invoking one or more cn.taketoday.test.web.servlet.request.RequestPostProcessors.
      指定者:
      postProcessRequest 在接口中 SmartRequestBuilder
      参数:
      request - the request to initialize
      返回:
      the request to use, either the one passed in or a wrapped one
    • addToMap

      private static void addToMap(Map<String,Object> map, String name, Object value)
    • addToMultiValueMap

      private static <T> void addToMultiValueMap(cn.taketoday.util.MultiValueMap<String,T> map, String name, T[] values)