use of com.dtflys.forest.http.ForestRequest in project forest by dromara.
the class TestGenericForestClient method testRequest_post_form_body_keys.
@Test
public void testRequest_post_form_body_keys() {
server.enqueue(new MockResponse().setBody(EXPECTED));
TypeReference<Result<Integer>> typeReference = new TypeReference<Result<Integer>>() {
};
ForestRequest request = Forest.post("http://localhost:" + server.getPort() + "/post").contentFormUrlEncoded().addBody("a", 1).addBody("b", 2);
assertThat(request.body().nameValuesMapWithObject()).extracting("a", "b").contains(1, 2);
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_X_WWW_FORM_URLENCODED).assertBodyEquals("a=1&b=2");
}
use of com.dtflys.forest.http.ForestRequest in project forest by dromara.
the class TestGenericForestClient method testRequest_async_retryWhen_success.
@Test
public void testRequest_async_retryWhen_success() throws InterruptedException {
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));
CountDownLatch latch = new CountDownLatch(1);
AtomicBoolean isSuccess = new AtomicBoolean(false);
AtomicInteger atomicRetryCount = new AtomicInteger(0);
ForestRequest<?> request = Forest.get("http://localhost").port(server.getPort()).async().maxRetryCount(3).retryWhen(((req, res) -> res.statusIs(203))).onRetry(((req, res) -> {
atomicRetryCount.incrementAndGet();
})).onSuccess(((data, req, res) -> {
isSuccess.set(true);
latch.countDown();
}));
request.execute();
latch.await();
assertThat(atomicRetryCount.get()).isEqualTo(3);
assertThat(request.getCurrentRetryCount()).isEqualTo(3);
assertThat(isSuccess.get()).isTrue();
}
use of com.dtflys.forest.http.ForestRequest in project forest by dromara.
the class TestGenericForestClient method testRequest_sync_error_retry.
@Test
public void testRequest_sync_error_retry() {
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));
AtomicBoolean isError = new AtomicBoolean(false);
ForestRequest<?> request = Forest.get("/").host("localhost").port(server.getPort()).maxRetryCount(3).onError(((ex, req, res) -> {
isError.set(true);
}));
request.execute();
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_path7.
@Test
public void testRequest_change_base_path7() {
ForestRequest request = Forest.get("/A").basePath("http://localhost:8080/X1/X2").host("baidu.com").port(1234);
assertThat(request).isNotNull();
assertThat(request.host()).isEqualTo("baidu.com");
assertThat(request.port()).isEqualTo(1234);
assertThat(request.urlString()).isEqualTo("http://baidu.com:1234/X1/X2/A");
}
use of com.dtflys.forest.http.ForestRequest in project forest by dromara.
the class TestGenericForestClient method testRequest_sync_retryWhen_error_not_retry.
@Test
public void testRequest_sync_retryWhen_error_not_retry() {
server.enqueue(new MockResponse().setBody(EXPECTED).setResponseCode(400));
AtomicBoolean isError = new AtomicBoolean(false);
ForestRequest<?> request = Forest.get("http://localhost").port(server.getPort()).maxRetryCount(3).retryWhen(((req, res) -> res.statusIs(200))).onError(((ex, req, res) -> {
isError.set(true);
}));
request.execute();
assertThat(isError.get()).isTrue();
assertThat(request.getCurrentRetryCount()).isEqualTo(0);
}
Aggregations