use of com.dtflys.forest.multipart.ForestMultipart in project forest by dromara.
the class TestUploadClient method testUploadByteArrayArray.
@Test
public void testUploadByteArrayArray() throws IOException, InterruptedException, FileUploadException {
server.enqueue(new MockResponse().setBody(EXPECTED));
URL[] urlArray = new URL[] { this.getClass().getResource("/test-img.jpg"), this.getClass().getResource("/test-img2.jpg") };
byte[][] byteArrayArray = new byte[urlArray.length][];
for (int i = 0; i < urlArray.length; i++) {
URL url = urlArray[i];
assertNotNull(url);
byte[] byteArray = IOUtils.toByteArray(url);
byteArrayArray[i] = byteArray;
}
ForestRequest<Map> request = uploadClient.uploadByteArrayArray(byteArrayArray);
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 ByteArrayMultipart);
assertEquals("file", multipart.getName());
byte[] bytes = multipart.getBytes();
String key = "test-img-" + i + ".jpg";
byte[] byteArray = byteArrayArray[i];
assertNotNull(bytes);
assertByteArrayEquals(byteArray, bytes);
assertEquals(key, 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.ForestMultipart 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);
}
});
}
Aggregations