Package org.nuxeo.http.test
Class HttpClientTestRule
java.lang.Object
org.nuxeo.http.test.HttpClientTestRule
- All Implemented Interfaces:
org.junit.rules.TestRule
This is an HttpClient wrapped as a JUnit
TestRule 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 on http://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 and executeAnd* 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
the JsonNodeHandler handler which asserts that the HTTP response status code has
the value 200, asserts that the header Content-Type has the application/json value,
deserializes the response entity to a JsonNode, 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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classThe http client test rule builder.class -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionorg.junit.runners.model.Statementapply(org.junit.runners.model.Statement base, org.junit.runner.Description description) buildDeleteRequest(String path) static HttpClientTestRule.Builderbuilder()buildGetRequest(String path) buildPostRequest(String path) buildPutRequest(String path) protected HttpClientTestRule.RequestBuilderbuildRequest(String method, String path) static HttpClientTestRuledefaultClient(Supplier<String> url) static HttpClientTestRuledefaultJsonClient(Supplier<String> url) voidfinished()voidstarting()
-
Field Details
-
ADMINISTRATOR
- See Also:
-
DEFAULT_TIMEOUT
-
url
-
timeout
-
headers
-
redirectsEnabled
protected final boolean redirectsEnabled -
client
protected org.apache.http.impl.client.CloseableHttpClient client
-
-
Method Details
-
apply
public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base, org.junit.runner.Description description) - Specified by:
applyin interfaceorg.junit.rules.TestRule
-
starting
public void starting() -
finished
public void finished() -
buildGetRequest
- Returns:
- the
HttpClientTestRule.RequestBuilderfor a GET request
-
buildPostRequest
- Returns:
- the
HttpClientTestRule.RequestBuilderfor a POST request
-
buildPutRequest
- Returns:
- the
HttpClientTestRule.RequestBuilderfor a PUT request
-
buildDeleteRequest
- Returns:
- the
HttpClientTestRule.RequestBuilderfor a DELETE request
-
buildRequest
-
defaultClient
- Returns:
- An
HttpClientTestRuletargeting the givenurland using the Administrator basic auth
-
defaultJsonClient
- Returns:
- An
HttpClientTestRuletargeting the givenurl, using the Administrator basic auth, and havingapplication/jsonforAcceptandContent-Typeheaders.
-
builder
- Returns:
- An
HttpClientTestRule.Builderto configure precisely your client
-