Search in sources :

Example 1 with JsonTestUser

use of com.dtflys.test.http.model.JsonTestUser in project forest by dromara.

the class TestGetClient method testQueryObjectGet.

@Test
public void testQueryObjectGet() throws InterruptedException {
    server.enqueue(new MockResponse().setHeader("Content-Type", "application/json").setHeader("Content-Encoding", "UTF-8").setBody(EXPECTED));
    JsonTestUser user = new JsonTestUser();
    user.setUsername("foo");
    assertThat(getClient.queryObjectGet(user)).isNotNull().isEqualTo(EXPECTED);
    mockRequest(server).assertHeaderEquals("Accept", "text/plain").assertPathEquals("/hello/user").assertQueryEquals("username", "foo");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) JsonTestUser(com.dtflys.test.http.model.JsonTestUser) Test(org.junit.Test)

Example 2 with JsonTestUser

use of com.dtflys.test.http.model.JsonTestUser in project forest by dromara.

the class TestPostClient method testPostJsonObjectWithoutLog.

@Test
public void testPostJsonObjectWithoutLog() {
    server.enqueue(new MockResponse().setBody(EXPECTED));
    ForestLogger logger = Mockito.mock(ForestLogger.class);
    configuration.getLogHandler().setLogger(logger);
    JsonTestUser user = new JsonTestUser();
    user.setUsername("foo");
    assertThat(postClient.postJsonObjectWithoutLog(user)).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\n" + "\tBody: {\"username\":\"foo\"}");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) JsonTestUser(com.dtflys.test.http.model.JsonTestUser) ForestLogger(com.dtflys.forest.logging.ForestLogger) Test(org.junit.Test)

Example 3 with JsonTestUser

use of com.dtflys.test.http.model.JsonTestUser in project forest by dromara.

the class TestPostClient method testPostJsonObjectWithLog_content_noStatus.

@Test
public void testPostJsonObjectWithLog_content_noStatus() {
    server.enqueue(new MockResponse().setBody(EXPECTED));
    ForestLogger logger = Mockito.mock(ForestLogger.class);
    configuration.getLogHandler().setLogger(logger);
    JsonTestUser user = new JsonTestUser();
    user.setUsername("foo");
    assertThat(postClient.postJsonObjectWithLog_content_noStatus(user)).isNotNull().isEqualTo(EXPECTED);
    Mockito.verify(logger).info("[Forest] Request (" + configuration.getBackend().getName() + "): \n" + "\tPOST http://localhost:" + server.getPort() + "/json HTTP\n" + "\tHeaders: \n" + "\t\tAccept-Encoding: UTF-8\n" + "\t\tContent-Type: application/json\n" + "\tBody: {\"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) ForestLogger(com.dtflys.forest.logging.ForestLogger) Test(org.junit.Test)

Example 4 with JsonTestUser

use of com.dtflys.test.http.model.JsonTestUser in project forest by dromara.

the class TestPostClient method testPostJsonObjListInData.

@Test
public void testPostJsonObjListInData() throws InterruptedException {
    server.enqueue(new MockResponse().setBody(EXPECTED));
    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.postJsonObjListInDataProperty(list)).isNotNull().isEqualTo(EXPECTED);
    mockRequest(server).assertMethodEquals("POST").assertPathEquals("/json").assertHeaderEquals("Content-Type", "application/json; charset=utf-8").assertBodyEquals("[{\"userList\":[{\"username\":\"foo\"}]}]");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) JsonTestUser(com.dtflys.test.http.model.JsonTestUser) ArrayList(java.util.ArrayList) JsonTestList(com.dtflys.test.http.model.JsonTestList) Test(org.junit.Test)

Example 5 with JsonTestUser

use of com.dtflys.test.http.model.JsonTestUser 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)

Aggregations

JsonTestUser (com.dtflys.test.http.model.JsonTestUser)11 MockResponse (okhttp3.mockwebserver.MockResponse)11 Test (org.junit.Test)11 JsonTestList (com.dtflys.test.http.model.JsonTestList)5 ForestLogger (com.dtflys.forest.logging.ForestLogger)4 ArrayList (java.util.ArrayList)3 ForestRequest (com.dtflys.forest.http.ForestRequest)1