use of com.dtflys.forest.multipart.FilePathMultipart 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.multipart.FilePathMultipart in project forest by dromara.
the class TestUploadClient method testUploadPathList.
@Test
public void testUploadPathList() 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) {
assertNotNull(url);
String path = getPathFromURL(url);
pathList.add(path);
}
ForestRequest<Map> request = uploadClient.uploadPathList(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", 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", fileItems -> {
assertEquals(2, 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);
}
FileItem fileItem2 = fileItems.get(1);
assertEquals("test-img-1.jpg", fileItem2.getName());
assertEquals("image/jpeg", fileItem2.getContentType());
try {
byte[] bytes = IOUtils.toByteArray(fileItem2.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.multipart.FilePathMultipart in project forest by dromara.
the class TestMultipart method testFilePathMultipart.
public void testFilePathMultipart() throws IOException {
URL url = TestMultipart.class.getClassLoader().getResource("log4j2.xml");
String filePath = url.getFile();
File file = new File(filePath);
FilePathMultipart filePathMultipart = new FilePathMultipart();
filePathMultipart.setName("file");
filePathMultipart.setData(file.getAbsolutePath());
File resultFile = filePathMultipart.getFile();
assertNotNull(resultFile);
assertEquals(file.getAbsoluteFile(), resultFile.getAbsoluteFile());
assertEquals("file", filePathMultipart.getName());
assertEquals("log4j2.xml", filePathMultipart.getOriginalFileName());
long fileSize = FileUtils.sizeOf(file);
assertEquals(fileSize, filePathMultipart.getSize());
String text = FileUtils.readFileToString(file);
String resultText = IOUtils.toString(filePathMultipart.getInputStream());
assertEquals(text, resultText);
byte[] byteArray = filePathMultipart.getBytes();
assertNotNull(byteArray);
String resultText2 = new String(byteArray);
assertEquals(text, resultText2);
}
Aggregations