Class SpringTestWebUtils
- java.lang.Object
-
- io.microsphere.spring.test.util.SpringTestWebUtils
-
public abstract class SpringTestWebUtils extends java.lang.ObjectWeb Utilities class for Spring Test- Since:
- 1.0.0
- Author:
- Mercy
- See Also:
NativeWebRequest,MockHttpServletRequest
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringPATH_ATTRIBUTE
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidclearAttributes(org.springframework.web.context.request.NativeWebRequest request)Clears all attributes from the request scope of the givenNativeWebRequest.static voidclearAttributes(org.springframework.web.context.request.NativeWebRequest request, int scope)Clears all attributes from the specified scope of the givenNativeWebRequest.static org.springframework.web.context.request.NativeWebRequestcreatePreFightRequest()Creates a new instance ofNativeWebRequestfor CORS pre-flight requests.static org.springframework.web.context.request.NativeWebRequestcreateWebRequest()Creates a new instance ofNativeWebRequestwith default settings.static org.springframework.web.context.request.NativeWebRequestcreateWebRequest(java.lang.String requestURI)Creates a new instance ofNativeWebRequestwith the specified request URI.static org.springframework.web.context.request.NativeWebRequestcreateWebRequest(java.util.function.Consumer<org.springframework.mock.web.MockHttpServletRequest> requestBuilder)Creates a new instance ofNativeWebRequestwith custom settings provided by the givenConsumer.static org.springframework.web.context.request.NativeWebRequestcreateWebRequestWithHeaders(java.lang.Object... headers)Creates a new instance ofNativeWebRequestwith the specified request headers.static org.springframework.web.context.request.NativeWebRequestcreateWebRequestWithHeaders(java.util.Map<java.lang.String,java.lang.String> headers)Creates a new instance ofNativeWebRequestwith the specified request headers.static org.springframework.web.context.request.NativeWebRequestcreateWebRequestWithParams(java.lang.Object... params)Creates a new instance ofNativeWebRequestwith the specified request parameters.
-
-
-
Method Detail
-
createWebRequest
public static org.springframework.web.context.request.NativeWebRequest createWebRequest()
Creates a new instance ofNativeWebRequestwith 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(Consumer)
-
createWebRequest
public static org.springframework.web.context.request.NativeWebRequest createWebRequest(java.util.function.Consumer<org.springframework.mock.web.MockHttpServletRequest> requestBuilder)
Creates a new instance ofNativeWebRequestwith custom settings provided by the givenConsumer.This method allows for flexible configuration of the underlying
MockHttpServletRequestbefore creating theServletWebRequest. 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- aConsumerthat accepts and configures aMockHttpServletRequest- Returns:
- a new instance of
NativeWebRequestbased on the configured request - See Also:
MockHttpServletRequest,ServletWebRequest
-
createWebRequest
public static org.springframework.web.context.request.NativeWebRequest createWebRequest(java.lang.String requestURI)
Creates a new instance ofNativeWebRequestwith the specified request URI.This method configures the underlying
MockHttpServletRequestwith the given URI and sets thePATH_ATTRIBUTEto 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 theMockHttpServletRequest- Returns:
- a new instance of
NativeWebRequestconfigured with the given URI - See Also:
createWebRequest(Consumer),MockHttpServletRequest.setRequestURI(String),PATH_ATTRIBUTE
-
createWebRequestWithParams
public static org.springframework.web.context.request.NativeWebRequest createWebRequestWithParams(java.lang.Object... params)
Creates a new instance ofNativeWebRequestwith the specified request parameters.This method configures the underlying
MockHttpServletRequestwith the given parameters usingMapUtils.of(Object...). The parameters are passed as key-value pairs in the form ofkey1, 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
NativeWebRequestconfigured with the given parameters - See Also:
createWebRequest(Consumer),MapUtils.of(Object...)
-
createWebRequestWithHeaders
public static org.springframework.web.context.request.NativeWebRequest createWebRequestWithHeaders(java.lang.Object... headers)
Creates a new instance ofNativeWebRequestwith the specified request headers.This method configures the underlying
MockHttpServletRequestwith the given headers usingMapUtils.of(Object...). The headers are passed as key-value pairs in the form ofkey1, 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
NativeWebRequestconfigured with the given headers - See Also:
createWebRequest(Consumer),MapUtils.of(Object...)
-
createWebRequestWithHeaders
public static org.springframework.web.context.request.NativeWebRequest createWebRequestWithHeaders(java.util.Map<java.lang.String,java.lang.String> headers)
Creates a new instance ofNativeWebRequestwith the specified request headers.This method configures the underlying
MockHttpServletRequestwith the given headers using aMap. 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
NativeWebRequestconfigured with the given headers - See Also:
createWebRequest(Consumer),MockHttpServletRequest.addHeader(String, Object)
-
createPreFightRequest
public static org.springframework.web.context.request.NativeWebRequest createPreFightRequest()
Creates a new instance ofNativeWebRequestfor CORS pre-flight requests.This method configures the underlying
MockHttpServletRequestwith 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
NativeWebRequestconfigured for CORS pre-flight requests - See Also:
createWebRequest(Consumer),MockHttpServletRequest
-
clearAttributes
public static void clearAttributes(@Nonnull org.springframework.web.context.request.NativeWebRequest request)Clears all attributes from the request scope of the givenNativeWebRequest.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- theNativeWebRequestfrom which to clear attributes- See Also:
clearAttributes(NativeWebRequest, int),RequestAttributes.SCOPE_REQUEST
-
clearAttributes
public static void clearAttributes(@Nonnull org.springframework.web.context.request.NativeWebRequest request, int scope)Clears all attributes from the specified scope of the givenNativeWebRequest.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- theNativeWebRequestfrom which to clear attributesscope- the scope from which to clear attributes. UseSCOPE_REQUESTorSCOPE_SESSION- See Also:
clearAttributes(NativeWebRequest),RequestAttributes.SCOPE_REQUEST,RequestAttributes.SCOPE_SESSION
-
-