Search in sources :

Example 21 with StringBody

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

the class ServletCertAndDigestAuthTestCase method testMultipartRequest.

@Test
public void testMultipartRequest() throws Exception {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 2000; i++) {
        sb.append("0123456789");
    }
    try (TestHttpClient client = new TestHttpClient()) {
        // create POST request
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addPart("part1", new ByteArrayBody(sb.toString().getBytes(), "file.txt"));
        builder.addPart("part2", new StringBody("0123456789", ContentType.TEXT_HTML));
        HttpEntity entity = builder.build();
        client.setSSLContext(clientSSLContext);
        String url = DefaultServer.getDefaultServerSSLAddress() + BASE_PATH + "multipart";
        HttpPost post = new HttpPost(url);
        post.setEntity(entity);
        post.addHeader(AUTHORIZATION.toString(), BASIC + " " + FlexBase64.encodeString(("user1" + ":" + "password1").getBytes(StandardCharsets.UTF_8), false));
        HttpResponse result = client.execute(post);
        assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) HttpEntity(org.apache.http.HttpEntity) StringBody(org.apache.http.entity.mime.content.StringBody) ByteArrayBody(org.apache.http.entity.mime.content.ByteArrayBody) HttpResponse(org.apache.http.HttpResponse) SecurityConstraint(io.undertow.servlet.api.SecurityConstraint) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 22 with StringBody

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

the class MultipartFormDataParserTestCase method testFileUpload.

@Test
public void testFileUpload() throws Exception {
    DefaultServer.setRootHandler(new BlockingHandler(createHandler()));
    TestHttpClient client = new TestHttpClient();
    try {
        HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/path");
        // post.setHeader(Headers.CONTENT_TYPE, MultiPartHandler.MULTIPART_FORM_DATA);
        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());
        HttpClientUtils.readResponse(result);
    } 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) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 23 with StringBody

use of org.apache.http.entity.mime.content.StringBody 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 24 with StringBody

use of org.apache.http.entity.mime.content.StringBody 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 25 with StringBody

use of org.apache.http.entity.mime.content.StringBody 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)

Aggregations

StringBody (org.apache.http.entity.mime.content.StringBody)65 HttpPost (org.apache.http.client.methods.HttpPost)44 MultipartEntity (org.apache.http.entity.mime.MultipartEntity)40 FileBody (org.apache.http.entity.mime.content.FileBody)30 HttpResponse (org.apache.http.HttpResponse)29 File (java.io.File)25 Test (org.junit.Test)25 HttpEntity (org.apache.http.HttpEntity)21 IOException (java.io.IOException)15 MultipartEntityBuilder (org.apache.http.entity.mime.MultipartEntityBuilder)15 TestHttpClient (io.undertow.testutils.TestHttpClient)14 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)8 Header (org.apache.http.Header)6 HttpClient (org.apache.http.client.HttpClient)6 FormBodyPart (org.apache.http.entity.mime.FormBodyPart)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 InputStreamBody (org.apache.http.entity.mime.content.InputStreamBody)5 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)5 RequestBuilder (org.apache.sling.testing.tools.http.RequestBuilder)5