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
Modifier and TypeClassDescriptionstatic class
The http client test rule builder.class
-
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionorg.junit.runners.model.Statement
apply
(org.junit.runners.model.Statement base, org.junit.runner.Description description) buildDeleteRequest
(String path) static HttpClientTestRule.Builder
builder()
buildGetRequest
(String path) buildPostRequest
(String path) 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 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:
apply
in interfaceorg.junit.rules.TestRule
-
starting
public void starting() -
finished
public void finished() -
buildGetRequest
- Returns:
- the
HttpClientTestRule.RequestBuilder
for a GET request
-
buildPostRequest
- Returns:
- the
HttpClientTestRule.RequestBuilder
for a POST request
-
buildPutRequest
- Returns:
- the
HttpClientTestRule.RequestBuilder
for a PUT request
-
buildDeleteRequest
- Returns:
- the
HttpClientTestRule.RequestBuilder
for a DELETE request
-
buildRequest
-
defaultClient
- Returns:
- An
HttpClientTestRule
targeting the givenurl
and using the Administrator basic auth
-
defaultJsonClient
- Returns:
- An
HttpClientTestRule
targeting the givenurl
, using the Administrator basic auth, and havingapplication/json
forAccept
andContent-Type
headers.
-
builder
- Returns:
- An
HttpClientTestRule.Builder
to configure precisely your client
-