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);
}
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();
}
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");
}
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 "";
}
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);
}
Aggregations