Search in sources :

Example 1 with FileMultipart

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

the class TestMultipart method testFileMultipart.

public void testFileMultipart() throws IOException {
    URL url = TestMultipart.class.getClassLoader().getResource("log4j2.xml");
    File file = new File(url.getFile());
    assertNotNull(file);
    FileMultipart fileMultipart = new FileMultipart();
    fileMultipart.setName("file");
    fileMultipart.setData(file);
    File resultFile = fileMultipart.getFile();
    assertNotNull(resultFile);
    assertEquals(file, resultFile);
    assertEquals("file", fileMultipart.getName());
    assertEquals("log4j2.xml", fileMultipart.getOriginalFileName());
    long fileSize = FileUtils.sizeOf(file);
    assertEquals(fileSize, fileMultipart.getSize());
    String text = FileUtils.readFileToString(file);
    String resultText = IOUtils.toString(fileMultipart.getInputStream());
    assertEquals(text, resultText);
    byte[] byteArray = fileMultipart.getBytes();
    assertNotNull(byteArray);
    String resultText2 = new String(byteArray);
    assertEquals(text, resultText2);
}
Also used : FileMultipart(com.dtflys.forest.multipart.FileMultipart) File(java.io.File) URL(java.net.URL)

Example 2 with FileMultipart

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

the class TestUploadClient method testUploadFile.

@Test
public void testUploadFile() throws InterruptedException, 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);
    }
    File file = new File(path);
    ForestRequest<Map> request = uploadClient.upload(file, progress -> {
    });
    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 FileMultipart);
    assertEquals("file", multipart.getName());
    assertEquals("test-img.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-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);
        }
    });
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) FileMultipart(com.dtflys.forest.multipart.FileMultipart) ForestMultipart(com.dtflys.forest.multipart.ForestMultipart) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) FileItem(org.apache.commons.fileupload.FileItem) File(java.io.File) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Example 3 with FileMultipart

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

the class TestUploadClient method testMixtureImageUploadWithJSONBodyParamsAndWithoutContentType.

@Test
public void testMixtureImageUploadWithJSONBodyParamsAndWithoutContentType() throws InterruptedException, 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);
    }
    File file = new File(path);
    Map<String, Object> map = new HashMap<>();
    map.put("a", 1);
    map.put("b", 2);
    ForestRequest request = uploadClient.imageUploadWithJSONBodyParamsAndWithoutContentType("img1.jpg", file, map);
    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 FileMultipart);
    assertEquals("file", multipart.getName());
    assertEquals("img1.jpg", multipart.getOriginalFileName());
    Map result = (Map) request.execute();
    assertNotNull(result);
    MockServerRequest.mockRequest(server).assertMultipart("file", multiparts -> {
        assertEquals(1, multiparts.size());
        FileItem fileItem = multiparts.get(0);
        assertEquals("img1.jpg", fileItem.getName());
        assertEquals("image/jpeg", fileItem.getContentType());
    }).assertMultipart("params", params -> {
        assertEquals(1, params.size());
        FileItem item = params.get(0);
        ContentType contentType = new ContentType(item.getContentType());
        assertEquals("application/json", contentType.toStringWithoutParameters());
        try {
            assertEquals(JSON.toJSONString(map), IOUtils.toString(item.getInputStream(), "UTF-8"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : BeforeClass(org.junit.BeforeClass) URL(java.net.URL) ContentType(com.dtflys.forest.backend.ContentType) ForestRequest(com.dtflys.forest.http.ForestRequest) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Assert.assertThat(org.junit.Assert.assertThat) ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) InputStreamMultipart(com.dtflys.forest.multipart.InputStreamMultipart) Map(java.util.Map) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) MockWebServer(okhttp3.mockwebserver.MockWebServer) LinkedList(java.util.LinkedList) ForestMultipart(com.dtflys.forest.multipart.ForestMultipart) Assert.assertNotNull(org.junit.Assert.assertNotNull) FileItem(org.apache.commons.fileupload.FileItem) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) FileMultipart(com.dtflys.forest.multipart.FileMultipart) ByteArrayMultipart(com.dtflys.forest.multipart.ByteArrayMultipart) File(java.io.File) Objects(java.util.Objects) UploadClient(com.dtflys.test.http.client.UploadClient) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) JSON(com.alibaba.fastjson.JSON) Rule(org.junit.Rule) MockServerRequest(com.dtflys.forest.mock.MockServerRequest) StringUtils(com.dtflys.forest.utils.StringUtils) FilePathMultipart(com.dtflys.forest.multipart.FilePathMultipart) FileUploadException(org.apache.commons.fileupload.FileUploadException) MockResponse(okhttp3.mockwebserver.MockResponse) HttpBackend(com.dtflys.forest.backend.HttpBackend) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) MockResponse(okhttp3.mockwebserver.MockResponse) FileMultipart(com.dtflys.forest.multipart.FileMultipart) ContentType(com.dtflys.forest.backend.ContentType) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ForestMultipart(com.dtflys.forest.multipart.ForestMultipart) ForestRequest(com.dtflys.forest.http.ForestRequest) IOException(java.io.IOException) FileItem(org.apache.commons.fileupload.FileItem) File(java.io.File) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Example 4 with FileMultipart

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

the class TestUploadClient method testMixtureUploadImageWithBodyParams.

@Test
public void testMixtureUploadImageWithBodyParams() throws InterruptedException, 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);
    }
    File file = new File(path);
    ForestRequest request = uploadClient.imageUploadWithBodyParams("img1.jpg", file, "1", "2");
    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 FileMultipart);
    assertEquals("file", multipart.getName());
    assertEquals("img1.jpg", multipart.getOriginalFileName());
    Map result = (Map) request.execute();
    assertNotNull(result);
    MockServerRequest.mockRequest(server).assertMultipart("file", multiparts -> {
        assertEquals(1, multiparts.size());
        FileItem fileItem = multiparts.get(0);
        assertEquals("img1.jpg", fileItem.getName());
        assertEquals("image/jpeg", fileItem.getContentType());
    }).assertMultipart("a", params -> {
        assertEquals(1, params.size());
        FileItem item = params.get(0);
        ContentType contentType = new ContentType(item.getContentType());
        assertEquals("text/plain", contentType.toStringWithoutParameters());
        try {
            assertEquals("1", IOUtils.toString(item.getInputStream()));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }).assertMultipart("b", params -> {
        assertEquals(1, params.size());
        FileItem item = params.get(0);
        ContentType contentType = new ContentType(item.getContentType());
        assertEquals("text/plain", contentType.toStringWithoutParameters());
        try {
            assertEquals("2", IOUtils.toString(item.getInputStream()));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : BeforeClass(org.junit.BeforeClass) URL(java.net.URL) ContentType(com.dtflys.forest.backend.ContentType) ForestRequest(com.dtflys.forest.http.ForestRequest) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Assert.assertThat(org.junit.Assert.assertThat) ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) InputStreamMultipart(com.dtflys.forest.multipart.InputStreamMultipart) Map(java.util.Map) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) MockWebServer(okhttp3.mockwebserver.MockWebServer) LinkedList(java.util.LinkedList) ForestMultipart(com.dtflys.forest.multipart.ForestMultipart) Assert.assertNotNull(org.junit.Assert.assertNotNull) FileItem(org.apache.commons.fileupload.FileItem) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) FileMultipart(com.dtflys.forest.multipart.FileMultipart) ByteArrayMultipart(com.dtflys.forest.multipart.ByteArrayMultipart) File(java.io.File) Objects(java.util.Objects) UploadClient(com.dtflys.test.http.client.UploadClient) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) JSON(com.alibaba.fastjson.JSON) Rule(org.junit.Rule) MockServerRequest(com.dtflys.forest.mock.MockServerRequest) StringUtils(com.dtflys.forest.utils.StringUtils) FilePathMultipart(com.dtflys.forest.multipart.FilePathMultipart) FileUploadException(org.apache.commons.fileupload.FileUploadException) MockResponse(okhttp3.mockwebserver.MockResponse) HttpBackend(com.dtflys.forest.backend.HttpBackend) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) MockResponse(okhttp3.mockwebserver.MockResponse) FileMultipart(com.dtflys.forest.multipart.FileMultipart) ContentType(com.dtflys.forest.backend.ContentType) ForestMultipart(com.dtflys.forest.multipart.ForestMultipart) ForestRequest(com.dtflys.forest.http.ForestRequest) IOException(java.io.IOException) FileItem(org.apache.commons.fileupload.FileItem) File(java.io.File) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Example 5 with FileMultipart

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

the class TestUploadClient method testMixtureUploadImageWithJSONBodyParams.

@Test
public void testMixtureUploadImageWithJSONBodyParams() throws InterruptedException, 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);
    }
    File file = new File(path);
    Map<String, Object> map = new HashMap<>();
    map.put("a", 1);
    map.put("b", 2);
    ForestRequest request = uploadClient.imageUploadWithJSONBodyParams("img1.jpg", file, map);
    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 FileMultipart);
    assertEquals("file", multipart.getName());
    assertEquals("img1.jpg", multipart.getOriginalFileName());
    Map result = (Map) request.execute();
    assertNotNull(result);
    MockServerRequest.mockRequest(server).assertMultipart("file", multiparts -> {
        assertEquals(1, multiparts.size());
        FileItem fileItem = multiparts.get(0);
        assertEquals("img1.jpg", fileItem.getName());
        assertEquals("image/jpeg", fileItem.getContentType());
    }).assertMultipart("params", params -> {
        assertEquals(1, params.size());
        FileItem item = params.get(0);
        ContentType contentType = new ContentType(item.getContentType());
        assertEquals("application/json", contentType.toStringWithoutParameters());
        try {
            assertEquals(JSON.toJSONString(map), IOUtils.toString(item.getInputStream()));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : BeforeClass(org.junit.BeforeClass) URL(java.net.URL) ContentType(com.dtflys.forest.backend.ContentType) ForestRequest(com.dtflys.forest.http.ForestRequest) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Assert.assertThat(org.junit.Assert.assertThat) ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) InputStreamMultipart(com.dtflys.forest.multipart.InputStreamMultipart) Map(java.util.Map) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) MockWebServer(okhttp3.mockwebserver.MockWebServer) LinkedList(java.util.LinkedList) ForestMultipart(com.dtflys.forest.multipart.ForestMultipart) Assert.assertNotNull(org.junit.Assert.assertNotNull) FileItem(org.apache.commons.fileupload.FileItem) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) FileMultipart(com.dtflys.forest.multipart.FileMultipart) ByteArrayMultipart(com.dtflys.forest.multipart.ByteArrayMultipart) File(java.io.File) Objects(java.util.Objects) UploadClient(com.dtflys.test.http.client.UploadClient) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) JSON(com.alibaba.fastjson.JSON) Rule(org.junit.Rule) MockServerRequest(com.dtflys.forest.mock.MockServerRequest) StringUtils(com.dtflys.forest.utils.StringUtils) FilePathMultipart(com.dtflys.forest.multipart.FilePathMultipart) FileUploadException(org.apache.commons.fileupload.FileUploadException) MockResponse(okhttp3.mockwebserver.MockResponse) HttpBackend(com.dtflys.forest.backend.HttpBackend) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) MockResponse(okhttp3.mockwebserver.MockResponse) FileMultipart(com.dtflys.forest.multipart.FileMultipart) ContentType(com.dtflys.forest.backend.ContentType) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ForestMultipart(com.dtflys.forest.multipart.ForestMultipart) ForestRequest(com.dtflys.forest.http.ForestRequest) IOException(java.io.IOException) FileItem(org.apache.commons.fileupload.FileItem) File(java.io.File) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

FileMultipart (com.dtflys.forest.multipart.FileMultipart)6 File (java.io.File)6 ForestMultipart (com.dtflys.forest.multipart.ForestMultipart)5 FileInputStream (java.io.FileInputStream)5 IOException (java.io.IOException)5 URL (java.net.URL)5 HashMap (java.util.HashMap)5 LinkedHashMap (java.util.LinkedHashMap)5 Map (java.util.Map)5 MockResponse (okhttp3.mockwebserver.MockResponse)5 FileItem (org.apache.commons.fileupload.FileItem)5 Test (org.junit.Test)5 JSON (com.alibaba.fastjson.JSON)4 ContentType (com.dtflys.forest.backend.ContentType)4 HttpBackend (com.dtflys.forest.backend.HttpBackend)4 ForestConfiguration (com.dtflys.forest.config.ForestConfiguration)4 ForestRequest (com.dtflys.forest.http.ForestRequest)4 MockServerRequest (com.dtflys.forest.mock.MockServerRequest)4 ByteArrayMultipart (com.dtflys.forest.multipart.ByteArrayMultipart)4 FilePathMultipart (com.dtflys.forest.multipart.FilePathMultipart)4