Search in sources :

Example 41 with ForestResponse

use of com.dtflys.forest.http.ForestResponse 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 42 with ForestResponse

use of com.dtflys.forest.http.ForestResponse in project forest by dromara.

the class TestErrorClient method testErrorGet3.

@Test
public void testErrorGet3() {
    server.enqueue(new MockResponse().setResponseCode(500).setBody(EXPECTED));
    boolean hasError = false;
    try {
        getClient.errorGet3();
    } catch (ForestNetworkException ex) {
        ex.printStackTrace();
        hasError = true;
        assertThat(ex.getStatusCode()).isEqualTo(500);
        assertThat(ex.getResponse()).isNotNull().extracting(ForestResponse::getContent).isEqualTo(EXPECTED);
    }
    assertThat(hasError).isTrue();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ForestResponse(com.dtflys.forest.http.ForestResponse) ForestNetworkException(com.dtflys.forest.exceptions.ForestNetworkException) Test(org.junit.Test)

Example 43 with ForestResponse

use of com.dtflys.forest.http.ForestResponse in project forest by dromara.

the class TestHeadClient method testResponseHead.

@Test
public void testResponseHead() throws InterruptedException {
    server.enqueue(new MockResponse().setHeader("server", "mock server").setHeader("Content-Length", "0"));
    ForestResponse response = headClient.responseHead();
    assertThat(response).isNotNull().extracting(ForestResponse::getStatusCode, ForestResponse::isSuccess, ForestResponse::isError).contains(200, true, false);
    assertThat(response.getHeaderValue("server")).isEqualTo("mock server");
    assertThat(response.getHeaderValue("content-length")).isEqualTo("0");
    mockRequest(server).assertMethodEquals("HEAD").assertPathEquals("/hello/user").assertHeaderEquals("Accept", "text/plain").assertHeaderEquals("accessToken", "11111111").assertHeaderEquals("test", "testquery:dsds").assertHeaderEquals("test2", "testquery2: dsds").assertQueryEquals("username", "foo");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ForestResponse(com.dtflys.forest.http.ForestResponse) Test(org.junit.Test)

Example 44 with ForestResponse

use of com.dtflys.forest.http.ForestResponse in project forest by dromara.

the class DefaultLogHandler method redirection.

/**
 * 请求重定向信息
 * @param requestLogMessage
 * @return 请求重定向信息字符串
 */
protected String redirection(RequestLogMessage requestLogMessage) {
    ForestRequest request = requestLogMessage.getRequest();
    if (request.isRedirection()) {
        ForestRequest prevRequest = request.getPrevRequest();
        ForestResponse prevResponse = request.getPrevResponse();
        return "[Redirect]: From " + prevRequest.getType().getName() + " " + prevRequest.getUrl() + " -> " + prevResponse.getStatusCode() + "\n\t";
    }
    return "";
}
Also used : ForestResponse(com.dtflys.forest.http.ForestResponse) ForestRequest(com.dtflys.forest.http.ForestRequest)

Example 45 with ForestResponse

use of com.dtflys.forest.http.ForestResponse in project forest by dromara.

the class Test1 method testClient1.

@Test
public void testClient1() {
    StopWatch sw = new StopWatch();
    sw.start();
    ForestResponse response = baiduClient.testTimeout("xxx");
    sw.stop();
    assertNotNull(response);
    ForestRequest request = response.getRequest();
    int reqTimeout = request.getTimeout();
    assertEquals(50, reqTimeout);
    long time = sw.getTotalTimeMillis();
    assertTrue(time >= 50);
    assertTrue(time <= 3600);
    LogConfiguration logConfiguration = request.getLogConfiguration();
    assertTrue(logConfiguration.isLogEnabled());
    assertTrue(logConfiguration.isLogRequest());
// assertTrue(logConfiguration.isLogResponseStatus());
// assertTrue(!logConfiguration.isLogResponseContent());
// assertTrue(logConfiguration.getLogHandler() instanceof TestLogHandler);
}
Also used : ForestResponse(com.dtflys.forest.http.ForestResponse) ForestRequest(com.dtflys.forest.http.ForestRequest) LogConfiguration(com.dtflys.forest.logging.LogConfiguration) StopWatch(org.springframework.util.StopWatch) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

ForestResponse (com.dtflys.forest.http.ForestResponse)46 ForestRequest (com.dtflys.forest.http.ForestRequest)38 Test (org.junit.Test)37 MockResponse (okhttp3.mockwebserver.MockResponse)34 BaseClientTest (com.dtflys.test.http.BaseClientTest)31 HttpBackend (com.dtflys.forest.backend.HttpBackend)30 ForestConfiguration (com.dtflys.forest.config.ForestConfiguration)30 AtomicReference (java.util.concurrent.atomic.AtomicReference)30 MockWebServer (okhttp3.mockwebserver.MockWebServer)30 BeforeClass (org.junit.BeforeClass)30 Rule (org.junit.Rule)30 MockServerRequest.mockRequest (com.dtflys.forest.mock.MockServerRequest.mockRequest)29 CountDownLatch (java.util.concurrent.CountDownLatch)29 TimeUnit (java.util.concurrent.TimeUnit)29 AssertionsForClassTypes.assertThat (org.assertj.core.api.AssertionsForClassTypes.assertThat)29 ForestNetworkException (com.dtflys.forest.exceptions.ForestNetworkException)3 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)3 HttpclientForestResponseFactory (com.dtflys.forest.backend.httpclient.response.HttpclientForestResponseFactory)2 ForestRetryException (com.dtflys.forest.exceptions.ForestRetryException)2 Date (java.util.Date)2