use of javax.servlet.http.Part in project jetty.project by eclipse.
the class MultiPartInputStreamTest method testCRandLFMixRequest.
@Test
public void testCRandLFMixRequest() throws Exception {
String str = "--AaB03x\r" + "content-disposition: form-data; name=\"field1\"\r" + "\r" + "\nJoe Blow\n" + "\r" + "--AaB03x\r" + "content-disposition: form-data; name=\"field2\"\r" + "\r" + "Other\r" + "--AaB03x--\r";
MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 3072, 50);
MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(str.getBytes()), _contentType, config, _tmpDir);
mpis.setDeleteOnExit(true);
Collection<Part> parts = mpis.getParts();
assertThat(parts.size(), is(2));
Part p1 = mpis.getPart("field1");
assertThat(p1, notNullValue());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
IO.copy(p1.getInputStream(), baos);
assertThat(baos.toString("UTF-8"), is("\nJoe Blow\n"));
Part p2 = mpis.getPart("field2");
assertThat(p2, notNullValue());
baos = new ByteArrayOutputStream();
IO.copy(p2.getInputStream(), baos);
assertThat(baos.toString("UTF-8"), is("Other"));
}
use of javax.servlet.http.Part in project jetty.project by eclipse.
the class MultiPartInputStreamTest method testPartFileNotDeleted.
@Test
public void testPartFileNotDeleted() throws Exception {
MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 3072, 50);
MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(createMultipartRequestString("tptfd").getBytes()), _contentType, config, _tmpDir);
mpis.setDeleteOnExit(true);
Collection<Part> parts = mpis.getParts();
MultiPart part = (MultiPart) mpis.getPart("stuff");
File stuff = ((MultiPartInputStreamParser.MultiPart) part).getFile();
// longer than 100 bytes, should already be a tmp file
assertThat(stuff, notNullValue());
part.write("tptfd.txt");
File tptfd = new File(_dirname + File.separator + "tptfd.txt");
assertThat(tptfd.exists(), is(true));
//got renamed
assertThat(stuff.exists(), is(false));
part.cleanUp();
//explicitly written file did not get removed after cleanup
assertThat(tptfd.exists(), is(true));
//clean up test
tptfd.deleteOnExit();
}
use of javax.servlet.http.Part in project jetty.project by eclipse.
the class MultiPartInputStreamTest method testFinalBoundaryOnly.
@Test
public void testFinalBoundaryOnly() throws Exception {
String delimiter = "\r\n";
final String boundary = "MockMultiPartTestBoundary";
// Malformed multipart request body containing only an arbitrary string of text, followed by the final boundary marker, delimited by empty lines.
String str = delimiter + "Hello world" + // Two delimiter markers, which make an empty line.
delimiter + delimiter + "--" + boundary + "--" + delimiter;
MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 3072, 50);
MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(str.getBytes()), "multipart/form-data, boundary=" + boundary, config, _tmpDir);
mpis.setDeleteOnExit(true);
Collection<Part> parts = mpis.getParts();
assertTrue(mpis.getParts().isEmpty());
}
use of javax.servlet.http.Part in project jetty.project by eclipse.
the class MultiPartInputStreamTest method testLFOnlyRequest.
@Test
public void testLFOnlyRequest() throws Exception {
String str = "--AaB03x\n" + "content-disposition: form-data; name=\"field1\"\n" + "\n" + "Joe Blow\n" + "--AaB03x\n" + "content-disposition: form-data; name=\"field2\"\n" + "\n" + "Other\n" + "--AaB03x--\n";
MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 3072, 50);
MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(str.getBytes()), _contentType, config, _tmpDir);
mpis.setDeleteOnExit(true);
Collection<Part> parts = mpis.getParts();
assertThat(parts.size(), is(2));
Part p1 = mpis.getPart("field1");
assertThat(p1, notNullValue());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
IO.copy(p1.getInputStream(), baos);
assertThat(baos.toString("UTF-8"), is("Joe Blow"));
Part p2 = mpis.getPart("field2");
assertThat(p2, notNullValue());
baos = new ByteArrayOutputStream();
IO.copy(p2.getInputStream(), baos);
assertThat(baos.toString("UTF-8"), is("Other"));
}
use of javax.servlet.http.Part in project jetty.project by eclipse.
the class MultiPartInputStreamTest method testLeadingWhitespaceBodyWithoutCRLF.
@Test
public void testLeadingWhitespaceBodyWithoutCRLF() throws Exception {
String body = " " + "--AaB03x\r\n" + "content-disposition: form-data; name=\"field1\"\r\n" + "\r\n" + "Joe Blow\r\n" + "--AaB03x\r\n" + "content-disposition: form-data; name=\"stuff\"; filename=\"" + "foo.txt" + "\"\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "aaaa" + "bbbbb" + "\r\n" + "--AaB03x--\r\n";
MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 3072, 50);
MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(body.getBytes()), _contentType, config, _tmpDir);
mpis.setDeleteOnExit(true);
Collection<Part> parts = mpis.getParts();
assertThat(parts, notNullValue());
assertThat(parts.size(), is(2));
Part field1 = mpis.getPart("field1");
assertThat(field1, notNullValue());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
IO.copy(field1.getInputStream(), baos);
assertThat(baos.toString("US-ASCII"), is("Joe Blow"));
Part stuff = mpis.getPart("stuff");
assertThat(stuff, notNullValue());
baos = new ByteArrayOutputStream();
IO.copy(stuff.getInputStream(), baos);
assertTrue(baos.toString("US-ASCII").contains("bbbbb"));
}
Aggregations