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 JUnitTestRuleto 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
handlerandexecuteAnd*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/meendpoint, and then we consume the HTTP response with theJsonNodeHandlerhandler which asserts that the HTTP response status code has the value200, asserts that the headerContent-Typehas theapplication/jsonvalue, 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 classHttpClientTestRule.BuilderThe http client test rule builder.classHttpClientTestRule.RequestBuilder
-
Field Summary
Fields Modifier and Type Field Description static StringADMINISTRATORprotected org.apache.http.impl.client.CloseableHttpClientclientstatic DurationDEFAULT_TIMEOUTprotected Map<String,String>headersprotected booleanredirectsEnabledprotected Durationtimeoutprotected Supplier<String>url
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description org.junit.runners.model.Statementapply(org.junit.runners.model.Statement base, org.junit.runner.Description description)HttpClientTestRule.RequestBuilderbuildDeleteRequest(String path)static HttpClientTestRule.Builderbuilder()HttpClientTestRule.RequestBuilderbuildGetRequest(String path)HttpClientTestRule.RequestBuilderbuildPostRequest(String path)HttpClientTestRule.RequestBuilderbuildPutRequest(String path)protected HttpClientTestRule.RequestBuilderbuildRequest(String method, String path)static HttpClientTestRuledefaultClient(Supplier<String> url)static HttpClientTestRuledefaultJsonClient(Supplier<String> url)voidfinished()voidstarting()
-
-
-
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:
applyin interfaceorg.junit.rules.TestRule
-
starting
public void starting()
-
finished
public void finished()
-
buildGetRequest
public HttpClientTestRule.RequestBuilder buildGetRequest(String path)
- Returns:
- the
HttpClientTestRule.RequestBuilderfor a GET request
-
buildPostRequest
public HttpClientTestRule.RequestBuilder buildPostRequest(String path)
- Returns:
- the
HttpClientTestRule.RequestBuilderfor a POST request
-
buildPutRequest
public HttpClientTestRule.RequestBuilder buildPutRequest(String path)
- Returns:
- the
HttpClientTestRule.RequestBuilderfor a PUT request
-
buildDeleteRequest
public HttpClientTestRule.RequestBuilder buildDeleteRequest(String path)
- Returns:
- the
HttpClientTestRule.RequestBuilderfor a DELETE request
-
buildRequest
protected HttpClientTestRule.RequestBuilder buildRequest(String method, String path)
-
defaultClient
public static HttpClientTestRule defaultClient(Supplier<String> url)
- Returns:
- An
HttpClientTestRuletargeting the givenurland using the Administrator basic auth
-
defaultJsonClient
public static HttpClientTestRule defaultJsonClient(Supplier<String> url)
- Returns:
- An
HttpClientTestRuletargeting the givenurl, using the Administrator basic auth, and havingapplication/jsonforAcceptandContent-Typeheaders.
-
builder
public static HttpClientTestRule.Builder builder()
- Returns:
- An
HttpClientTestRule.Builderto configure precisely your client
-
-