use of com.dtflys.forest.multipart.InputStreamMultipart in project forest by dromara.
the class TestUploadClient method testUploadInputStream.
@Test
public void testUploadInputStream() throws IOException, InterruptedException, FileUploadException {
server.enqueue(new MockResponse().setBody(EXPECTED));
String path = this.getClass().getResource("/test-img.jpg").getPath();
if (path.startsWith("/") && isWindows()) {
path = path.substring(1);
}
File file = new File(path);
InputStream in = new FileInputStream(file);
ForestRequest<Map> request = uploadClient.upload(in, "test-byte-array.jpg");
assertNotNull(request);
assertTrue(StringUtils.isNotEmpty(request.getBoundary()));
List<ForestMultipart> multipartList = request.getMultiparts();
assertEquals(1, multipartList.size());
ForestMultipart multipart = multipartList.get(0);
// assertTrue(Map.class.isAssignableFrom(request.getMethod().getReturnClass()));
assertTrue(multipart instanceof InputStreamMultipart);
assertEquals("file", multipart.getName());
assertEquals("test-byte-array.jpg", multipart.getOriginalFileName());
Map result = (Map) request.execute();
assertNotNull(result);
MockServerRequest.mockRequest(server).assertMultipart("file", fileItems -> {
assertEquals(1, fileItems.size());
FileItem fileItem = fileItems.get(0);
assertEquals("test-byte-array.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.InputStreamMultipart in project forest by dromara.
the class TestMultipart method testInputStreamMultipart.
public void testInputStreamMultipart() throws IOException {
URL url = TestMultipart.class.getClassLoader().getResource("log4j2.xml");
String filePath = url.getFile();
File file = new File(filePath);
InputStream in = new FileInputStream(file);
InputStreamMultipart inputStreamMultipart = new InputStreamMultipart();
inputStreamMultipart.setName("file");
inputStreamMultipart.setData(in);
File resultFile = inputStreamMultipart.getFile();
inputStreamMultipart.setFileName("log4j2.xml");
assertNull(resultFile);
assertEquals("file", inputStreamMultipart.getName());
assertEquals("log4j2.xml", inputStreamMultipart.getOriginalFileName());
assertEquals(-1, inputStreamMultipart.getSize());
String text = FileUtils.readFileToString(file);
String resultText = IOUtils.toString(inputStreamMultipart.getInputStream());
assertEquals(text, resultText);
}
use of com.dtflys.forest.multipart.InputStreamMultipart in project forest by dromara.
the class TestMultipart method testInputStreamMultipart2.
public void testInputStreamMultipart2() throws IOException {
URL url = TestMultipart.class.getClassLoader().getResource("log4j2.xml");
String filePath = url.getFile();
File file = new File(filePath);
InputStream in = new FileInputStream(file);
InputStreamMultipart inputStreamMultipart = new InputStreamMultipart();
inputStreamMultipart.setData(in);
File resultFile = inputStreamMultipart.getFile();
inputStreamMultipart.setFileName("log4j2.xml");
assertNull(resultFile);
assertEquals("log4j2.xml", inputStreamMultipart.getOriginalFileName());
assertEquals(-1, inputStreamMultipart.getSize());
String text = FileUtils.readFileToString(file);
byte[] byteArray = inputStreamMultipart.getBytes();
assertNotNull(byteArray);
String resultText2 = new String(byteArray);
assertEquals(text, resultText2);
}
Aggregations