Search in sources :

Example 6 with Part

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"));
}
Also used : MultipartConfigElement(javax.servlet.MultipartConfigElement) ByteArrayInputStream(java.io.ByteArrayInputStream) MultiPart(org.eclipse.jetty.util.MultiPartInputStreamParser.MultiPart) Part(javax.servlet.http.Part) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 7 with Part

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();
}
Also used : MultipartConfigElement(javax.servlet.MultipartConfigElement) MultiPart(org.eclipse.jetty.util.MultiPartInputStreamParser.MultiPart) ByteArrayInputStream(java.io.ByteArrayInputStream) MultiPart(org.eclipse.jetty.util.MultiPartInputStreamParser.MultiPart) Part(javax.servlet.http.Part) File(java.io.File) Test(org.junit.Test)

Example 8 with Part

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());
}
Also used : MultipartConfigElement(javax.servlet.MultipartConfigElement) ByteArrayInputStream(java.io.ByteArrayInputStream) MultiPart(org.eclipse.jetty.util.MultiPartInputStreamParser.MultiPart) Part(javax.servlet.http.Part) Test(org.junit.Test)

Example 9 with Part

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"));
}
Also used : MultipartConfigElement(javax.servlet.MultipartConfigElement) ByteArrayInputStream(java.io.ByteArrayInputStream) MultiPart(org.eclipse.jetty.util.MultiPartInputStreamParser.MultiPart) Part(javax.servlet.http.Part) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 10 with Part

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"));
}
Also used : MultipartConfigElement(javax.servlet.MultipartConfigElement) ByteArrayInputStream(java.io.ByteArrayInputStream) MultiPart(org.eclipse.jetty.util.MultiPartInputStreamParser.MultiPart) Part(javax.servlet.http.Part) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

Part (javax.servlet.http.Part)68 Test (org.junit.Test)42 ByteArrayInputStream (java.io.ByteArrayInputStream)27 MultipartConfigElement (javax.servlet.MultipartConfigElement)27 MultiPart (org.eclipse.jetty.util.MultiPartInputStreamParser.MultiPart)24 IOException (java.io.IOException)19 InputStream (java.io.InputStream)12 ArrayList (java.util.ArrayList)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 HttpServletResponse (javax.servlet.http.HttpServletResponse)11 ServletException (javax.servlet.ServletException)10 HttpServletRequest (javax.servlet.http.HttpServletRequest)9 AbstractHttpClientServerTest (org.eclipse.jetty.client.AbstractHttpClientServerTest)7 File (java.io.File)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)5 MockPart (org.springframework.mock.web.test.MockPart)5 RequestPart (org.springframework.web.bind.annotation.RequestPart)5 PrintWriter (java.io.PrintWriter)4