Search in sources :

Example 31 with ForestRequest

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");
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MockResponse(okhttp3.mockwebserver.MockResponse) TypeReference(com.dtflys.forest.utils.TypeReference) ForestRequest(com.dtflys.forest.http.ForestRequest) Result(com.dtflys.test.model.Result) Test(org.junit.Test) BaseClientTest(com.dtflys.test.http.BaseClientTest)

Example 32 with ForestRequest

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();
}
Also used : Arrays(java.util.Arrays) URL(java.net.URL) ContentType(com.dtflys.forest.backend.ContentType) ForestRequest(com.dtflys.forest.http.ForestRequest) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Result(com.dtflys.test.model.Result) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Lists(com.google.common.collect.Lists) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) MockWebServer(okhttp3.mockwebserver.MockWebServer) AssertionsForClassTypes.entry(org.assertj.core.api.AssertionsForClassTypes.entry) MockServerRequest.mockRequest(com.dtflys.forest.mock.MockServerRequest.mockRequest) AssertionsForClassTypes.assertThat(org.assertj.core.api.AssertionsForClassTypes.assertThat) AssertionsForClassTypes.linesOf(org.assertj.core.api.AssertionsForClassTypes.linesOf) ForestHeader(com.dtflys.forest.http.ForestHeader) MalformedURLException(java.net.MalformedURLException) ForestRequestType(com.dtflys.forest.http.ForestRequestType) Test(org.junit.Test) Forest(com.dtflys.forest.Forest) File(java.io.File) Objects(java.util.Objects) CountDownLatch(java.util.concurrent.CountDownLatch) URLEncoder(java.net.URLEncoder) List(java.util.List) JSON(com.alibaba.fastjson.JSON) Rule(org.junit.Rule) ForestURL(com.dtflys.forest.http.ForestURL) Type(java.lang.reflect.Type) ForestAddress(com.dtflys.forest.http.ForestAddress) MockResponse(okhttp3.mockwebserver.MockResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpBackend(com.dtflys.forest.backend.HttpBackend) TypeReference(com.dtflys.forest.utils.TypeReference) BaseClientTest(com.dtflys.test.http.BaseClientTest) MockResponse(okhttp3.mockwebserver.MockResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) BaseClientTest(com.dtflys.test.http.BaseClientTest)

Example 33 with ForestRequest

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);
}
Also used : Arrays(java.util.Arrays) URL(java.net.URL) ContentType(com.dtflys.forest.backend.ContentType) ForestRequest(com.dtflys.forest.http.ForestRequest) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Result(com.dtflys.test.model.Result) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Lists(com.google.common.collect.Lists) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) MockWebServer(okhttp3.mockwebserver.MockWebServer) AssertionsForClassTypes.entry(org.assertj.core.api.AssertionsForClassTypes.entry) MockServerRequest.mockRequest(com.dtflys.forest.mock.MockServerRequest.mockRequest) AssertionsForClassTypes.assertThat(org.assertj.core.api.AssertionsForClassTypes.assertThat) AssertionsForClassTypes.linesOf(org.assertj.core.api.AssertionsForClassTypes.linesOf) ForestHeader(com.dtflys.forest.http.ForestHeader) MalformedURLException(java.net.MalformedURLException) ForestRequestType(com.dtflys.forest.http.ForestRequestType) Test(org.junit.Test) Forest(com.dtflys.forest.Forest) File(java.io.File) Objects(java.util.Objects) CountDownLatch(java.util.concurrent.CountDownLatch) URLEncoder(java.net.URLEncoder) List(java.util.List) JSON(com.alibaba.fastjson.JSON) Rule(org.junit.Rule) ForestURL(com.dtflys.forest.http.ForestURL) Type(java.lang.reflect.Type) ForestAddress(com.dtflys.forest.http.ForestAddress) MockResponse(okhttp3.mockwebserver.MockResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpBackend(com.dtflys.forest.backend.HttpBackend) TypeReference(com.dtflys.forest.utils.TypeReference) BaseClientTest(com.dtflys.test.http.BaseClientTest) MockResponse(okhttp3.mockwebserver.MockResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) BaseClientTest(com.dtflys.test.http.BaseClientTest)

Example 34 with ForestRequest

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");
}
Also used : ForestRequest(com.dtflys.forest.http.ForestRequest) Test(org.junit.Test) BaseClientTest(com.dtflys.test.http.BaseClientTest)

Example 35 with ForestRequest

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);
}
Also used : Arrays(java.util.Arrays) URL(java.net.URL) ContentType(com.dtflys.forest.backend.ContentType) ForestRequest(com.dtflys.forest.http.ForestRequest) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Result(com.dtflys.test.model.Result) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Lists(com.google.common.collect.Lists) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) MockWebServer(okhttp3.mockwebserver.MockWebServer) AssertionsForClassTypes.entry(org.assertj.core.api.AssertionsForClassTypes.entry) MockServerRequest.mockRequest(com.dtflys.forest.mock.MockServerRequest.mockRequest) AssertionsForClassTypes.assertThat(org.assertj.core.api.AssertionsForClassTypes.assertThat) AssertionsForClassTypes.linesOf(org.assertj.core.api.AssertionsForClassTypes.linesOf) ForestHeader(com.dtflys.forest.http.ForestHeader) MalformedURLException(java.net.MalformedURLException) ForestRequestType(com.dtflys.forest.http.ForestRequestType) Test(org.junit.Test) Forest(com.dtflys.forest.Forest) File(java.io.File) Objects(java.util.Objects) CountDownLatch(java.util.concurrent.CountDownLatch) URLEncoder(java.net.URLEncoder) List(java.util.List) JSON(com.alibaba.fastjson.JSON) Rule(org.junit.Rule) ForestURL(com.dtflys.forest.http.ForestURL) Type(java.lang.reflect.Type) ForestAddress(com.dtflys.forest.http.ForestAddress) MockResponse(okhttp3.mockwebserver.MockResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpBackend(com.dtflys.forest.backend.HttpBackend) TypeReference(com.dtflys.forest.utils.TypeReference) BaseClientTest(com.dtflys.test.http.BaseClientTest) MockResponse(okhttp3.mockwebserver.MockResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) BaseClientTest(com.dtflys.test.http.BaseClientTest)

Aggregations

ForestRequest (com.dtflys.forest.http.ForestRequest)85 Test (org.junit.Test)75 MockResponse (okhttp3.mockwebserver.MockResponse)59 BaseClientTest (com.dtflys.test.http.BaseClientTest)48 HttpBackend (com.dtflys.forest.backend.HttpBackend)42 MockWebServer (okhttp3.mockwebserver.MockWebServer)42 Rule (org.junit.Rule)42 ForestConfiguration (com.dtflys.forest.config.ForestConfiguration)39 ForestResponse (com.dtflys.forest.http.ForestResponse)37 CountDownLatch (java.util.concurrent.CountDownLatch)36 BeforeClass (org.junit.BeforeClass)36 MockServerRequest.mockRequest (com.dtflys.forest.mock.MockServerRequest.mockRequest)35 AssertionsForClassTypes.assertThat (org.assertj.core.api.AssertionsForClassTypes.assertThat)35 TimeUnit (java.util.concurrent.TimeUnit)29 AtomicReference (java.util.concurrent.atomic.AtomicReference)29 ContentType (com.dtflys.forest.backend.ContentType)15 File (java.io.File)14 URL (java.net.URL)14 HashMap (java.util.HashMap)14 LinkedHashMap (java.util.LinkedHashMap)14