Search in sources :

Example 21 with ForestMultipart

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);
        }
    });
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ForestMultipart(com.dtflys.forest.multipart.ForestMultipart) IOException(java.io.IOException) URL(java.net.URL) FileInputStream(java.io.FileInputStream) FileItem(org.apache.commons.fileupload.FileItem) ByteArrayMultipart(com.dtflys.forest.multipart.ByteArrayMultipart) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Example 22 with ForestMultipart

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);
        }
    });
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ForestMultipart(com.dtflys.forest.multipart.ForestMultipart) IOException(java.io.IOException) URL(java.net.URL) LinkedList(java.util.LinkedList) FileInputStream(java.io.FileInputStream) FileItem(org.apache.commons.fileupload.FileItem) FilePathMultipart(com.dtflys.forest.multipart.FilePathMultipart) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) File(java.io.File) Test(org.junit.Test)

Aggregations

ForestMultipart (com.dtflys.forest.multipart.ForestMultipart)22 Map (java.util.Map)18 IOException (java.io.IOException)17 HashMap (java.util.HashMap)17 LinkedHashMap (java.util.LinkedHashMap)17 MockResponse (okhttp3.mockwebserver.MockResponse)17 FileItem (org.apache.commons.fileupload.FileItem)17 Test (org.junit.Test)17 FileInputStream (java.io.FileInputStream)16 URL (java.net.URL)14 File (java.io.File)13 ByteArrayMultipart (com.dtflys.forest.multipart.ByteArrayMultipart)11 FilePathMultipart (com.dtflys.forest.multipart.FilePathMultipart)11 LinkedList (java.util.LinkedList)10 ContentType (com.dtflys.forest.backend.ContentType)8 ForestRequest (com.dtflys.forest.http.ForestRequest)8 FileMultipart (com.dtflys.forest.multipart.FileMultipart)8 InputStreamMultipart (com.dtflys.forest.multipart.InputStreamMultipart)8 InputStream (java.io.InputStream)8 JSON (com.alibaba.fastjson.JSON)7