org.springframework.test.web.servlet.request

Class MockHttpServletRequestBuilder

  • java.lang.Object
    • org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder
    • Method Detail

      • contextPath

        public MockHttpServletRequestBuilder contextPath(java.lang.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:
        HttpServletRequest.getContextPath()
      • servletPath

        public MockHttpServletRequestBuilder servletPath(java.lang.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:
        HttpServletRequest.getServletPath()
      • pathInfo

        public MockHttpServletRequestBuilder pathInfo(@Nullable
                                                      java.lang.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:
        HttpServletRequest.getPathInfo()
      • secure

        public MockHttpServletRequestBuilder 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 MockHttpServletRequestBuilder characterEncoding(java.lang.String encoding)
        Set the character encoding of the request.
        Parameters:
        encoding - the character encoding
      • content

        public MockHttpServletRequestBuilder content(java.lang.String content)
        Set the request body as a UTF-8 String.
        Parameters:
        content - the body content
      • contentType

        public MockHttpServletRequestBuilder contentType(java.lang.String contentType)
        Set the 'Content-Type' header of the request.
        Parameters:
        contentType - the content type
        Since:
        4.1.2
      • accept

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

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

        public MockHttpServletRequestBuilder param(java.lang.String name,
                                                   java.lang.String... values)
        Add a request parameter to the MockHttpServletRequest.

        If called more than once, new values get added to existing ones.

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

        public MockHttpServletRequestBuilder params(MultiValueMap<java.lang.String,java.lang.String> params)
        Add a map of request parameters to the MockHttpServletRequest, for example when testing a form submission.

        If called more than once, new values get added to existing ones.

        Parameters:
        params - the parameters to add
        Since:
        4.2.4
      • cookie

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

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

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

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

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

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

        public MockHttpServletRequestBuilder flashAttrs(java.util.Map<java.lang.String,java.lang.Object> flashAttributes)
        Set flash attributes.
        Parameters:
        flashAttributes - the flash attributes
      • principal

        public MockHttpServletRequestBuilder principal(java.security.Principal principal)
        Set the principal of the request.
        Parameters:
        principal - the principal
      • isMergeEnabled

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

        public java.lang.Object merge(@Nullable
                                      java.lang.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