use of com.dtflys.forest.http.ForestRequest in project forest by dromara.
the class TestResponseFactory method testHttpclientForestResponseFactory.
@Test
public void testHttpclientForestResponseFactory() {
ForestRequest request = new ForestRequest(ForestConfiguration.configuration());
Date requestTime = new Date();
HttpclientForestResponseFactory responseFactory = new HttpclientForestResponseFactory();
HttpResponse httpResponse = mock(HttpResponse.class);
StatusLine statusLine = mock(StatusLine.class);
when(statusLine.getStatusCode()).thenReturn(200);
when(httpResponse.getStatusLine()).thenReturn(statusLine);
LifeCycleHandler lifeCycleHandler = new NoneLifeCycleHandler();
ForestResponse response = responseFactory.createResponse(request, httpResponse, lifeCycleHandler, null, requestTime);
assertThat(response).isNotNull().extracting(ForestResponse::getStatusCode, ForestResponse::getContent, ForestResponse::getResult).contains(200, "", null);
}
use of com.dtflys.forest.http.ForestRequest in project forest by dromara.
the class TestGenericForestClient method testRequest_post_json_body_map2.
@Test
public void testRequest_post_json_body_map2() {
server.enqueue(new MockResponse().setBody(EXPECTED));
TypeReference<Result<Integer>> typeReference = new TypeReference<Result<Integer>>() {
};
Map<String, Integer> map = new LinkedHashMap<>();
map.put("a", 1);
map.put("b", 2);
ForestRequest request = Forest.post("http://localhost:" + server.getPort() + "/post").contentTypeJson().addBody(map).addBody("c", 3);
assertThat(request.body().nameValuesMapWithObject()).extracting("a", "b", "c").contains(1, 2, 3);
Result<Integer> result = (Result<Integer>) request.execute(typeReference);
assertThat(result).isNotNull();
assertThat(result.getStatus()).isEqualTo(1);
assertThat(result.getData()).isEqualTo(2);
mockRequest(server).assertMethodEquals("POST").assertPathEquals("/post").assertHeaderEquals(ForestHeader.CONTENT_TYPE, ContentType.APPLICATION_JSON).assertBodyEquals("{\"a\":1,\"b\":2,\"c\":3}");
}
use of com.dtflys.forest.http.ForestRequest in project forest by dromara.
the class TestGenericForestClient method testRequest_async_error_retry.
@Test
public void testRequest_async_error_retry() throws InterruptedException {
server.enqueue(new MockResponse().setBody(EXPECTED).setResponseCode(400));
server.enqueue(new MockResponse().setBody(EXPECTED).setResponseCode(400));
server.enqueue(new MockResponse().setBody(EXPECTED).setResponseCode(400));
server.enqueue(new MockResponse().setBody(EXPECTED).setResponseCode(400));
CountDownLatch latch = new CountDownLatch(1);
AtomicBoolean isError = new AtomicBoolean(false);
ForestRequest<?> request = Forest.get("http://localhost:" + server.getPort()).maxRetryCount(3).maxRetryInterval(10L).setOnError(((ex, req, res) -> {
isError.set(true);
latch.countDown();
}));
request.execute();
latch.await();
assertThat(isError.get()).isTrue();
assertThat(request.getCurrentRetryCount()).isEqualTo(3);
}
use of com.dtflys.forest.http.ForestRequest in project forest by dromara.
the class TestGenericForestClient method testRequest_change_base_path6.
@Test
public void testRequest_change_base_path6() {
ForestRequest request = Forest.get("/A").host("baidu.com").port(1234).basePath("http://localhost:8080/X1/X2");
assertThat(request).isNotNull();
assertThat(request.host()).isEqualTo("localhost");
assertThat(request.port()).isEqualTo(8080);
assertThat(request.urlString()).isEqualTo("http://localhost:8080/X1/X2/A");
}
use of com.dtflys.forest.http.ForestRequest in project forest by dromara.
the class TestGenericForestClient method testRequest_sync_retryWhen_success.
@Test
public void testRequest_sync_retryWhen_success() {
server.enqueue(new MockResponse().setBody(EXPECTED).setResponseCode(203));
server.enqueue(new MockResponse().setBody(EXPECTED).setResponseCode(203));
server.enqueue(new MockResponse().setBody(EXPECTED).setResponseCode(203));
server.enqueue(new MockResponse().setBody(EXPECTED).setResponseCode(203));
ForestRequest<?> request = Forest.get("http://localhost:" + server.getPort()).maxRetryCount(3).maxRetryInterval(2).retryWhen(((req, res) -> res.statusIs(203)));
request.execute();
assertThat(request.getCurrentRetryCount()).isEqualTo(3);
}
Aggregations