Search in sources :

Example 1 with TypeReference

use of com.dtflys.forest.utils.TypeReference in project forest by dromara.

the class TestGenericForestClient method testRequest_upload_file.

@Test
public void testRequest_upload_file() {
    server.enqueue(new MockResponse().setBody(EXPECTED));
    TypeReference<Result<Integer>> typeReference = new TypeReference<Result<Integer>>() {
    };
    String path = Objects.requireNonNull(this.getClass().getResource("/test-img.jpg")).getPath();
    if (path.startsWith("/") && isWindows()) {
        path = path.substring(1);
    }
    File file = new File(path);
    Result<Integer> result = Forest.post("http://localhost:" + server.getPort()).contentTypeMultipartFormData().addFile("file", file).execute(typeReference);
    assertThat(result).isNotNull();
    assertThat(result.getStatus()).isEqualTo(1);
    assertThat(result.getData()).isEqualTo(2);
    mockRequest(server).assertMethodEquals("POST").assertPathEquals("/");
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MockResponse(okhttp3.mockwebserver.MockResponse) TypeReference(com.dtflys.forest.utils.TypeReference) File(java.io.File) Result(com.dtflys.test.model.Result) Test(org.junit.Test) BaseClientTest(com.dtflys.test.http.BaseClientTest)

Example 2 with TypeReference

use of com.dtflys.forest.utils.TypeReference in project forest by dromara.

the class TestGenericForestClient method testRequest_post_form_body_keys.

@Test
public void testRequest_post_form_body_keys() {
    server.enqueue(new MockResponse().setBody(EXPECTED));
    TypeReference<Result<Integer>> typeReference = new TypeReference<Result<Integer>>() {
    };
    ForestRequest request = Forest.post("http://localhost:" + server.getPort() + "/post").contentFormUrlEncoded().addBody("a", 1).addBody("b", 2);
    assertThat(request.body().nameValuesMapWithObject()).extracting("a", "b").contains(1, 2);
    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_X_WWW_FORM_URLENCODED).assertBodyEquals("a=1&b=2");
}
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) Test(org.junit.Test) BaseClientTest(com.dtflys.test.http.BaseClientTest)

Example 3 with TypeReference

use of com.dtflys.forest.utils.TypeReference 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

TypeReference (com.dtflys.forest.utils.TypeReference)3 BaseClientTest (com.dtflys.test.http.BaseClientTest)3 Result (com.dtflys.test.model.Result)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 MockResponse (okhttp3.mockwebserver.MockResponse)3 Test (org.junit.Test)3 ForestRequest (com.dtflys.forest.http.ForestRequest)2 File (java.io.File)1 LinkedHashMap (java.util.LinkedHashMap)1