Class ClientSupport
- All Implemented Interfaces:
AutoCloseable
JerseyClient support for direct web tests (complete dropwizard startup).
Client support maintains single JerseyClient instance. It may be used for calling any urls (not just
application). Class provides many utility methods for automatic construction of base context paths, so
tests could be completely independent of actual configuration.
Client customization is possible through custom
TestClientFactory implementation.
Dropwizard configurations for rest, app and admin contexts:
server.applicationContextPath- app context path ("/" by default, "/application" for simple server)server.adminContextPath- admin context path ("/" by default, "/admin" for simple server)server.rootPath- rest context path ("/" by default)
server.applicationContextPath + server.rootPath.
The class provides access for 4 clients (with the same api
TestClient):
ClientSupportitself is a client for application rootrestClient()specialized rest client (mapped rest path counted)appClient()client for the app context (context mapping path counted)adminClient()client for the admin context (context mapping path and port counted)
There is also a specialized externalClient(String, Object...) for custom clients for creating remote api
clients (with the same client api):
support.externalClient("http://localhost:8080/some/path").get("/some/resource").
There is a special class-based client constructor restClient(Class) for resources. This makes tests
type-safe as the target resource path is obtained directly from the class annotation. Also, such clients
could use resource class method calls for request configuration
(ResourceClient.method(ru.vyarus.dropwizard.guice.url.util.Caller)).
Note: defaults, declared on root ClientSupport class would be inherited
by sub clients (excluding external clients).
The root client is not much useful, so the assumed usage is to get the required client and use it for actual calls.
Class also provides base urls and related jersey targets (pure jersey api):
- Since:
- 04.05.2020
- See Also:
-
Field Summary
Fields inherited from class ru.vyarus.dropwizard.guice.test.client.builder.TestClientDefaults
defaults -
Constructor Summary
ConstructorsConstructorDescriptionClientSupport(io.dropwizard.testing.DropwizardTestSupport<?> support) Create a client.ClientSupport(io.dropwizard.testing.DropwizardTestSupport<?> support, @Nullable TestClientFactory factory) Create client with custom factory.ClientSupport(io.dropwizard.testing.DropwizardTestSupport<?> support, @Nullable TestClientFactory factory, @Nullable TestRequestConfig defaults) Create client with custom factory. -
Method Summary
Modifier and TypeMethodDescriptionTestClient<?>Construct a client for the admin context.Shortcut to be able to quickly build an apache-connector-based client.TestClient<?>Construct a client for the app context.For example, with the default configuration it would be "http://localhost:8081/".For example, with the default configuration it would be "http://localhost:8080/".Deprecated.For example, with the default configuration it would be "http://localhost:8080/".Root application path is, usually, not very interesting so consider usingbasePathApp(),basePathRest()orbasePathAdmin()instead.voidclose()TestClient<?>externalClient(String url, Object... args) Construct a client for external url.intorg.glassfish.jersey.client.JerseyClientSingle client instance maintained within test and method will always return the same instance.intgetPort()protected jakarta.ws.rs.client.WebTargetgetRoot()Subclasses could override this method: this is required because it is not always possible to provide the correct root target in the constructor (when this method is overridden root, provided in the constructor is null).TestClient<?>Construct a rest client.<K> ResourceClient<K>restClient(Class<K> resource) Construct a specialized rest client for the given resource class.jakarta.ws.rs.client.WebTargetCreate a jerseyWebTargetfor a path, relative to application root (e.g.jakarta.ws.rs.client.WebTargettargetAdmin(String path, Object... args) Shortcut forWebTargetcreation for the admin context path.jakarta.ws.rs.client.WebTargetShortcut forWebTargetcreation for the app context path.jakarta.ws.rs.client.WebTargettargetMain(String path, Object... args) Deprecated.usetargetApp(String, Object...)insteadjakarta.ws.rs.client.WebTargettargetRest(String path, Object... args) Shortcut forWebTargetcreation for the rest context path.Shortcut to be able to quickly build an apache-connector-based client.Methods inherited from class ru.vyarus.dropwizard.guice.test.client.TestClient
asRestClient, build, buildDelete, buildForm, buildGet, buildPatch, buildPost, buildPut, delete, delete, delete, get, get, get, getBaseUri, getEntity, patch, patch, patch, post, post, post, put, put, put, request, subClient, subClient, subClient, subResourceClient, toStringMethods inherited from class ru.vyarus.dropwizard.guice.test.client.builder.TestClientDefaults
defaultAccept, defaultAccept, defaultCacheControl, defaultCacheControl, defaultCookie, defaultCookie, defaultCookie, defaultDebug, defaultEncoding, defaultFormDateFormat, defaultFormDateFormatter, defaultFormDateTimeFormatter, defaultHeader, defaultHeader, defaultHeader, defaultHeader, defaultLanguage, defaultLanguage, defaultMatrixParam, defaultMatrixParam, defaultPathConfiguration, defaultPathParam, defaultPathParam, defaultProperty, defaultProperty, defaultQueryParam, defaultQueryParam, defaultRegister, defaultRegister, defaultRegister, defaultRequestConfiguration, getDefaults, hasDefaultAccepts, hasDefaultCacheControl, hasDefaultCookies, hasDefaultCustomConfigurators, hasDefaultEncodings, hasDefaultExtensions, hasDefaultFormDateFormatter, hasDefaultHeaders, hasDefaultLanguages, hasDefaultMatrixParams, hasDefaultPathParams, hasDefaultProperties, hasDefaultQueryParams, hasDefaults, isDebugEnabled, printDefaults, reset, self
-
Constructor Details
-
ClientSupport
public ClientSupport(io.dropwizard.testing.DropwizardTestSupport<?> support) Create a client.- Parameters:
support- dropwizard test support
-
ClientSupport
public ClientSupport(io.dropwizard.testing.DropwizardTestSupport<?> support, @Nullable TestClientFactory factory) Create client with custom factory.- Parameters:
support- dropwizard test supportfactory- custom client factory
-
ClientSupport
public ClientSupport(io.dropwizard.testing.DropwizardTestSupport<?> support, @Nullable TestClientFactory factory, @Nullable TestRequestConfig defaults) Create client with custom factory.- Parameters:
support- dropwizard test supportfactory- custom client factorydefaults- defaults for all clients (could be overridden by the client-specific defaults)
-
-
Method Details
-
getClient
public org.glassfish.jersey.client.JerseyClient getClient()Single client instance maintained within test and method will always return the same instance.- Returns:
- client instance
-
apacheClient
Shortcut to be able to quickly build an apache-connector-based client. The default urlconnector-based client is better for multipart calls (apache client has problems). But the apache client is better for PATCH calls because the default urlconnector will complain on java > 16.With this shortcut it would be possible to use both clients in one test.
Applied defaults are inherited.
- Returns:
- client based on apache connector
-
urlconnectorClient
Shortcut to be able to quickly build an apache-connector-based client. The default urlconnector-based client is better for multipart calls (apache client has problems). But the apache client is better for PATCH calls because the default urlconnector will complain on java > 16.With this shortcut it would be possible to use both clients in one test.
Applied defaults are inherited.
- Returns:
- client based on apache connector
-
getPort
public int getPort()- Returns:
- app context port
- Throws:
NullPointerException- for guicey test (when web not started)
-
getAdminPort
public int getAdminPort()- Returns:
- admin context port
- Throws:
NullPointerException- for guicey test (when web not started)
-
basePathRoot
Root application path is, usually, not very interesting so consider usingbasePathApp(),basePathRest()orbasePathAdmin()instead.- Returns:
- root application path (localhost + port)
- Throws:
NullPointerException- for guicey test (when web not started)
-
basePathMain
Deprecated.usebasePathApp()- Returns:
- base path for application context
-
basePathApp
For example, with the default configuration it would be "http://localhost:8080/". If "server.applicationContextPath" is changed to "/someth", then the method will return "http://localhost:8080/someth/".Returned path will always end with a slash.
- Returns:
- base path for app context
- Throws:
NullPointerException- for guicey test (when web not started)- See Also:
-
basePathAdmin
For example, with the default configuration it would be "http://localhost:8081/". For the "simple" server, it would be "http://localhost:8080/adminPath/". If "server.adminContextPath" is changed to "/adm", then the method will return "http://localhost:8081/adm/" for the default server and "http://localhost:8080/adm/" for "simple" server.Returned path will always end with a slash.
- Returns:
- base path for admin context
- Throws:
NullPointerException- for guicey test (when web not started)- See Also:
-
basePathRest
For example, with the default configuration it would be "http://localhost:8080/". If "server.rootPath" is changed to "/api", then the method will return "http://localhost:8080/api/". If the app context mapping changed from root, then the returned path will count it too (e.g. "http://localhost:8080/root/rest/", when "server.applicationContextPath" is "/root").Returned path will always end with a slash.
- Returns:
- base path for rest
- Throws:
NullPointerException- for guicey test (when web not started)- See Also:
-
target
Create a jerseyWebTargetfor a path, relative to application root (e.g. "http://localhost:8080/" + provided path)".It could be also used to request any external api, for example:
target("http://somewhere.com/api/smth/").request().buildGet().invoke().String format could be used to format the path:
.target("/smth/%s/other", 12).request().buildGet().invoke()NOTE: could be used with guicey-only tests (when web part not started) to call any external url.
IMPORTANT: it would not apply configured defaults (like
TestClientDefaults.defaultHeader(String, Object)). Use builders (TestClient.build(String, String, Object, Object...)) or shortcutsTestClient.get(String, Class, Object...)to cal with defaults.- Overrides:
targetin classTestClient<ClientSupport>- Parameters:
path- target path, relative to client root or absolute external path (could contain String.format() placeholders: %s)args- variables for path placeholders (String.format() arguments)- Returns:
- jersey web target object
- See Also:
-
targetMain
Deprecated.usetargetApp(String, Object...)insteadShortcut forWebTargetcreation for the application context path.- Parameters:
path- target path, relative to the admin context (could contain String.format() placeholders: %s)args- variables for path placeholders (String.format() arguments)- Returns:
- jersey web target object for the admin context
-
targetApp
Shortcut forWebTargetcreation for the app context path.Example:
.targetApp("/path").request().buildGet().invoke()would call "http://localhost:8080/path" (or, if root context mapping changed withserver.applicationContextPath = "root", "http://localhost:8080/root/path").String format could be used to format the path:
.targetApp("/smth/%s/other", 12).request().buildGet().invoke()IMPORTANT: it would not apply configured defaults (like
TestClientDefaults.defaultHeader(String, Object)). Use app clientappClient()to call with defaults.- Parameters:
path- target path, relative to the app context (could contain String.format() placeholders: %s)args- variables for path placeholders (String.format() arguments)- Returns:
- jersey web target object for the app context
- Throws:
NullPointerException- for guicey test (when web not started)- See Also:
-
targetAdmin
Shortcut forWebTargetcreation for the admin context path.Example:
.targetAdmin("/path").request().buildGet().invoke()would call "http://localhost:8081/path" (or, if admin context mapping changed withserver.adminContextPath = "admin", "http://localhost:8081/admin/path").String format could be used to format the path:
.targetAdmin("/smth/%s/other", 12).request().buildGet().invoke()IMPORTANT: it would not apply configured defaults (like
TestClientDefaults.defaultHeader(String, Object)). Use admin clientadminClient()to call with defaults.- Parameters:
path- target path, relative to the admin context (could contain String.format() placeholders: %s)args- variables for path placeholders (String.format() arguments)- Returns:
- jersey web target object for the admin context
- Throws:
NullPointerException- for guicey test (when web not started)- See Also:
-
targetRest
Shortcut forWebTargetcreation for the rest context path.Example:
.targetRest("/path").request().buildGet().invoke()would call "http://localhost:8080/path" (or, if rest context mapping changed withserver.rootPath = "api", "http://localhost:8081/api/path").String format could be used to format the path:
.targetRest("/smth/%s/other", 12).request().buildGet().invoke()IMPORTANT: it would not apply configured defaults (like
TestClientDefaults.defaultHeader(String, Object)). Use rest clientrestClient()(or specializedrestClient(Class)) to call with defaults.- Parameters:
path- target path, relative to the rest context (could contain String.format() placeholders: %s)args- variables for path placeholders (String.format() arguments)- Returns:
- jersey web target object for rest context
- Throws:
NullPointerException- for guicey test (when web not started)- See Also:
-
restClient
Construct a rest client. Client would be configured with the current rest path root (@linkbasePathRest()), so requests would need to use only relative paths.Typed rest client could be obtained from this generic client:
restClient().subClient(Resource.class);Inherits current defaults (like
TestClientDefaults.defaultHeader(String, Object).- Returns:
- rest client
-
restClient
Construct a specialized rest client for the given resource class. Client would be configured with the current rest path root (@linkbasePathRest()) together with the resource class path (obtained from annotation), so requests would need to use paths, relative to resource.Also, such client provide methods to configure requests direct from resource methods
ResourceClient.method(ru.vyarus.dropwizard.guice.url.util.Caller).Inherits current defaults (like
TestClientDefaults.defaultHeader(String, Object).- Overrides:
restClientin classTestClient<ClientSupport>- Type Parameters:
K- resource type- Parameters:
resource- resource class to configure target url from- Returns:
- rest client for the given resource class
-
appClient
Construct a client for the app context. Client would be configured with the current app context path root (@linkbasePathApp()), so requests would need to use only relative paths.Note: by default, root client
ClientSupportitself and app context client would be the same, because usually app context is "/". But, you can still prefer app client to shield from potential app context path changes.Inherits current defaults (like
TestClientDefaults.defaultHeader(String, Object).- Returns:
- app client
-
adminClient
Construct a client for the admin context. Client would be configured with the current admin context path root (@linkbasePathAdmin()), so requests would need to use only relative paths.Inherits current defaults (like
TestClientDefaults.defaultHeader(String, Object).- Returns:
- admin client
-
externalClient
Construct a client for external url.Example of variables usage:
externalClient("http://localhost:8080/%s/other", 12).Will not inherit current defaults.
- Parameters:
url- external url, started with "http(s)" (could contain String.format() placeholders: %s)args- variables for path placeholders (String.format() arguments)- Returns:
- client for the given external url
-
close
- Specified by:
closein interfaceAutoCloseable- Throws:
Exception
-
getRoot
protected jakarta.ws.rs.client.WebTarget getRoot()Description copied from class:TestClientSubclasses could override this method: this is required because it is not always possible to provide the correct root target in the constructor (when this method is overridden root, provided in the constructor is null).- Overrides:
getRootin classTestClient<ClientSupport>- Returns:
- root target
-
basePathApp()