Class SpringTestWebUtils

java.lang.Object
io.microsphere.spring.test.util.SpringTestWebUtils

public abstract class SpringTestWebUtils extends Object
Web Utilities class for Spring Test
Since:
1.0.0
Author:
Mercy
See Also:
  • NativeWebRequest
  • MockHttpServletRequest
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    clearAttributes(org.springframework.web.context.request.NativeWebRequest request)
    Clears all attributes from the request scope of the given NativeWebRequest.
    static void
    clearAttributes(org.springframework.web.context.request.NativeWebRequest request, int scope)
    Clears all attributes from the specified scope of the given NativeWebRequest.
    static org.springframework.web.context.request.NativeWebRequest
    Creates a new instance of NativeWebRequest for CORS pre-flight requests.
    static org.springframework.web.context.request.NativeWebRequest
    Creates a new instance of NativeWebRequest with default settings.
    static org.springframework.web.context.request.NativeWebRequest
    Creates a new instance of NativeWebRequest with the specified request URI.
    static org.springframework.web.context.request.NativeWebRequest
    createWebRequest(Consumer<org.springframework.mock.web.MockHttpServletRequest> requestBuilder)
    Creates a new instance of NativeWebRequest with custom settings provided by the given Consumer.
    static org.springframework.web.context.request.NativeWebRequest
    Creates a new instance of NativeWebRequest with the specified request headers.
    static org.springframework.web.context.request.NativeWebRequest
    Creates a new instance of NativeWebRequest with the specified request headers.
    static org.springframework.web.context.request.NativeWebRequest
    Creates a new instance of NativeWebRequest with the specified request parameters.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • PATH_ATTRIBUTE

      public static final String PATH_ATTRIBUTE
  • Method Details

    • createWebRequest

      public static org.springframework.web.context.request.NativeWebRequest createWebRequest()
      Creates a new instance of NativeWebRequest with default settings.

      This method is equivalent to calling createWebRequest(Consumer) with an empty consumer.

      Example Usage

      
       NativeWebRequest request = SpringTestWebUtils.createWebRequest();
       
      Returns:
      a new instance of NativeWebRequest
      See Also:
    • createWebRequest

      public static org.springframework.web.context.request.NativeWebRequest createWebRequest(Consumer<org.springframework.mock.web.MockHttpServletRequest> requestBuilder)
      Creates a new instance of NativeWebRequest with custom settings provided by the given Consumer.

      This method allows for flexible configuration of the underlying MockHttpServletRequest before creating the ServletWebRequest. The consumer can modify any aspect of the request, such as headers, parameters, method, URI, etc.

      Example Usage

      
       NativeWebRequest request = SpringTestWebUtils.createWebRequest(req -> {
           req.setMethod("POST");
           req.setRequestURI("/api/users");
           req.addHeader("Content-Type", "application/json");
           req.setParameter("id", "123");
       });
       
      Parameters:
      requestBuilder - a Consumer that accepts and configures a MockHttpServletRequest
      Returns:
      a new instance of NativeWebRequest based on the configured request
      See Also:
      • MockHttpServletRequest
      • ServletWebRequest
    • createWebRequest

      public static org.springframework.web.context.request.NativeWebRequest createWebRequest(String requestURI)
      Creates a new instance of NativeWebRequest with the specified request URI.

      This method configures the underlying MockHttpServletRequest with the given URI and sets the PATH_ATTRIBUTE to the same value. It's a convenient way to create a web request for a specific endpoint.

      Example Usage

      
       NativeWebRequest request = SpringTestWebUtils.createWebRequest("/api/users/123");
       
      Parameters:
      requestURI - the request URI to set on the MockHttpServletRequest
      Returns:
      a new instance of NativeWebRequest configured with the given URI
      See Also:
    • createWebRequestWithParams

      public static org.springframework.web.context.request.NativeWebRequest createWebRequestWithParams(Object... params)
      Creates a new instance of NativeWebRequest with the specified request parameters.

      This method configures the underlying MockHttpServletRequest with the given parameters using MapUtils.of(Object...). The parameters are passed as key-value pairs in the form of key1, value1, key2, value2, ....

      Example Usage

      
       NativeWebRequest request = SpringTestWebUtils.createWebRequestWithParams(
           "name", "John Doe",
           "age", "30"
       );
       
      Parameters:
      params - the request parameters as key-value pairs
      Returns:
      a new instance of NativeWebRequest configured with the given parameters
      See Also:
    • createWebRequestWithHeaders

      public static org.springframework.web.context.request.NativeWebRequest createWebRequestWithHeaders(Object... headers)
      Creates a new instance of NativeWebRequest with the specified request headers.

      This method configures the underlying MockHttpServletRequest with the given headers using MapUtils.of(Object...). The headers are passed as key-value pairs in the form of key1, value1, key2, value2, ....

      Example Usage

      
       NativeWebRequest request = SpringTestWebUtils.createWebRequestWithHeaders(
           "Content-Type", "application/json",
           "Authorization", "Bearer token123"
       );
       
      Parameters:
      headers - the request headers as key-value pairs
      Returns:
      a new instance of NativeWebRequest configured with the given headers
      See Also:
    • createWebRequestWithHeaders

      public static org.springframework.web.context.request.NativeWebRequest createWebRequestWithHeaders(Map<String,String> headers)
      Creates a new instance of NativeWebRequest with the specified request headers.

      This method configures the underlying MockHttpServletRequest with the given headers using a Map. The headers are passed as a map of key-value pairs.

      Example Usage

      
       Map<String, String> headers = new HashMap<>();
       headers.put("Content-Type", "application/json");
       headers.put("Authorization", "Bearer token123");
       NativeWebRequest request = SpringTestWebUtils.createWebRequestWithHeaders(headers);
       
      Parameters:
      headers - the request headers as a map of key-value pairs
      Returns:
      a new instance of NativeWebRequest configured with the given headers
      See Also:
    • createPreFightRequest

      public static org.springframework.web.context.request.NativeWebRequest createPreFightRequest()
      Creates a new instance of NativeWebRequest for CORS pre-flight requests.

      This method configures the underlying MockHttpServletRequest with the OPTIONS method and adds the following headers:

      • :METHOD: - set to "OPTIONS"
      • Origin - set to "*"
      • Access-Control-Request-Method - set to "*"

      Example Usage

      
       NativeWebRequest preFlightRequest = SpringTestWebUtils.createPreFightRequest();
       
      Returns:
      a new instance of NativeWebRequest configured for CORS pre-flight requests
      See Also:
    • clearAttributes

      public static void clearAttributes(@Nonnull org.springframework.web.context.request.NativeWebRequest request)
      Clears all attributes from the request scope of the given NativeWebRequest.

      This method removes all attributes currently set in the request scope. It's useful for cleaning up request state between tests or operations.

      Example Usage

      
       NativeWebRequest request = SpringTestWebUtils.createWebRequest();
       request.setAttribute("key1", "value1", RequestAttributes.SCOPE_REQUEST);
       request.setAttribute("key2", "value2", RequestAttributes.SCOPE_REQUEST);
      
       // Clear all request-scoped attributes
       SpringTestWebUtils.clearAttributes(request);
       
      Parameters:
      request - the NativeWebRequest from which to clear attributes
      See Also:
    • clearAttributes

      public static void clearAttributes(@Nonnull org.springframework.web.context.request.NativeWebRequest request, int scope)
      Clears all attributes from the specified scope of the given NativeWebRequest.

      This method removes all attributes currently set in the specified scope (either request or session). It's useful for cleaning up request or session state between tests or operations.

      Example Usage

      
       NativeWebRequest request = SpringTestWebUtils.createWebRequest();
       request.setAttribute("key1", "value1", RequestAttributes.SCOPE_REQUEST);
       request.setAttribute("key2", "value2", RequestAttributes.SCOPE_SESSION);
      
       // Clear all request-scoped attributes
       SpringTestWebUtils.clearAttributes(request, RequestAttributes.SCOPE_REQUEST);
      
       // Clear all session-scoped attributes
       SpringTestWebUtils.clearAttributes(request, RequestAttributes.SCOPE_SESSION);
       
      Parameters:
      request - the NativeWebRequest from which to clear attributes
      scope - the scope from which to clear attributes. Use SCOPE_REQUEST or SCOPE_SESSION
      See Also: