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());
}
}
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();
}
}
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();
}
}
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();
}
}
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();
}
}
Aggregations