Search in sources :

Example 21 with MultipartEntity

use of org.apache.http.entity.mime.MultipartEntity in project undertow by undertow-io.

the class MultipartFormDataParserTestCase method testFileUploadWithLargeFileSizeThreshold.

@Test
public void testFileUploadWithLargeFileSizeThreshold() throws Exception {
    DefaultServer.setRootHandler(new BlockingHandler(createInMemoryReadingHandler(10_000)));
    TestHttpClient client = new TestHttpClient();
    try {
        HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/path");
        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        entity.addPart("formValue", new StringBody("myValue", "text/plain", StandardCharsets.UTF_8));
        entity.addPart("file", new FileBody(new File(MultipartFormDataParserTestCase.class.getResource("uploadfile.txt").getFile())));
        post.setEntity(entity);
        HttpResponse result = client.execute(post);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        String resp = HttpClientUtils.readResponse(result);
        Map<String, String> parsedResponse = parse(resp);
        Assert.assertEquals("true", parsedResponse.get("in_memory"));
        Assert.assertEquals("uploadfile.txt", parsedResponse.get("file_name"));
        Assert.assertEquals(DigestUtils.md5Hex(new FileInputStream(new File(MultipartFormDataParserTestCase.class.getResource("uploadfile.txt").getFile()))), parsedResponse.get("hash"));
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) FileBody(org.apache.http.entity.mime.content.FileBody) BlockingHandler(io.undertow.server.handlers.BlockingHandler) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody) HttpResponse(org.apache.http.HttpResponse) File(java.io.File) FileInputStream(java.io.FileInputStream) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 22 with MultipartEntity

use of org.apache.http.entity.mime.MultipartEntity in project undertow by undertow-io.

the class MultipartFormDataParserTestCase method testFileUploadWithEagerParsingAndNonASCIIFilename.

@Test
public void testFileUploadWithEagerParsingAndNonASCIIFilename() throws Exception {
    DefaultServer.setRootHandler(new EagerFormParsingHandler().setNext(createHandler()));
    TestHttpClient client = new TestHttpClient();
    try {
        HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/path");
        MultipartEntity entity = new MultipartEntity();
        entity.addPart("formValue", new StringBody("myValue", "text/plain", StandardCharsets.UTF_8));
        File uploadfile = new File(MultipartFormDataParserTestCase.class.getResource("uploadfile.txt").getFile());
        FormBodyPart filePart = new FormBodyPart("file", new FileBody(uploadfile, "τεστ", "application/octet-stream", Charsets.UTF_8.toString()));
        filePart.addField("Content-Disposition", "form-data; name=\"file\"; filename*=\"utf-8''%CF%84%CE%B5%CF%83%CF%84.txt\"");
        entity.addPart(filePart);
        post.setEntity(entity);
        HttpResponse result = client.execute(post);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : FormBodyPart(org.apache.http.entity.mime.FormBodyPart) HttpPost(org.apache.http.client.methods.HttpPost) FileBody(org.apache.http.entity.mime.content.FileBody) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody) HttpResponse(org.apache.http.HttpResponse) File(java.io.File) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 23 with MultipartEntity

use of org.apache.http.entity.mime.MultipartEntity in project undertow by undertow-io.

the class MultipartFormDataParserTestCase method testFileUploadWithMediumFileSizeThresholdAndLargeFile.

@Test
public void testFileUploadWithMediumFileSizeThresholdAndLargeFile() throws Exception {
    int fileSizeThreshold = 1000;
    DefaultServer.setRootHandler(new BlockingHandler(createInMemoryReadingHandler(fileSizeThreshold)));
    TestHttpClient client = new TestHttpClient();
    File file = new File("tmp_upload_file.txt");
    file.createNewFile();
    try {
        writeLargeFileContent(file, fileSizeThreshold * 2);
        HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/path");
        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        entity.addPart("formValue", new StringBody("myValue", "text/plain", StandardCharsets.UTF_8));
        entity.addPart("file", new FileBody(file));
        post.setEntity(entity);
        HttpResponse result = client.execute(post);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        String resp = HttpClientUtils.readResponse(result);
        Map<String, String> parsedResponse = parse(resp);
        Assert.assertEquals("false", parsedResponse.get("in_memory"));
        Assert.assertEquals("tmp_upload_file.txt", parsedResponse.get("file_name"));
        Assert.assertEquals(DigestUtils.md5Hex(new FileInputStream(file)), parsedResponse.get("hash"));
    } finally {
        file.delete();
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) FileBody(org.apache.http.entity.mime.content.FileBody) BlockingHandler(io.undertow.server.handlers.BlockingHandler) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody) HttpResponse(org.apache.http.HttpResponse) File(java.io.File) FileInputStream(java.io.FileInputStream) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 24 with MultipartEntity

use of org.apache.http.entity.mime.MultipartEntity in project undertow by undertow-io.

the class MultiPartTestCase method testMultiPartIndividualFileToLarge.

@Test
public void testMultiPartIndividualFileToLarge() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
        String uri = DefaultServer.getDefaultServerURL() + "/servletContext/3";
        HttpPost post = new HttpPost(uri);
        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        entity.addPart("formValue", new StringBody("myValue", "text/plain", StandardCharsets.UTF_8));
        entity.addPart("file", new FileBody(new File(MultiPartTestCase.class.getResource("uploadfile.txt").getFile())));
        post.setEntity(entity);
        HttpResponse result = client.execute(post);
        final String response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("EXCEPTION: class java.lang.IllegalStateException", response);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) FileBody(org.apache.http.entity.mime.content.FileBody) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody) HttpResponse(org.apache.http.HttpResponse) File(java.io.File) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 25 with MultipartEntity

use of org.apache.http.entity.mime.MultipartEntity in project undertow by undertow-io.

the class MultiPartTestCase method testMultiPartRequestUtf8CharsetInPart.

@Test
public void testMultiPartRequestUtf8CharsetInPart() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
        String uri = DefaultServer.getDefaultServerURL() + "/servletContext/1";
        HttpPost post = new HttpPost(uri);
        MultipartEntity entity = new MultipartEntity();
        entity.addPart("formValue", new StringBody("myValue\u00E5", ContentType.create("text/plain", StandardCharsets.UTF_8)));
        post.setEntity(entity);
        HttpResponse result = client.execute(post);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        final String response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("PARAMS:\r\n" + "parameter count: 1\r\n" + "parameter name count: 1\r\n" + "name: formValue\r\n" + "filename: null\r\n" + "content-type: text/plain; charset=UTF-8\r\n" + "Content-Disposition: form-data; name=\"formValue\"\r\n" + "Content-Transfer-Encoding: 8bit\r\n" + "Content-Type: text/plain; charset=UTF-8\r\n" + "size: 9\r\n" + "content: myValue\u00E5\r\n", response);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Aggregations

MultipartEntity (org.apache.http.entity.mime.MultipartEntity)62 HttpPost (org.apache.http.client.methods.HttpPost)41 StringBody (org.apache.http.entity.mime.content.StringBody)40 FileBody (org.apache.http.entity.mime.content.FileBody)37 File (java.io.File)31 HttpResponse (org.apache.http.HttpResponse)28 Test (org.junit.Test)24 TestHttpClient (io.undertow.testutils.TestHttpClient)13 IOException (java.io.IOException)13 InputStreamBody (org.apache.http.entity.mime.content.InputStreamBody)7 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)7 JSONObject (org.json.JSONObject)7 HashMap (java.util.HashMap)5 Map (java.util.Map)5 HttpEntity (org.apache.http.HttpEntity)5 ByteArrayBody (org.apache.http.entity.mime.content.ByteArrayBody)5 RequestBuilder (org.apache.sling.testing.tools.http.RequestBuilder)5 JSONException (org.json.JSONException)5 BlockingHandler (io.undertow.server.handlers.BlockingHandler)4 InputStream (java.io.InputStream)4