use of com.dtflys.forest.http.ForestRequest in project forest by dromara.
the class TestEncoderClient method testGson.
@Test
public void testGson() {
EncoderClient.Entry entry = new EncoderClient.Entry("AAA", "BBB");
server.enqueue(new MockResponse().setBody(EXPECTED));
ForestRequest request = encoderClient.testGson(entry);
assertThat(request).isNotNull();
assertThat(request.bodyType()).isNotNull().isEqualTo(ForestDataType.JSON);
assertThat(request.getEncoder()).isNotNull().isInstanceOf(ForestGsonConverter.class);
request.execute();
mockRequest(server).assertBodyEquals("{\"name\":\"AAA\",\"value\":\"BBB\"}");
}
use of com.dtflys.forest.http.ForestRequest in project forest by dromara.
the class TestTimeoutClient method testReadTimeout.
@Test
public void testReadTimeout() {
server.enqueue(new MockResponse().setHeadersDelay(20, TimeUnit.MILLISECONDS));
ForestRequest request = timeoutClient.testReadTimeout();
assertThat(request).isNotNull();
assertThat(request.getReadTimeout()).isEqualTo(10);
ForestResponse response = (ForestResponse) request.execute(ForestResponse.class);
assertThat(response).isNotNull();
assertThat(response.isTimeout()).isTrue();
}
use of com.dtflys.forest.http.ForestRequest in project forest by dromara.
the class TestUploadClient method testMixtureUploadImage.
@Test
public void testMixtureUploadImage() throws InterruptedException, FileUploadException {
server.enqueue(new MockResponse().setBody(EXPECTED));
String path = Objects.requireNonNull(this.getClass().getResource("/test-img.jpg")).getPath();
if (path.startsWith("/") && isWindows()) {
path = path.substring(1);
}
File file = new File(path);
Map<String, Object> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
ForestRequest request = uploadClient.imageUploadWithMapParams("img1.jpg", file, map);
assertNotNull(request);
List<ForestMultipart> multipartList = request.getMultiparts();
assertEquals(1, multipartList.size());
ForestMultipart multipart = multipartList.get(0);
// assertTrue(Map.class.isAssignableFrom(request.getMethod().getReturnClass()));
assertTrue(multipart instanceof FileMultipart);
assertEquals("file", multipart.getName());
assertEquals("img1.jpg", multipart.getOriginalFileName());
Map result = (Map) request.execute();
assertNotNull(result);
MockServerRequest.mockRequest(server).assertMultipart("file", multiparts -> {
assertEquals(1, multiparts.size());
FileItem fileItem = multiparts.get(0);
assertEquals("img1.jpg", fileItem.getName());
assertEquals("image/jpeg", fileItem.getContentType());
}).assertMultipart("a", params -> {
assertEquals(1, params.size());
FileItem item = params.get(0);
ContentType contentType = new ContentType(item.getContentType());
assertEquals("text/plain", contentType.toStringWithoutParameters());
try {
assertEquals("1", IOUtils.toString(item.getInputStream()));
} catch (IOException e) {
throw new RuntimeException(e);
}
}).assertMultipart("b", params -> {
assertEquals(1, params.size());
FileItem item = params.get(0);
ContentType contentType = new ContentType(item.getContentType());
assertEquals("text/plain", contentType.toStringWithoutParameters());
try {
assertEquals("2", IOUtils.toString(item.getInputStream()));
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
use of com.dtflys.forest.http.ForestRequest in project forest by dromara.
the class TestUploadClient method testUploadPathList2.
@Test
public void testUploadPathList2() throws InterruptedException, FileUploadException {
server.enqueue(new MockResponse().setBody(EXPECTED));
URL[] urlArray = new URL[] { this.getClass().getResource("/test-img.jpg"), this.getClass().getResource("/test-img2.jpg") };
List<String> pathList = new LinkedList<>();
for (URL url : urlArray) {
String path = getPathFromURL(url);
pathList.add(path);
}
ForestRequest<Map> request = uploadClient.uploadPathList2(pathList);
assertNotNull(request);
List<ForestMultipart> multipartList = request.getMultiparts();
assertEquals(2, multipartList.size());
// assertTrue(Map.class.isAssignableFrom(request.getMethod().getReturnClass()));
int i = 0;
for (ForestMultipart multipart : multipartList) {
assertTrue(multipart instanceof FilePathMultipart);
assertEquals("file_" + i, multipart.getName());
File file = multipart.getFile();
assertNotNull(file);
URL url = urlArray[i];
assertEquals(getPathFromURL(url), file.getAbsolutePath().replaceAll("\\\\", "/"));
assertEquals(getPathFromURL(url), ((FilePathMultipart) multipart).getFilePath());
assertEquals("test-img-" + i + ".jpg", multipart.getOriginalFileName());
i++;
}
Map result = (Map) request.execute();
assertNotNull(result);
MockServerRequest.mockRequest(server).assertMultipart("file_0", fileItems -> {
assertEquals(1, fileItems.size());
FileItem fileItem1 = fileItems.get(0);
assertEquals("test-img-0.jpg", fileItem1.getName());
assertEquals("image/jpeg", fileItem1.getContentType());
try {
byte[] bytes = IOUtils.toByteArray(fileItem1.getInputStream());
URL url = urlArray[0];
assertNotNull(url);
assertArrayEquals(IOUtils.toByteArray(new FileInputStream(url.getFile())), bytes);
} catch (IOException e) {
throw new RuntimeException(e);
}
}).assertMultipart("file_1", fileItems -> {
assertEquals(1, fileItems.size());
FileItem fileItem1 = fileItems.get(0);
assertEquals("test-img-1.jpg", fileItem1.getName());
assertEquals("image/jpeg", fileItem1.getContentType());
try {
byte[] bytes = IOUtils.toByteArray(fileItem1.getInputStream());
URL url = urlArray[1];
assertNotNull(url);
assertArrayEquals(IOUtils.toByteArray(new FileInputStream(url.getFile())), bytes);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
use of com.dtflys.forest.http.ForestRequest 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\"}");
}
Aggregations