Search in sources :

Example 11 with ForestLogger

use of com.dtflys.forest.logging.ForestLogger in project forest by dromara.

the class TestPostClient method testPostJsonWithLog.

@Test
public void testPostJsonWithLog() {
    server.enqueue(new MockResponse().setBody(EXPECTED));
    ForestLogger logger = Mockito.mock(ForestLogger.class);
    configuration.getLogHandler().setLogger(logger);
    JsonTestUser user = new JsonTestUser();
    user.setUsername("foo");
    ForestResponse<String> response = postClient.postJsonWithLog("foo", "1111111111111");
    assertThat(response).isNotNull();
    assertThat(response.getResult()).isNotNull().isEqualTo(EXPECTED);
    ForestRequest request = response.getRequest();
    assertThat(request.getRequestLogMessage()).isNotNull().extracting(RequestLogMessage::getRequest).isEqualTo(request);
    Mockito.verify(logger).info("[Forest] Request (" + configuration.getBackend().getName() + "): \n" + "\tPOST http://localhost:" + server.getPort() + "/json HTTP\n" + "\tHeaders: \n" + "\t\tAccept: application/json\n" + "\t\tAuthorization: 1111111111111\n" + "\t\tContent-Type: application/json\n" + "\tBody: {\"username\":\"foo\"}");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) JsonTestUser(com.dtflys.test.http.model.JsonTestUser) ForestLogger(com.dtflys.forest.logging.ForestLogger) ForestRequest(com.dtflys.forest.http.ForestRequest) Test(org.junit.Test)

Example 12 with ForestLogger

use of com.dtflys.forest.logging.ForestLogger in project forest by dromara.

the class TestPostClient method testPostJsonMapWithLog.

@Test
public void testPostJsonMapWithLog() {
    server.enqueue(new MockResponse().setBody(EXPECTED));
    ForestLogger logger = Mockito.mock(ForestLogger.class);
    configuration.getLogHandler().setLogger(logger);
    Map<String, Object> userMap = new HashMap<>();
    userMap.put("username", "foo");
    assertThat(postClient.postJsonMapWithLog(userMap)).isNotNull().isEqualTo(EXPECTED);
    Mockito.verify(logger).info("[Forest] Request (" + configuration.getBackend().getName() + "): \n" + "\tPOST http://localhost:" + server.getPort() + "/json HTTP\n" + "\tHeaders: \n" + "\t\tContent-Type: application/json\n" + "\tBody: {\"username\":\"foo\"}");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ForestLogger(com.dtflys.forest.logging.ForestLogger) JSONObject(com.alibaba.fastjson.JSONObject) Test(org.junit.Test)

Example 13 with ForestLogger

use of com.dtflys.forest.logging.ForestLogger in project forest by dromara.

the class TestPostClient method testPostJsonObjListWithLog_content_noRequest_noStatus.

@Test
public void testPostJsonObjListWithLog_content_noRequest_noStatus() {
    server.enqueue(new MockResponse().setBody(EXPECTED));
    ForestLogger logger = Mockito.mock(ForestLogger.class);
    configuration.getLogHandler().setLogger(logger);
    JsonTestUser user = new JsonTestUser();
    user.setUsername("foo");
    JsonTestList testList = new JsonTestList();
    testList.setUserList(Collections.singletonList(user));
    List<JsonTestList> list = new ArrayList<>();
    list.add(testList);
    assertThat(postClient.postJsonObjListWithLog_content_noRequest_noStatus(list)).isNotNull().isEqualTo(EXPECTED);
    Mockito.verify(logger, Mockito.never()).info("[Forest] Request (" + configuration.getBackend().getName() + "): \n" + "\tPOST http://localhost:" + server.getPort() + "/json HTTP\n" + "\tHeaders: \n" + "\t\tContent-Type: application/json; charset=utf-8\n" + "\tBody: [{\"userList\":[{\"username\":\"foo\"}]}]");
    Mockito.verify(logger).info("[Forest] Response Content:\n\t" + EXPECTED);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) JsonTestUser(com.dtflys.test.http.model.JsonTestUser) ArrayList(java.util.ArrayList) ForestLogger(com.dtflys.forest.logging.ForestLogger) JsonTestList(com.dtflys.test.http.model.JsonTestList) Test(org.junit.Test)

Example 14 with ForestLogger

use of com.dtflys.forest.logging.ForestLogger in project forest by dromara.

the class SSLTest2 method testConfiguration.

@Test
public void testConfiguration() {
    ForestLogger logger = Mockito.mock(ForestLogger.class);
    assertEquals(Integer.valueOf(300), sslConfig.getMaxConnections());
    assertEquals(Integer.valueOf(300), sslConfig.getMaxRouteConnections());
    assertEquals(Integer.valueOf(3000), sslConfig.getTimeout());
    assertEquals(Integer.valueOf(3000), sslConfig.getConnectTimeout());
    assertEquals(Integer.valueOf(2), sslConfig.getMaxRetryCount());
    assertEquals(1, sslConfig.getSslKeyStores().size());
    SSLKeyStore sslKeyStore = sslConfig.getKeyStore("keystore1");
    assertThat(sslKeyStore).isNotNull();
    // assertEquals("keystore1", sslKeyStore.getId());
    // assertEquals("test.keystore", sslKeyStore.getFilePath());
    // assertEquals("123456", sslKeyStore.getKeystorePass());
    // assertEquals("123456", sslKeyStore.getCertPass());
    // assertEquals(1, sslKeyStore.getProtocols().length);
    // assertEquals("SSLv3", sslKeyStore.getProtocols()[0]);
    assertThat(sslKeyStore.getSslSocketFactoryBuilder()).isNotNull().isInstanceOf(MySSLSocketFactoryBuilder.class);
    assertThat(sslKeyStore.getHostnameVerifier()).isNotNull().isInstanceOf(MyHostnameVerifier.class);
    ForestRequest<String> request = giteeClient.index2();
    assertThat(request).isNotNull();
    request.getLogConfiguration().getLogHandler().setLogger(logger);
    String result = (String) request.execute();
    assertThat(result.startsWith("Global: ")).isTrue();
    Mockito.verify(logger).info("[Forest] [Test2] 请求: \n" + "\tGET https://gitee.com/dt_flys HTTPS");
    Throwable th = null;
    try {
        giteeClient.index3();
    } catch (ForestRuntimeException ex) {
        th = ex.getCause();
    }
    assertThat(th).isNotNull();
}
Also used : ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) SSLKeyStore(com.dtflys.forest.ssl.SSLKeyStore) ForestLogger(com.dtflys.forest.logging.ForestLogger) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

ForestLogger (com.dtflys.forest.logging.ForestLogger)14 Test (org.junit.Test)14 MockResponse (okhttp3.mockwebserver.MockResponse)9 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)5 JsonTestUser (com.dtflys.test.http.model.JsonTestUser)4 JSONObject (com.alibaba.fastjson.JSONObject)2 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)2 SSLKeyStore (com.dtflys.forest.ssl.SSLKeyStore)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 HttpBackend (com.dtflys.forest.backend.HttpBackend)1 ForestRequest (com.dtflys.forest.http.ForestRequest)1 ForestLogHandler (com.dtflys.forest.logging.ForestLogHandler)1 BaseClientTest (com.dtflys.test.http.BaseClientTest)1 JsonTestList (com.dtflys.test.http.model.JsonTestList)1 JsonTestUser2 (com.dtflys.test.http.model.JsonTestUser2)1 ArrayList (java.util.ArrayList)1