Class RestClient
- java.lang.Object
-
- ru.vyarus.dropwizard.guice.test.rest.RestClient
-
public class RestClient extends java.lang.ObjectREST client for test stubbed rest (StubRest).client()provides a raw client, configured with: - Random port - Requests logging (@StubRest(logRequests = true), disabled by default) - Enabled restricted headers and method workaround (for url connection, used by in-memory test container) - Set default timeouts to avoid infinite calls - Enabled multipart support (if available in classpath)target(String...)- shortcut for calling rest services with a relative urls (no server, port or rest prefix). Without default* configuration (see below).Shortcut rest call methods with response mapping: -
get(String, Class)-post(String, Object, Class)-put(String, Object, Class)-delete(String, Class)Putting null instead of result class implies void response. In this case, response status checked to be 200 or 204 (could be changed withdefaultOk(Integer...)).To verify response headers use generic request method (
request(String...)): for example, to request get response:Response res = rest.request("/path/").get().To simplify default shortcut methods usage, additional parameters like custom headers and query parameter are configured as defaults: -
defaultHeader(String, String)-defaultQueryParam(String, String)-defaultAccept(String...)-defaultOk(Integer...)used only to verify correct OK codes for void responses (for methods returning mapped a mapped result:get("/path/", null)) - fail if the response status was not specified in defaultOk (200, 204 by default). For methods returning a real result, status is not checked (result presence already means correct execution)By default, defaults are reset after each test. So defaults could be specified in test setup method (to apply the same for all tests in class) or just before method call (in method test directly). Automatic rest could be disabled with
@StubRest(autoReset = false).- Since:
- 20.02.2025
-
-
Constructor Summary
Constructors Constructor Description RestClient(GuiceyJerseyTest jerseyTest)Create client.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description javax.ws.rs.client.Clientclient()Returns the pre-configuredClientfor this test.RestClientdefaultAccept(java.lang.String... accept)Apply default Accept header for shortcut rest call methods.RestClientdefaultHeader(java.lang.String name, java.lang.String value)Apply a default header for shortcut rest call methods.RestClientdefaultOk(java.lang.Integer... codes)Set allowed response codes for void calls (by default, 200 and 204).RestClientdefaultQueryParam(java.lang.String name, java.lang.String value)Apply a default query param for shortcut rest call methods.<T> Tdelete(java.lang.String path, java.lang.Class<T> result)Simple DELETE call shortcut.<T> Tget(java.lang.String path, java.lang.Class<T> result)Simple GET call shortcut.java.net.URIgetBaseUri()Just in case, it is not required when using any rest call method.booleanhasDefaultAccepts()Could be used for verifications in tests to avoid defaults collide.booleanhasDefaultHeaders()Could be used for verifications in tests to avoid defaults collide.booleanhasDefaultQueryParams()Could be used for verifications in tests to avoid defaults collide.booleanisDefaultStatusChanged()Could be used for verifications in tests to avoid defaults collide.<T> Tpost(java.lang.String path, java.lang.Object body, java.lang.Class<T> result)Simple POST call shortcut.<T> Tpost(java.lang.String rootPath, javax.ws.rs.client.Entity<?> body, java.lang.Class<T> result)Same aspost(String, Object, Class), but accepts generic entity.<T> Tput(java.lang.String path, java.lang.Object body, java.lang.Class<T> result)Simple PUT call shortcut.<T> Tput(java.lang.String path, javax.ws.rs.client.Entity<?> body, java.lang.Class<T> result)Same asput(String, Object, Class), but accepts generic entity.javax.ws.rs.client.Invocation.Builderrequest(java.lang.String... paths)Create request for provided target with all defaults applied.RestClientreset()Reset configured defaults.javax.ws.rs.client.WebTargettarget(java.lang.String... paths)Creates a web target to be sent to the resource under testing.
-
-
-
Constructor Detail
-
RestClient
public RestClient(GuiceyJerseyTest jerseyTest)
Create client.- Parameters:
jerseyTest- jersey test instance
-
-
Method Detail
-
target
public javax.ws.rs.client.WebTarget target(java.lang.String... paths)
Creates a web target to be sent to the resource under testing. When multiple parameters provided, they are connected with "/", avoiding duplicate slash appearances so, for example, "app, path", "app/, /path" or any other variation would always lead to correct "app/path"). Essentially this is the same as usingWebTarget.path(String)multiple times (after initial target creation).Example:
.target("/smth/").request().buildGet().invoke()WARNING: any specified defaults do not affect this method!
This is a generic method. Provided shortcuts (like
get(String, Class)should simplify usage.- Parameters:
paths- one or more path parts (assumed to be joined with '/') - overall, relative path (from tested application base URI) this web target should point to- Returns:
- jersey web target object
-
client
public javax.ws.rs.client.Client client()
Returns the pre-configuredClientfor this test. For sending requests prefertarget(String...). Usetarget(String...)method to avoid specifying full target path.- Returns:
- the
JerseyTestconfiguredClient
-
getBaseUri
public java.net.URI getBaseUri()
Just in case, it is not required when using any rest call method.- Returns:
- root rest url
-
defaultHeader
public RestClient defaultHeader(java.lang.String name, java.lang.String value)
Apply a default header for shortcut rest call methods. Does not apply fortarget(String...)method.- Parameters:
name- header namevalue- header value- Returns:
- client itself for chained calls
-
defaultQueryParam
public RestClient defaultQueryParam(java.lang.String name, java.lang.String value)
Apply a default query param for shortcut rest call methods. Does not apply fortarget(String...)method.- Parameters:
name- parameter namevalue- parameter value- Returns:
- client itself for chained calls
-
defaultOk
public RestClient defaultOk(java.lang.Integer... codes)
Set allowed response codes for void calls (by default, 200 and 204). For example,get("/path/", null)would fail if response differs from specified. Note that status is not checked for responses, returning result (result presence already indicates correct execution).Does not apply for
target(String...)method.Override previous setting.
- Parameters:
codes- response codes allowed for void rest calls- Returns:
- client itself for chained calls
-
hasDefaultHeaders
public boolean hasDefaultHeaders()
Could be used for verifications in tests to avoid defaults collide.- Returns:
- true if default headers specified
-
hasDefaultQueryParams
public boolean hasDefaultQueryParams()
Could be used for verifications in tests to avoid defaults collide.- Returns:
- true if default query params specified
-
hasDefaultAccepts
public boolean hasDefaultAccepts()
Could be used for verifications in tests to avoid defaults collide.- Returns:
- true if default accepts specified
-
isDefaultStatusChanged
public boolean isDefaultStatusChanged()
Could be used for verifications in tests to avoid defaults collide.- Returns:
- true if default void statuses changed
-
defaultAccept
public RestClient defaultAccept(java.lang.String... accept)
Apply default Accept header for shortcut rest call methods. Does not apply fortarget(String...)method.- Parameters:
accept- accept values- Returns:
- client itself for chained calls
- See Also:
MediaType
-
request
public javax.ws.rs.client.Invocation.Builder request(java.lang.String... paths)
Create request for provided target with all defaults applied. Use to get the complete response object to validate response headers:Response res = request("/path/").get().- Parameters:
paths- target path, relative to rest root- Returns:
- request object, ready to be sent
-
get
public <T> T get(java.lang.String path, @Nullable java.lang.Class<T> result)Simple GET call shortcut. Provided path should include only the target rest path.To provide additional headers and query params see
defaultHeader(String, String)(defaultAccept(String...)) anddefaultQueryParam(String, String). For void responses (result class null) checks response status correctness (seedefaultOk(Integer...)).For response headers validation, use raw
Response res = request("/path/").get().- Type Parameters:
T- result type- Parameters:
path- target path, relative to rest rootresult- result type (when null, accepts any 200 or 204 responses)- Returns:
- mapped result object or null (if class not declared)
-
post
public <T> T post(java.lang.String path, @Nullable java.lang.Object body, @Nullable java.lang.Class<T> result)Simple POST call shortcut. Provided path should include only the target rest path. Body object assumed to be a json entity (would be serialized as json). For file sending use method with generic entitypost(String, javax.ws.rs.client.Entity, Class).To provide additional headers and query params see
defaultHeader(String, String)(defaultAccept(String...)) anddefaultQueryParam(String, String). For void responses (result class null) checks response status correctness (seedefaultOk(Integer...)).For response headers validation, use raw
Response res = request("/path/").post(Entity.json(body).- Type Parameters:
T- result type- Parameters:
path- target path, relative to rest rootbody- post body object (serialized as json)result- result type (when null, accepts any 200 or 204 responses)- Returns:
- mapped result object or null (if class not declared)
-
post
public <T> T post(java.lang.String rootPath, @Nullable javax.ws.rs.client.Entity<?> body, @Nullable java.lang.Class<T> result)Same aspost(String, Object, Class), but accepts generic entity. Useful for sending multipart requests.- Type Parameters:
T- result type- Parameters:
rootPath- target path, relative to rest rootbody- entity objectresult- result type (when null, accepts any 200 or 204 responses)- Returns:
- mapped result object or null (if class not declared)
-
put
public <T> T put(java.lang.String path, @Nullable java.lang.Object body, @Nullable java.lang.Class<T> result)Simple PUT call shortcut. Provided path should include only the target rest path. Body object assumed to be a json entity (would be serialized as json). For file sending use method with generic entityput(String, javax.ws.rs.client.Entity, Class).To provide additional headers and query params see
defaultHeader(String, String)(defaultAccept(String...)) anddefaultQueryParam(String, String). For void responses (result class null) checks response status correctness (seedefaultOk(Integer...)).For response headers validation, use raw
Response res = request("/path/").put(Entity.json(body)).- Type Parameters:
T- result type- Parameters:
path- target path, relative to rest rootbody- put body object (serialized as json)result- result type (when null, accepts any 200 or 204 responses)- Returns:
- mapped result object or null (if class not declared)
-
put
public <T> T put(java.lang.String path, javax.ws.rs.client.Entity<?> body, @Nullable java.lang.Class<T> result)Same asput(String, Object, Class), but accepts generic entity. Useful for sending multipart requests.- Type Parameters:
T- result type- Parameters:
path- target path, relative to rest rootbody- post body object (serialized as json)result- result type (when null, accepts any 200 or 204 responses)- Returns:
- mapped result object or null (if class not declared)
-
delete
public <T> T delete(java.lang.String path, @Nullable java.lang.Class<T> result)Simple DELETE call shortcut. Provided path should include only the target rest path.To provide additional headers and query params see
defaultHeader(String, String)(defaultAccept(String...)) anddefaultQueryParam(String, String). For void responses (result class null) checks response status correctness (seedefaultOk(Integer...)).For response headers validation, use raw
Response res = request("/path/").delete().- Type Parameters:
T- result type- Parameters:
path- target path, relative to rest rootresult- result type (when null, accepts any 200 or 204 responses)- Returns:
- mapped result object or null (if class not declared)
-
reset
public RestClient reset()
Reset configured defaults.- Returns:
- rest client itself for chained calls
-
-