Package org.nuxeo.http.test
Class HttpClientTestRule
- java.lang.Object
-
- org.nuxeo.http.test.HttpClientTestRule
-
- All Implemented Interfaces:
org.junit.rules.TestRule
public class HttpClientTestRule extends Object implements org.junit.rules.TestRule
This is an HttpClient wrapped as a JUnitTestRule
to perform needed cleanup on teardown.In a unit test with an embedded Nuxeo, the client can be instantiated like below:
@Inject protected ServletContainerFeature servletContainerFeature; @Rule public final HttpClientTestRule httpClient = HttpClientTestRule.defaultJsonClient( () -> servletContainerFeature.getHttpUrl());
The client is now ready to execute requests onhttp://localhost:PORT
. The default JSON client has the following configuration:- uses Administrator:Administrator basic auth
- sends the Accept header with application/json value
- sends the Content-Type header with application/json value
handler
andexecuteAnd*
APIs, for example:httpClient.buildGetRequest("/api/v1/me").executeAndConsume(new JsonNodeHandler(), node -> { assertEquals("user", node.get("entity-type").asText()); assertEquals("Administrator", node.get("id").asText()); });
In this example, we execute a GET request to/api/v1/me
endpoint, and then we consume the HTTP response with theJsonNodeHandler
handler which asserts that the HTTP response status code has the value200
, asserts that the headerContent-Type
has theapplication/json
value, deserializes the response entity to aJsonNode
, and gives it to the lambda passed as second parameter.When an error or an assertion failure occurs, in the handler or custom assertion, the client will automatically log the HTTP request and response related to the failure.
- Since:
- 2023.13
- See Also:
builder()
,defaultClient(Supplier)
,defaultJsonClient(Supplier)
,org.nuxeo.http.test.handler
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
HttpClientTestRule.Builder
The http client test rule builder.class
HttpClientTestRule.RequestBuilder
-
Field Summary
Fields Modifier and Type Field Description static String
ADMINISTRATOR
protected org.apache.http.impl.client.CloseableHttpClient
client
static Duration
DEFAULT_TIMEOUT
protected Map<String,String>
headers
protected boolean
redirectsEnabled
protected Duration
timeout
protected Supplier<String>
url
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description org.junit.runners.model.Statement
apply(org.junit.runners.model.Statement base, org.junit.runner.Description description)
HttpClientTestRule.RequestBuilder
buildDeleteRequest(String path)
static HttpClientTestRule.Builder
builder()
HttpClientTestRule.RequestBuilder
buildGetRequest(String path)
HttpClientTestRule.RequestBuilder
buildPostRequest(String path)
HttpClientTestRule.RequestBuilder
buildPutRequest(String path)
protected HttpClientTestRule.RequestBuilder
buildRequest(String method, String path)
static HttpClientTestRule
defaultClient(Supplier<String> url)
static HttpClientTestRule
defaultJsonClient(Supplier<String> url)
void
finished()
void
starting()
-
-
-
Field Detail
-
ADMINISTRATOR
public static final String ADMINISTRATOR
- See Also:
- Constant Field Values
-
DEFAULT_TIMEOUT
public static final Duration DEFAULT_TIMEOUT
-
timeout
protected final Duration timeout
-
redirectsEnabled
protected final boolean redirectsEnabled
-
client
protected org.apache.http.impl.client.CloseableHttpClient client
-
-
Method Detail
-
apply
public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base, org.junit.runner.Description description)
- Specified by:
apply
in interfaceorg.junit.rules.TestRule
-
starting
public void starting()
-
finished
public void finished()
-
buildGetRequest
public HttpClientTestRule.RequestBuilder buildGetRequest(String path)
- Returns:
- the
HttpClientTestRule.RequestBuilder
for a GET request
-
buildPostRequest
public HttpClientTestRule.RequestBuilder buildPostRequest(String path)
- Returns:
- the
HttpClientTestRule.RequestBuilder
for a POST request
-
buildPutRequest
public HttpClientTestRule.RequestBuilder buildPutRequest(String path)
- Returns:
- the
HttpClientTestRule.RequestBuilder
for a PUT request
-
buildDeleteRequest
public HttpClientTestRule.RequestBuilder buildDeleteRequest(String path)
- Returns:
- the
HttpClientTestRule.RequestBuilder
for a DELETE request
-
buildRequest
protected HttpClientTestRule.RequestBuilder buildRequest(String method, String path)
-
defaultClient
public static HttpClientTestRule defaultClient(Supplier<String> url)
- Returns:
- An
HttpClientTestRule
targeting the givenurl
and using the Administrator basic auth
-
defaultJsonClient
public static HttpClientTestRule defaultJsonClient(Supplier<String> url)
- Returns:
- An
HttpClientTestRule
targeting the givenurl
, using the Administrator basic auth, and havingapplication/json
forAccept
andContent-Type
headers.
-
builder
public static HttpClientTestRule.Builder builder()
- Returns:
- An
HttpClientTestRule.Builder
to configure precisely your client
-
-