Search in sources :

Example 71 with ForestRequest

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);
}
Also used : StatusLine(org.apache.http.StatusLine) ForestResponse(com.dtflys.forest.http.ForestResponse) NoneLifeCycleHandler(com.dtflys.forest.reflection.NoneLifeCycleHandler) LifeCycleHandler(com.dtflys.forest.handler.LifeCycleHandler) HttpclientForestResponseFactory(com.dtflys.forest.backend.httpclient.response.HttpclientForestResponseFactory) HttpResponse(org.apache.http.HttpResponse) ForestRequest(com.dtflys.forest.http.ForestRequest) Date(java.util.Date) NoneLifeCycleHandler(com.dtflys.forest.reflection.NoneLifeCycleHandler) Test(org.junit.Test)

Example 72 with ForestRequest

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}");
}
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) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test) BaseClientTest(com.dtflys.test.http.BaseClientTest)

Example 73 with ForestRequest

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);
}
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) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) BaseClientTest(com.dtflys.test.http.BaseClientTest)

Example 74 with ForestRequest

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

Example 75 with ForestRequest

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);
}
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) 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