Search in sources :

Example 6 with Result

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

the class TestGenericForestClient method testRequest_post_json_body_map2.

@Test
public void testRequest_post_json_body_map2() {
    server.enqueue(new MockResponse().setBody(EXPECTED));
    TypeReference<Result<Integer>> typeReference = new TypeReference<Result<Integer>>() {
    };
    Map<String, Integer> map = new LinkedHashMap<>();
    map.put("a", 1);
    map.put("b", 2);
    ForestRequest request = Forest.post("http://localhost:" + server.getPort() + "/post").contentTypeJson().addBody(map).addBody("c", 3);
    assertThat(request.body().nameValuesMapWithObject()).extracting("a", "b", "c").contains(1, 2, 3);
    Result<Integer> result = (Result<Integer>) request.execute(typeReference);
    assertThat(result).isNotNull();
    assertThat(result.getStatus()).isEqualTo(1);
    assertThat(result.getData()).isEqualTo(2);
    mockRequest(server).assertMethodEquals("POST").assertPathEquals("/post").assertHeaderEquals(ForestHeader.CONTENT_TYPE, ContentType.APPLICATION_JSON).assertBodyEquals("{\"a\":1,\"b\":2,\"c\":3}");
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MockResponse(okhttp3.mockwebserver.MockResponse) TypeReference(com.dtflys.forest.utils.TypeReference) ForestRequest(com.dtflys.forest.http.ForestRequest) Result(com.dtflys.test.model.Result) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test) BaseClientTest(com.dtflys.test.http.BaseClientTest)

Aggregations

Result (com.dtflys.test.model.Result)6 BaseClientTest (com.dtflys.test.http.BaseClientTest)4 MockResponse (okhttp3.mockwebserver.MockResponse)4 Test (org.junit.Test)4 TypeReference (com.dtflys.forest.utils.TypeReference)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 ForestRequest (com.dtflys.forest.http.ForestRequest)2 Contact (com.dtflys.test.model.Contact)2 Location (com.dtflys.test.model.Location)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 TypeReference (com.alibaba.fastjson.TypeReference)1 JavaType (com.fasterxml.jackson.databind.JavaType)1 TypeToken (com.google.gson.reflect.TypeToken)1 File (java.io.File)1 Type (java.lang.reflect.Type)1 LinkedHashMap (java.util.LinkedHashMap)1