Search in sources :

Example 1 with ByteArrayMultipart

use of com.dtflys.forest.multipart.ByteArrayMultipart in project forest by dromara.

the class TestUploadClient method testUploadByteArrayMap.

/**
 * Test Byte Array Collections *
 */
@Test
public void testUploadByteArrayMap() 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") };
    Map<String, byte[]> byteArrayMap = new LinkedHashMap<>();
    for (int i = 0; i < urlArray.length; i++) {
        URL url = urlArray[i];
        byte[] byteArray = IOUtils.toByteArray(url);
        byteArrayMap.put("test-img-" + i + ".jpg", byteArray);
    }
    ForestRequest<Map> request = uploadClient.uploadByteArrayMap(byteArrayMap);
    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 = byteArrayMap.get(key);
        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) LinkedHashMap(java.util.LinkedHashMap) 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 2 with ByteArrayMultipart

use of com.dtflys.forest.multipart.ByteArrayMultipart in project forest by dromara.

the class TestUploadClient method testUploadByteArrayList.

@Test
public void testUploadByteArrayList() 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") };
    List<byte[]> byteArrayList = new LinkedList<>();
    for (int i = 0; i < urlArray.length; i++) {
        URL url = urlArray[i];
        byte[] byteArray = IOUtils.toByteArray(url);
        byteArrayList.add(byteArray);
    }
    ForestRequest<Map> request = uploadClient.uploadByteArrayList(byteArrayList);
    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 = byteArrayList.get(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) LinkedList(java.util.LinkedList) 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 3 with ByteArrayMultipart

use of com.dtflys.forest.multipart.ByteArrayMultipart in project forest by dromara.

the class TestUploadClient method testUploadByteArray.

@Test
public void testUploadByteArray() throws IOException, InterruptedException, FileUploadException {
    server.enqueue(new MockResponse().setBody(EXPECTED));
    URL url = this.getClass().getResource("/test-img.jpg");
    byte[] byteArray = IOUtils.toByteArray(url);
    ForestRequest<Map> request = uploadClient.upload(byteArray, "test-byte-array.jpg");
    assertNotNull(request);
    List<ForestMultipart> multipartList = request.getMultiparts();
    assertEquals(1, multipartList.size());
    assertTrue(StringUtils.isNotBlank(request.getBoundary()));
    ForestMultipart multipart = multipartList.get(0);
    // assertTrue(Map.class.isAssignableFrom(request.getMethod().getReturnClass()));
    assertTrue(multipart instanceof ByteArrayMultipart);
    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(byteArray, bytes);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) FileItem(org.apache.commons.fileupload.FileItem) ForestMultipart(com.dtflys.forest.multipart.ForestMultipart) ByteArrayMultipart(com.dtflys.forest.multipart.ByteArrayMultipart) IOException(java.io.IOException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) URL(java.net.URL) Test(org.junit.Test)

Example 4 with ByteArrayMultipart

use of com.dtflys.forest.multipart.ByteArrayMultipart 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 5 with ByteArrayMultipart

use of com.dtflys.forest.multipart.ByteArrayMultipart in project forest by dromara.

the class TestMultipart method testByteArrayMutltipart.

public void testByteArrayMutltipart() {
    String text = "{\"id\":1,\"direction\":4,\"type\":\"yyy\",\"body\":\"xxx\",\"crc\":1}";
    byte[] byteArray = text.getBytes();
    ByteArrayMultipart byteArrayMultipart = new ByteArrayMultipart();
    byteArrayMultipart.setData(byteArray);
    byteArrayMultipart.setName("file");
    byteArrayMultipart.setFileName("test.json");
    byteArrayMultipart.setContentType("application/json");
    byte[] results = byteArrayMultipart.getBytes();
    assertNotNull(results);
    assertEquals(byteArray.length, results.length);
    String resultText = new String(results);
    assertEquals(text, resultText);
    assertEquals("file", byteArrayMultipart.getName());
    assertEquals("test.json", byteArrayMultipart.getOriginalFileName());
    assertEquals("application/json", byteArrayMultipart.getContentType());
    assertEquals(byteArray.length, byteArrayMultipart.getSize());
}
Also used : ByteArrayMultipart(com.dtflys.forest.multipart.ByteArrayMultipart)

Aggregations

ByteArrayMultipart (com.dtflys.forest.multipart.ByteArrayMultipart)5 ForestMultipart (com.dtflys.forest.multipart.ForestMultipart)4 IOException (java.io.IOException)4 URL (java.net.URL)4 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 Map (java.util.Map)4 MockResponse (okhttp3.mockwebserver.MockResponse)4 FileItem (org.apache.commons.fileupload.FileItem)4 Test (org.junit.Test)4 FileInputStream (java.io.FileInputStream)3 LinkedList (java.util.LinkedList)1