Class MockMvcSupport
java.lang.Object
com.c4_soft.springaddons.security.oauth2.test.webmvc.MockMvcSupport
Just another wrapper for Spring MockMvc.
It would extend MockMvc if it was not final :-/
- auto sets "Accept" and "Content-Type" headers according
com.c4-soft.springaddons.test.web.default-media-typeandcom.c4-soft.springaddons.test.web.default-charsetto test properties, defaulted toapplication/jsonandutf-8 - serializes request body according to Content-type using registered message converters
- provides with shortcuts to issue requests in basic but most common cases (no fancy headers, cookies, etc): get, post, patch, put and delete methods
- wraps MockMvc
performand exposes request builder helpers for advanced cases (when you need to further customizeMockHttpServletRequestBuilderwith cookies or additional headers for instance)
- Author:
- Jérôme Wacongne <ch4mp@c4-soft.com>
-
Constructor Summary
ConstructorsConstructorDescriptionMockMvcSupport(org.springframework.test.web.servlet.MockMvc mockMvc, SerializationHelper serializationHelper, MockMvcProperties mockMvcProperties, org.springframework.boot.autoconfigure.web.ServerProperties serverProperties, SpringAddonsOidcProperties addonsProperties) -
Method Summary
Modifier and TypeMethodDescriptionorg.springframework.test.web.servlet.ResultActionsShortcut to issue a DELETE request (no header)org.springframework.test.web.servlet.request.MockHttpServletRequestBuilderdeleteRequestBuilder(String urlTemplate, Object... uriVars) Factory for a DELETE request builder.<T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilderfeed(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder request, T payload, org.springframework.http.MediaType mediaType, Charset charset) Adds serialized payload to request content.org.springframework.test.web.servlet.ResultActionsShortcut to create a builder for a GET request with minimal headers and submit it (Accept header defaulted to what this helper was constructed with).org.springframework.test.web.servlet.ResultActionsShortcut to issue a GET request with minimal headers and submit it.org.springframework.web.servlet.DispatcherServletorg.springframework.test.web.servlet.request.MockHttpServletRequestBuildergetRequestBuilder(String urlTemplate, Object... uriVars) Factory providing with a request builder to issue a GET request (with Accept header defaulted to what this helper is constructed with).org.springframework.test.web.servlet.request.MockHttpServletRequestBuildergetRequestBuilder(org.springframework.http.MediaType accept, String urlTemplate, Object... uriVars) Factory providing with a request builder to issue a GET request (with Accept header).org.springframework.test.web.servlet.ResultActionsShortcut to issue a HEAD request (no header)org.springframework.test.web.servlet.request.MockHttpServletRequestBuilderheadRequestBuilder(String urlTemplate, Object... uriVars) Factory for a HEAD request builder.org.springframework.test.web.servlet.ResultActionsShortcut to issue an OPTION request with default media-type as Accept headerorg.springframework.test.web.servlet.ResultActionsShortcut to issue an OPTION request with Accept headerorg.springframework.test.web.servlet.request.MockHttpServletRequestBuilderoptionRequestBuilder(String urlTemplate, Object... uriVars) Factory for an OPTION request initialized with an Accept header set to default media-type.org.springframework.test.web.servlet.request.MockHttpServletRequestBuilderoptionRequestBuilder(org.springframework.http.MediaType accept, String urlTemplate, Object... uriVars) Factory for an OPTION request initialized with an Accept header.<T> org.springframework.test.web.servlet.ResultActionsShortcut to issue a patch request with Content-type header and a body (using default media-type).<T> org.springframework.test.web.servlet.ResultActionspatch(T payload, org.springframework.http.MediaType contentType, String urlTemplate, Object... uriVars) Shortcut to issue a patch request with Content-type header and a body.<T> org.springframework.test.web.servlet.ResultActionspatch(T payload, org.springframework.http.MediaType contentType, Charset charset, String urlTemplate, Object... uriVars) Shortcut to issue a patch request with Content-type header and a body.<T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilderpatchRequestBuilder(T payload, String urlTemplate, Object... uriVars) Factory for a patch request builder (with Content-type set to default media-type).<T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilderpatchRequestBuilder(T payload, org.springframework.http.MediaType contentType, String urlTemplate, Object... uriVars) Factory for a patch request builder (with Content-type already set).<T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilderpatchRequestBuilder(T payload, org.springframework.http.MediaType contentType, Charset charset, String urlTemplate, Object... uriVars) Factory for a patch request builder (with Content-type already set).org.springframework.test.web.servlet.ResultActionsperform(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder requestBuilder) To be called with fully configured request builder (wraps MockMvcperform).<T> org.springframework.test.web.servlet.ResultActionsShortcut to issue a POST request with provided payload as body, using default media-type for serialization (and Content-type header).<T> org.springframework.test.web.servlet.ResultActionspost(T payload, org.springframework.http.MediaType contentType, Charset charset, org.springframework.http.MediaType accept, String urlTemplate, Object... uriVars) Shortcut to issue a POST request with provided payload as body, using given media-type for serialization (and Content-type header).<T> org.springframework.test.web.servlet.ResultActionspost(T payload, org.springframework.http.MediaType contentType, org.springframework.http.MediaType accept, String urlTemplate, Object... uriVars) Shortcut to issue a POST request with provided payload as body, using given media-type for serialization (and Content-type header).<T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilderpostRequestBuilder(T payload, String urlTemplate, Object... uriVars) Factory for a POST request builder.<T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilderpostRequestBuilder(T payload, org.springframework.http.MediaType contentType, Charset charset, org.springframework.http.MediaType accept, String urlTemplate, Object... uriVars) Factory for a POST request builder containing a body set to payload serialized in given media type (with adequate Content-type header).<T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilderpostRequestBuilder(T payload, org.springframework.http.MediaType contentType, org.springframework.http.MediaType accept, String urlTemplate, Object... uriVars) Factory for a POST request builder containing a body set to payload serialized in given media type (with adequate Content-type header).<T> org.springframework.test.web.servlet.ResultActionsShortcut to issue a PUT request (with default media-type as Content-type).<T> org.springframework.test.web.servlet.ResultActionsput(T payload, org.springframework.http.MediaType contentType, String urlTemplate, Object... uriVars) Shortcut to issue a PUT request.<T> org.springframework.test.web.servlet.ResultActionsput(T payload, org.springframework.http.MediaType contentType, String charset, String urlTemplate, Object... uriVars) Shortcut to issue a PUT request.<T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilderputRequestBuilder(T payload, String urlTemplate, Object... uriVars) Factory for a POST request builder containing a body.<T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilderputRequestBuilder(T payload, org.springframework.http.MediaType contentType, String urlTemplate, Object... uriVars) Factory for a POST request builder containing a body.<T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilderputRequestBuilder(T payload, org.springframework.http.MediaType contentType, Charset charset, String urlTemplate, Object... uriVars) Factory for a POST request builder containing a body.org.springframework.test.web.servlet.request.MockHttpServletRequestBuilderrequestBuilder(Optional<org.springframework.http.MediaType> accept, Optional<Charset> charset, org.springframework.http.HttpMethod method, String urlTemplate, Object... uriVars) Factory for a genericMockHttpServletRequestBuilderwith relevant "Accept" and "Content-Type" headers.setCharset(Charset charset) setCsrf(boolean isCsrf) setMediaType(org.springframework.http.MediaType mediaType) setSecure(boolean isSecure) with(org.springframework.test.web.servlet.request.RequestPostProcessor postProcessor)
-
Constructor Details
-
MockMvcSupport
public MockMvcSupport(org.springframework.test.web.servlet.MockMvc mockMvc, SerializationHelper serializationHelper, MockMvcProperties mockMvcProperties, org.springframework.boot.autoconfigure.web.ServerProperties serverProperties, SpringAddonsOidcProperties addonsProperties) - Parameters:
mockMvc- wrapped Spring MVC testing helperserializationHelper- used to serialize payloads to requestedContent-typeusing Spring registered message convertersmockMvcProperties- default values for media-type, charset and https usage
-
-
Method Details
-
setSecure
- Parameters:
isSecure- if true, requests are sent with https instead of http- Returns:
-
setCsrf
- Parameters:
isCsrf- should MockMvcRequests be issued with CSRF- Returns:
-
setMediaType
- Parameters:
mediaType- override configured default media-type- Returns:
-
setCharset
- Parameters:
charset- override configured default charset- Returns:
-
requestBuilder
public org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder requestBuilder(Optional<org.springframework.http.MediaType> accept, Optional<Charset> charset, org.springframework.http.HttpMethod method, String urlTemplate, Object... uriVars) Factory for a genericMockHttpServletRequestBuilderwith relevant "Accept" and "Content-Type" headers. You might prefer to usegetRequestBuilderor alike which go further with request pre-configuration or evenget,post(Object, String, Object...)and so on which issue simple requests in one step.- Parameters:
accept- should be non-empty when issuing response with body (GET, POST, OPTION), none otherwisecharset- char-set to be used for serialized payloadsmethod- whatever HTTP verb you needurlTemplate- end-point to be requesteduriVars- end-point template placeholders values- Returns:
- a request builder with minimal info you can tweak further: add headers, cookies, etc.
-
perform
public org.springframework.test.web.servlet.ResultActions perform(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder requestBuilder) To be called with fully configured request builder (wraps MockMvcperform).- Parameters:
requestBuilder- fully configured request- Returns:
- API answer to be tested
-
getRequestBuilder
public org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder getRequestBuilder(org.springframework.http.MediaType accept, String urlTemplate, Object... uriVars) Factory providing with a request builder to issue a GET request (with Accept header).- Parameters:
accept- determines request Accept header (and response body format)urlTemplate- API end-point to calluriVars- values to feed URL template placeholders- Returns:
- a request builder to be further configured (additional headers, cookies, etc.)
-
getRequestBuilder
public org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder getRequestBuilder(String urlTemplate, Object... uriVars) Factory providing with a request builder to issue a GET request (with Accept header defaulted to what this helper is constructed with).- Parameters:
urlTemplate- API end-point to calluriVars- values to feed URL template placeholders- Returns:
- a request builder to be further configured (additional headers, cookies, etc.)
-
get
public org.springframework.test.web.servlet.ResultActions get(org.springframework.http.MediaType accept, String urlTemplate, Object... uriVars) Shortcut to issue a GET request with minimal headers and submit it.- Parameters:
accept- determines request Accept header (and response body format)urlTemplate- API endpoint to be requesteduriVars- values to replace endpoint placeholders with- Returns:
- API response to test
-
get
public org.springframework.test.web.servlet.ResultActions get(String urlTemplate, Object... uriVars) Shortcut to create a builder for a GET request with minimal headers and submit it (Accept header defaulted to what this helper was constructed with).- Parameters:
urlTemplate- API endpoint to be requesteduriVars- values to replace endpoint placeholders with- Returns:
- API response to test
-
postRequestBuilder
public <T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder postRequestBuilder(T payload, org.springframework.http.MediaType contentType, Charset charset, org.springframework.http.MediaType accept, String urlTemplate, Object... uriVars) Factory for a POST request builder containing a body set to payload serialized in given media type (with adequate Content-type header).- Type Parameters:
T- payload type- Parameters:
payload- to be serialized as body in contentType formatcontentType- format to be used for payload serializationcharset- char-set for request and responseaccept- how should the response body be serialized (if any)urlTemplate- API end-point to be requesteduriVars- values to replace end-point placeholders with- Returns:
- Request builder to further configure (cookies, additional headers, etc.)
-
postRequestBuilder
public <T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder postRequestBuilder(T payload, org.springframework.http.MediaType contentType, org.springframework.http.MediaType accept, String urlTemplate, Object... uriVars) Factory for a POST request builder containing a body set to payload serialized in given media type (with adequate Content-type header).- Type Parameters:
T- payload type- Parameters:
payload- to be serialized as body in contentType formatcontentType- format to be used for payload serializationaccept- how should the response body be serialized (if any)urlTemplate- API end-point to be requesteduriVars- values to replace end-point placeholders with- Returns:
- Request builder to further configure (cookies, additional headers, etc.)
-
postRequestBuilder
public <T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder postRequestBuilder(T payload, String urlTemplate, Object... uriVars) Factory for a POST request builder. Body is pre-set to payload. Both Content-type and Accept headers are set to default media-type.- Type Parameters:
T- payload type- Parameters:
payload- request bodyurlTemplate- API end-pointuriVars- values ofr URL template placeholders- Returns:
- Request builder to further configure (cookies, additional headers, etc.)
-
post
public <T> org.springframework.test.web.servlet.ResultActions post(T payload, org.springframework.http.MediaType contentType, Charset charset, org.springframework.http.MediaType accept, String urlTemplate, Object... uriVars) Shortcut to issue a POST request with provided payload as body, using given media-type for serialization (and Content-type header).- Type Parameters:
T- payload type- Parameters:
payload- POST request bodycontentType- media type used to serialize payload and set Content-type headeraccept- media-type to be set as Accept header (and response serialization)charset- char-set for request and responseurlTemplate- API end-point to be calleduriVars- values ofr URL template placeholders- Returns:
- API response to test
-
post
public <T> org.springframework.test.web.servlet.ResultActions post(T payload, org.springframework.http.MediaType contentType, org.springframework.http.MediaType accept, String urlTemplate, Object... uriVars) Shortcut to issue a POST request with provided payload as body, using given media-type for serialization (and Content-type header).- Type Parameters:
T- payload type- Parameters:
payload- POST request bodycontentType- media type used to serialize payload and set Content-type headeraccept- media-type to be set as Accept header (and response serialization)urlTemplate- API end-point to be calleduriVars- values ofr URL template placeholders- Returns:
- API response to test
-
post
public <T> org.springframework.test.web.servlet.ResultActions post(T payload, String urlTemplate, Object... uriVars) Shortcut to issue a POST request with provided payload as body, using default media-type for serialization (and Content-type header).- Type Parameters:
T- payload type- Parameters:
payload- POST request bodyurlTemplate- API end-point to be calleduriVars- values ofr URL template placeholders- Returns:
- API response to test
-
putRequestBuilder
public <T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder putRequestBuilder(T payload, org.springframework.http.MediaType contentType, Charset charset, String urlTemplate, Object... uriVars) Factory for a POST request builder containing a body.- Type Parameters:
T- payload type- Parameters:
payload- to be serialized as body in contentType formatcontentType- format to be used for payload serializationcharset- char-set for requesturlTemplate- API end-point to be requesteduriVars- values to replace end-point placeholders with- Returns:
- Request builder to further configure (cookies, additional headers, etc.)
-
putRequestBuilder
public <T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder putRequestBuilder(T payload, org.springframework.http.MediaType contentType, String urlTemplate, Object... uriVars) Factory for a POST request builder containing a body.- Type Parameters:
T- payload type- Parameters:
payload- to be serialized as body in contentType formatcontentType- format to be used for payload serializationurlTemplate- API end-point to be requesteduriVars- values to replace end-point placeholders with- Returns:
- Request builder to further configure (cookies, additional headers, etc.)
-
putRequestBuilder
public <T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder putRequestBuilder(T payload, String urlTemplate, Object... uriVars) Factory for a POST request builder containing a body. Default media-type is used for payload serialization (and Content-type header).- Type Parameters:
T- payload type- Parameters:
payload- to be serialized as body in contentType formaturlTemplate- API end-point to be requesteduriVars- values to replace end-point placeholders with- Returns:
- Request builder to further configure (cookies, additional headers, etc.)
-
put
public <T> org.springframework.test.web.servlet.ResultActions put(T payload, org.springframework.http.MediaType contentType, String charset, String urlTemplate, Object... uriVars) Shortcut to issue a PUT request.- Type Parameters:
T- payload type- Parameters:
payload- request bodycontentType- payload serialization media-typecharset- char-set for request and responseurlTemplate- API end-point to requesturiVars- values to be used in end-point URL placehoders- Returns:
- API response to be tested
-
put
public <T> org.springframework.test.web.servlet.ResultActions put(T payload, org.springframework.http.MediaType contentType, String urlTemplate, Object... uriVars) Shortcut to issue a PUT request.- Type Parameters:
T- payload type- Parameters:
payload- request bodycontentType- payload serialization media-typeurlTemplate- API end-point to requesturiVars- values to be used in end-point URL placehoders- Returns:
- API response to be tested
-
put
public <T> org.springframework.test.web.servlet.ResultActions put(T payload, String urlTemplate, Object... uriVars) Shortcut to issue a PUT request (with default media-type as Content-type).- Type Parameters:
T- payload type- Parameters:
payload- request bodyurlTemplate- API end-point to requesturiVars- values to be used in end-point URL placehoders- Returns:
- API response to be tested
-
patchRequestBuilder
public <T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder patchRequestBuilder(T payload, org.springframework.http.MediaType contentType, Charset charset, String urlTemplate, Object... uriVars) Factory for a patch request builder (with Content-type already set).- Type Parameters:
T- payload type- Parameters:
payload- request bodycharset- char-set to be used for serialized payloadscontentType- payload serialization formaturlTemplate- API end-pointuriVars- values for end-point placeholders- Returns:
- request builder to further configure (additional headers, cookies, etc.)
-
patchRequestBuilder
public <T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder patchRequestBuilder(T payload, org.springframework.http.MediaType contentType, String urlTemplate, Object... uriVars) Factory for a patch request builder (with Content-type already set).- Type Parameters:
T- payload type- Parameters:
payload- request bodycontentType- payload serialization formaturlTemplate- API end-pointuriVars- values for end-point placeholders- Returns:
- request builder to further configure (additional headers, cookies, etc.)
-
patchRequestBuilder
public <T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder patchRequestBuilder(T payload, String urlTemplate, Object... uriVars) Factory for a patch request builder (with Content-type set to default media-type).- Type Parameters:
T- payload type- Parameters:
payload- request bodyurlTemplate- API end-pointuriVars- values for end-point placeholders- Returns:
- request builder to further configure (additional headers, cookies, etc.)
-
patch
public <T> org.springframework.test.web.servlet.ResultActions patch(T payload, org.springframework.http.MediaType contentType, Charset charset, String urlTemplate, Object... uriVars) Shortcut to issue a patch request with Content-type header and a body.- Type Parameters:
T- payload type- Parameters:
payload- request bodycontentType- to be used for payload serializationcharset- to be used for payload serializationurlTemplate- end-point URLuriVars- values for end-point URL placeholders- Returns:
- API response to be tested
-
patch
public <T> org.springframework.test.web.servlet.ResultActions patch(T payload, org.springframework.http.MediaType contentType, String urlTemplate, Object... uriVars) Shortcut to issue a patch request with Content-type header and a body.- Type Parameters:
T- payload type- Parameters:
payload- request bodycontentType- to be used for payload serializationurlTemplate- end-point URLuriVars- values for end-point URL placeholders- Returns:
- API response to be tested
-
patch
public <T> org.springframework.test.web.servlet.ResultActions patch(T payload, String urlTemplate, Object... uriVars) Shortcut to issue a patch request with Content-type header and a body (using default media-type).- Type Parameters:
T- payload type- Parameters:
payload- request bodyurlTemplate- end-point URLuriVars- values for end-point URL placeholders- Returns:
- API response to be tested
-
deleteRequestBuilder
public org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder deleteRequestBuilder(String urlTemplate, Object... uriVars) Factory for a DELETE request builder.- Parameters:
urlTemplate- API end-pointuriVars- values for end-point URL placeholders- Returns:
- request builder to further configure (additional headers, cookies, etc.)
-
delete
public org.springframework.test.web.servlet.ResultActions delete(String urlTemplate, Object... uriVars) Shortcut to issue a DELETE request (no header)- Parameters:
urlTemplate- API end-pointuriVars- values for end-point URL placeholders- Returns:
- API response to be tested
-
headRequestBuilder
public org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder headRequestBuilder(String urlTemplate, Object... uriVars) Factory for a HEAD request builder.- Parameters:
urlTemplate- API end-pointuriVars- values for end-point URL placeholders- Returns:
- request builder to further configure (additional headers, cookies, etc.)
-
head
public org.springframework.test.web.servlet.ResultActions head(String urlTemplate, Object... uriVars) Shortcut to issue a HEAD request (no header)- Parameters:
urlTemplate- API end-pointuriVars- values for end-point URL placeholders- Returns:
- API response to be tested
-
optionRequestBuilder
public org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder optionRequestBuilder(org.springframework.http.MediaType accept, String urlTemplate, Object... uriVars) Factory for an OPTION request initialized with an Accept header.- Parameters:
accept- response body media-typeurlTemplate- API end-pointuriVars- values for end-point URL placeholders- Returns:
- request builder to be further configured (additional headers, cookies, etc.)
-
optionRequestBuilder
public org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder optionRequestBuilder(String urlTemplate, Object... uriVars) Factory for an OPTION request initialized with an Accept header set to default media-type.- Parameters:
urlTemplate- API end-pointuriVars- values for end-point URL placeholders- Returns:
- request builder to be further configured (additional headers, cookies, etc.)
-
option
public org.springframework.test.web.servlet.ResultActions option(org.springframework.http.MediaType accept, String urlTemplate, Object... uriVars) Shortcut to issue an OPTION request with Accept header- Parameters:
accept- response body media-typeurlTemplate- API end-pointuriVars- values for end-point URL placeholders- Returns:
- API response to be further configured
-
option
public org.springframework.test.web.servlet.ResultActions option(String urlTemplate, Object... uriVars) Shortcut to issue an OPTION request with default media-type as Accept header- Parameters:
urlTemplate- API end-pointuriVars- values for end-point URL placeholders- Returns:
- API response to be further configured
-
feed
public <T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder feed(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder request, T payload, org.springframework.http.MediaType mediaType, Charset charset) Adds serialized payload to request content. Rather low-level, consider using this classMockHttpServletRequestBuilderfactories instead (getRequestBuilder, postRequestBuilder, etc.)- Type Parameters:
T- payload type- Parameters:
request- builder you want to set body topayload- object to be serialized as bodymediaType- what format you want payload to be serialized to (corresponding HttpMessageConverter must be registered)charset- char-set to be used for payload serialization- Returns:
- the request with provided payload as content
-
getDispatcherServlet
public org.springframework.web.servlet.DispatcherServlet getDispatcherServlet() -
with
public MockMvcSupport with(org.springframework.test.web.servlet.request.RequestPostProcessor postProcessor) - Parameters:
postProcessor- request post-processor to be added to the list of those applied before request is performed- Returns:
- this
MockMvcSupport
-