Class ClientSupport
- java.lang.Object
-
- ru.vyarus.dropwizard.guice.test.ClientSupport
-
- All Implemented Interfaces:
java.lang.AutoCloseable
public class ClientSupport extends java.lang.Object implements java.lang.AutoCloseableJerseyClientsupport for direct web tests (complete dropwizard startup).Client support maintains single
JerseyClientinstance. 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 from actual configuration.- Since:
- 04.05.2020
-
-
Constructor Summary
Constructors Constructor Description ClientSupport(io.dropwizard.testing.DropwizardTestSupport<?> support)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.StringbasePathAdmin()For example, with default configuration it would be "http://localhost:8080/".java.lang.StringbasePathMain()For example, with default configuration it would be "http://localhost:8080/".java.lang.StringbasePathRest()For example, with default configuration it would be "http://localhost:8080/".voidclose()intgetAdminPort()org.glassfish.jersey.client.JerseyClientgetClient()Single client instance maintained within test and method will always return the same instance.intgetPort()javax.ws.rs.client.WebTargettarget(java.lang.String... paths)Unbounded (universal)WebTargetconstruction shortcut.javax.ws.rs.client.WebTargettargetAdmin(java.lang.String... paths)Shortcut forWebTargetcreation for admin context path.javax.ws.rs.client.WebTargettargetMain(java.lang.String... paths)Shortcut forWebTargetcreation for main context path.javax.ws.rs.client.WebTargettargetRest(java.lang.String... paths)Shortcut forWebTargetcreation for rest context path.
-
-
-
Method Detail
-
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
-
getPort
public int getPort()
- Returns:
- main context port
- Throws:
java.lang.NullPointerException- for guicey test
-
getAdminPort
public int getAdminPort()
- Returns:
- admin context port
- Throws:
java.lang.NullPointerException- for guicey test
-
basePathMain
public java.lang.String basePathMain()
For example, with default configuration it would be "http://localhost:8080/". If "server.applicationContextPath" would be changed to "/someth" then method will return "http://localhost:8080/someth/".Returned path will always end with slash.
- Returns:
- base path for application main context
- Throws:
java.lang.NullPointerException- for guicey test
-
basePathAdmin
public java.lang.String basePathAdmin()
For example, with default configuration it would be "http://localhost:8080/". If "server.rootPath" would be changed to "/someth" then method will return "http://localhost:8080/someth/". If main context mapping changed from root, then returned path will count in too (e.g. "http://localhost:8080/root/rest/", when "server.applicationContextPath" is "/root").Returned path will always end with slash.
- Returns:
- base path for admin context
- Throws:
java.lang.NullPointerException- for guicey test
-
basePathRest
public java.lang.String basePathRest()
For example, with default configuration it would be "http://localhost:8080/". If "server.applicationContextPath" would be changed to "/someth" then method will return "http://localhost:8080/someth/".Returned path will always end with slash.
- Returns:
- base path for rest
- Throws:
java.lang.NullPointerException- for guicey test
-
target
public javax.ws.rs.client.WebTarget target(java.lang.String... paths)
Unbounded (universal)WebTargetconstruction shortcut. First url part must contain host (port) target. 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("http://localhotst:8080/smth/").request().buildGet().invoke()NOTE: safe to use with guicey-only tests (when web part not started) to call any external url.
- Parameters:
paths- one or more path parts (joined with '/')- Returns:
- jersey web target object
-
targetMain
public javax.ws.rs.client.WebTarget targetMain(java.lang.String... paths)
Shortcut forWebTargetcreation for main context path. Method abstracts you from actual configuration so you can just call servlets by their registration uri.Without parameters it will target main context root:
.targetMain().request().buildGet().invoke()would call "http://localhost:8080/".Additional paths may be provided to construct urls:
.targetMain("something").request().buildGet().invoke()would call "http://localhost:8080/something" and.targetMain("foo", "bar").request().buildGet().invoke()would call "http://localhost:8080/foo/bar". Last example is equivalent to jersey api (kind of shortcut):.targetMain().path("foo").path("bar").request().buildGet().invoke().- Parameters:
paths- zero, one or more path parts (joined with '/') and appended to base path- Returns:
- jersey web target object for main context
- Throws:
java.lang.NullPointerException- for guicey test- See Also:
for base use construction details
-
targetAdmin
public javax.ws.rs.client.WebTarget targetAdmin(java.lang.String... paths)
Shortcut forWebTargetcreation for admin context path. Method abstracts you from actual configuration so you can just call servlets by their registration uri.Without parameters it will target admin context root:
.targetAdmin().request().buildGet().invoke()would call "http://localhost:8081/". For simple server it would be "http://localhost:8080/admin/".Additional paths may be provided to construct urls:
.targetAdmin("something").request().buildGet().invoke()would call "http://localhost:8081/something" and.targetAdmin("foo", "bar").request().buildGet().invoke()would call "http://localhost:8081/foo/bar". Last example is equivalent to jersey api (kind of shortcut):.targetAdmin().path("foo").path("bar").request().buildGet().invoke().- Parameters:
paths- zero, one or more path parts (joined with '/') and appended to base path- Returns:
- jersey web target object for admin context
- Throws:
java.lang.NullPointerException- for guicey test- See Also:
for base use construction details
-
targetRest
public javax.ws.rs.client.WebTarget targetRest(java.lang.String... paths)
Shortcut forWebTargetcreation for rest context path. Method abstracts you from actual configuration so you can just call rest resources by their registration uri.Without parameters it will target rest context root:
.targetRest().request().buildGet().invoke()would call "http://localhost:8080/".Additional paths may be provided to construct urls:
.targetRest("something").request().buildGet().invoke()would call "http://localhost:8080/something" and.targetRest("foo", "bar").request().buildGet().invoke()would call "http://localhost:8080/foo/bar". Last example is equivalent to jersey api (kind of shortcut):.targetRest().path("foo").path("bar").request().buildGet().invoke().- Parameters:
paths- zero, one or more path parts (joined with '/') and appended to base path- Returns:
- jersey web target object for rest context
- Throws:
java.lang.NullPointerException- for guicey test- See Also:
for base use construction details
-
close
public void close() throws java.lang.Exception- Specified by:
closein interfacejava.lang.AutoCloseable- Throws:
java.lang.Exception
-
-