Search in sources :

Example 11 with MultipartConfigElement

use of javax.servlet.MultipartConfigElement in project jetty.project by eclipse.

the class MultiPartInputStreamTest method testBadMultiPartRequest.

@Test
public void testBadMultiPartRequest() throws Exception {
    String boundary = "X0Y0";
    String str = "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"fileup\"; filename=\"test.upload\"\r\n" + "Content-Type: application/octet-stream\r\n\r\n" + "How now brown cow." + "\r\n--" + boundary + "-\r\n\r\n";
    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);
    try {
        mpis.getParts();
        fail("Multipart incomplete");
    } catch (IOException e) {
        assertTrue(e.getMessage().startsWith("Incomplete"));
    }
}
Also used : MultipartConfigElement(javax.servlet.MultipartConfigElement) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) Test(org.junit.Test)

Example 12 with MultipartConfigElement

use of javax.servlet.MultipartConfigElement in project jetty.project by eclipse.

the class MultiPartInputStreamTest method testBodyAlreadyConsumed.

@Test
public void testBodyAlreadyConsumed() throws Exception {
    ServletInputStream is = new ServletInputStream() {

        @Override
        public boolean isFinished() {
            return true;
        }

        @Override
        public boolean isReady() {
            return false;
        }

        @Override
        public void setReadListener(ReadListener readListener) {
        }

        @Override
        public int read() throws IOException {
            return 0;
        }
    };
    MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 3072, 50);
    MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(is, _contentType, config, _tmpDir);
    mpis.setDeleteOnExit(true);
    Collection<Part> parts = mpis.getParts();
    assertEquals(0, parts.size());
}
Also used : MultipartConfigElement(javax.servlet.MultipartConfigElement) ServletInputStream(javax.servlet.ServletInputStream) MultiPart(org.eclipse.jetty.util.MultiPartInputStreamParser.MultiPart) Part(javax.servlet.http.Part) ReadListener(javax.servlet.ReadListener) Test(org.junit.Test)

Example 13 with MultipartConfigElement

use of javax.servlet.MultipartConfigElement in project jetty.project by eclipse.

the class MultiPartInputStreamTest method testEmpty.

@Test
public void testEmpty() throws Exception {
    String delimiter = "\r\n";
    final String boundary = "MockMultiPartTestBoundary";
    String str = 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);
    assertTrue(mpis.getParts().isEmpty());
}
Also used : MultipartConfigElement(javax.servlet.MultipartConfigElement) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 14 with MultipartConfigElement

use of javax.servlet.MultipartConfigElement in project jetty.project by eclipse.

the class MultiPartInputStreamTest method testPartTmpFileDeletion.

@Test
public void testPartTmpFileDeletion() 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());
    assertThat(stuff.exists(), is(true));
    part.cleanUp();
    //tmp file was removed after cleanup
    assertThat(stuff.exists(), is(false));
}
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 15 with MultipartConfigElement

use of javax.servlet.MultipartConfigElement in project jetty.project by eclipse.

the class MultiPartInputStreamTest method testCROnlyRequest.

@Test
public void testCROnlyRequest() throws Exception {
    String str = "--AaB03x\r" + "content-disposition: form-data; name=\"field1\"\r" + "\r" + "Joe Blow\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));
    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)

Aggregations

MultipartConfigElement (javax.servlet.MultipartConfigElement)51 Test (org.junit.Test)35 ByteArrayInputStream (java.io.ByteArrayInputStream)30 Part (javax.servlet.http.Part)27 MultiPart (org.eclipse.jetty.util.MultiPartInputStreamParser.MultiPart)24 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13 IOException (java.io.IOException)8 File (java.io.File)7 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)5 ServletException (javax.servlet.ServletException)4 ServletInputStream (javax.servlet.ServletInputStream)4 InputStream (java.io.InputStream)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Servlet (javax.servlet.Servlet)2 ServletRegistration (javax.servlet.ServletRegistration)2 MultipartConfig (javax.servlet.annotation.MultipartConfig)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 MultiPartInputStreamParser (org.eclipse.jetty.util.MultiPartInputStreamParser)2 DbxException (com.dropbox.core.DbxException)1