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