use of com.dtflys.forest.multipart.FilePathMultipart in project forest by dromara.
the class TestUploadClient method testUploadPathArray.
@Test
public void testUploadPathArray() 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") };
String[] pathArray = new String[urlArray.length];
for (int i = 0; i < urlArray.length; i++) {
URL url = urlArray[i];
String path = getPathFromURL(url);
pathArray[i] = path;
}
ForestRequest<Map> request = uploadClient.uploadPathArray(pathArray);
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);
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 TestUploadClient method testUploadFilePath.
@Test
public void testUploadFilePath() throws 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);
}
ForestRequest<Map> request = uploadClient.upload(path, progress -> {
});
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 FilePathMultipart);
assertEquals("file", multipart.getName());
File file = multipart.getFile();
assertEquals("test-img.jpg", file.getName());
assertEquals(path, file.getAbsolutePath().replaceAll("\\\\", "/"));
assertEquals("test-img.jpg", multipart.getOriginalFileName());
Object result = request.execute();
assertNotNull(result);
MockServerRequest.mockRequest(server).assertMultipart("file", fileItems -> {
assertEquals(1, fileItems.size());
FileItem fileItem = fileItems.get(0);
assertEquals("test-img.jpg", fileItem.getName());
assertEquals("image/jpeg", fileItem.getContentType());
try {
byte[] bytes = IOUtils.toByteArray(fileItem.getInputStream());
assertArrayEquals(IOUtils.toByteArray(new FileInputStream(file)), bytes);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
use of com.dtflys.forest.multipart.FilePathMultipart in project forest by dromara.
the class TestUploadClient method testUploadPathArray2.
@Test
public void testUploadPathArray2() 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") };
String[] pathArray = new String[urlArray.length];
for (int i = 0; i < urlArray.length; i++) {
URL url = urlArray[i];
String path = getPathFromURL(url);
pathArray[i] = path;
}
ForestRequest<Map> request = uploadClient.uploadPathArray2(pathArray);
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);
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 testUploadPathMap2.
@Test
public void testUploadPathMap2() throws InterruptedException, FileUploadException {
server.enqueue(new MockResponse().setBody(EXPECTED));
String path1 = this.getClass().getResource("/test-img.jpg").getPath();
String path2 = this.getClass().getResource("/test-img2.jpg").getPath();
Map<String, String> pathMap = new LinkedHashMap<>();
pathMap.put("test-map-1.jpg", path1);
pathMap.put("test-map-2.jpg", path2);
for (String key : pathMap.keySet()) {
String value = pathMap.get(key);
if (isWindows() && value.startsWith("/")) {
value = value.substring(1);
}
pathMap.put(key, value);
}
ForestRequest<Map> request = uploadClient.uploadPathMap2(pathMap);
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);
assertEquals("test-map-" + (i + 1) + ".jpg", multipart.getOriginalFileName());
i++;
}
Map result = (Map) request.execute();
assertNotNull(result);
MockServerRequest.mockRequest(server).assertMultipart("file_0", fileItems1 -> {
assertEquals(1, fileItems1.size());
FileItem fileItem1 = fileItems1.get(0);
assertEquals("test-map-1.jpg", fileItem1.getName());
assertEquals("image/jpeg", fileItem1.getContentType());
try {
byte[] bytes = IOUtils.toByteArray(fileItem1.getInputStream());
assertArrayEquals(IOUtils.toByteArray(new FileInputStream(path1)), bytes);
} catch (IOException e) {
throw new RuntimeException(e);
}
}).assertMultipart("file_0", fileItems2 -> {
assertEquals(1, fileItems2.size());
FileItem fileItem1 = fileItems2.get(0);
assertEquals("test-map-1.jpg", fileItem1.getName());
assertEquals("image/jpeg", fileItem1.getContentType());
try {
byte[] bytes = IOUtils.toByteArray(fileItem1.getInputStream());
assertArrayEquals(IOUtils.toByteArray(new FileInputStream(path1)), bytes);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
use of com.dtflys.forest.multipart.FilePathMultipart in project forest by dromara.
the class TestUploadClient method testUploadPathMap.
/**
* Test Path Collections *
*/
@Test
public void testUploadPathMap() throws InterruptedException, FileUploadException {
server.enqueue(new MockResponse().setBody(EXPECTED));
String path1 = this.getClass().getResource("/test-img.jpg").getPath();
String path2 = this.getClass().getResource("/test-img2.jpg").getPath();
Map<String, String> pathMap = new LinkedHashMap<>();
pathMap.put("test-map-1.jpg", path1);
pathMap.put("test-map-2.jpg", path2);
for (String key : pathMap.keySet()) {
String value = pathMap.get(key);
if (isWindows() && value.startsWith("/")) {
value = value.substring(1);
}
pathMap.put(key, value);
}
ForestRequest<Map> request = uploadClient.uploadPathMap(pathMap);
assertNotNull(request);
List<ForestMultipart> multipartList = request.getMultiparts();
assertEquals(2, multipartList.size());
// assertTrue(Map.class.isAssignableFrom(request.getLifeCycleHandler().getResultType()));
int i = 1;
for (ForestMultipart multipart : multipartList) {
assertTrue(multipart instanceof FilePathMultipart);
assertEquals("file", multipart.getName());
File file = multipart.getFile();
assertNotNull(file);
assertEquals("test-map-" + 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-map-1.jpg", fileItem1.getName());
assertEquals("image/jpeg", fileItem1.getContentType());
try {
byte[] bytes = IOUtils.toByteArray(fileItem1.getInputStream());
assertArrayEquals(IOUtils.toByteArray(new FileInputStream(path1)), bytes);
} catch (IOException e) {
throw new RuntimeException(e);
}
FileItem fileItem2 = fileItems.get(1);
assertEquals("test-map-2.jpg", fileItem2.getName());
assertEquals("image/jpeg", fileItem2.getContentType());
try {
byte[] bytes = IOUtils.toByteArray(fileItem2.getInputStream());
assertArrayEquals(IOUtils.toByteArray(new FileInputStream(path2)), bytes);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
Aggregations