Search in sources :

Example 81 with ForestRequest

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

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

the class Test0 method testClient0.

@Test
public void testClient0() {
    ForestResponse<String> response = beastshopClient.shops();
    assertNotNull(response);
    assertNotNull(response.getResult());
    ForestRequest request = response.getRequest();
    assertNotNull(request);
    String name = request.getHeaderValue("MyName");
    String pass = request.getHeaderValue("MyPass");
    assertEquals("foo", name);
    assertEquals("bar", pass);
}
Also used : ForestRequest(com.dtflys.forest.http.ForestRequest) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 83 with ForestRequest

use of com.dtflys.forest.http.ForestRequest 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)

Example 84 with ForestRequest

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

the class ErrorTest method testErrorCallback.

public void testErrorCallback() {
    configuration.setTimeout(10);
    final AtomicInteger count = new AtomicInteger(0);
    final boolean[] ts = new boolean[] { false };
    errorClient.testError(new OnError() {

        @Override
        public void onError(ForestRuntimeException ex, ForestRequest request, ForestResponse response) {
            int status = response.getStatusCode();
            count.incrementAndGet();
            assertNotNull(ex);
            assertNotNull(request);
        }
    });
    assertEquals(1, count.get());
}
Also used : ForestResponse(com.dtflys.forest.http.ForestResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) OnError(com.dtflys.forest.callback.OnError) ForestRequest(com.dtflys.forest.http.ForestRequest)

Example 85 with ForestRequest

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

the class ProgressTest method testProgressWithNegativeTotalBytes.

@Test
public void testProgressWithNegativeTotalBytes() {
    ForestRequest request = Mockito.mock(ForestRequest.class);
    ForestProgress progress = new ForestProgress(request, -100);
    progress.setBegin(true);
    assertThat(progress.getTotalBytes()).isEqualTo(-100);
    progress.setCurrentBytes(0);
    assertThat(progress.getRate()).isEqualTo(0);
    progress.setCurrentBytes(10);
    assertThat(progress.getRate()).isEqualTo(0);
    progress.setCurrentBytes(100);
    assertThat(progress.getRate()).isEqualTo(0);
}
Also used : ForestProgress(com.dtflys.forest.utils.ForestProgress) ForestRequest(com.dtflys.forest.http.ForestRequest) Test(org.junit.Test)

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