Search in sources :

Example 26 with MultipartConfigElement

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

the class MultiPartInputStreamTest method testLeadingWhitespaceBodyWithCRLF.

@Test
public void testLeadingWhitespaceBodyWithCRLF() throws Exception {
    String body = "              \n\n\n\r\n\r\n\r\n\r\n" + "--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("aaaa"));
}
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 27 with MultipartConfigElement

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

the class MultiPartInputStreamTest method testRequestTooBig.

@Test
public void testRequestTooBig() throws Exception {
    MultipartConfigElement config = new MultipartConfigElement(_dirname, 60, 100, 50);
    MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(_multi.getBytes()), _contentType, config, _tmpDir);
    mpis.setDeleteOnExit(true);
    Collection<Part> parts = null;
    try {
        parts = mpis.getParts();
        fail("Request should have exceeded maxRequestSize");
    } catch (IllegalStateException e) {
        assertTrue(e.getMessage().startsWith("Request exceeds maxRequestSize"));
    }
}
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 28 with MultipartConfigElement

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

the class MultiPartInputStreamTest method testNoBody.

@Test
public void testNoBody() throws Exception {
    String body = "";
    MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 3072, 50);
    MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(body.getBytes()), _contentType, config, _tmpDir);
    mpis.setDeleteOnExit(true);
    try {
        mpis.getParts();
        fail("Multipart missing body");
    } catch (IOException e) {
        assertTrue(e.getMessage().startsWith("Missing content"));
    }
}
Also used : MultipartConfigElement(javax.servlet.MultipartConfigElement) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) Test(org.junit.Test)

Example 29 with MultipartConfigElement

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

the class MultiPartInputStreamTest method testNoLimits.

@Test
public void testNoLimits() throws Exception {
    MultipartConfigElement config = new MultipartConfigElement(_dirname);
    MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(_multi.getBytes()), _contentType, config, _tmpDir);
    mpis.setDeleteOnExit(true);
    Collection<Part> parts = mpis.getParts();
    assertFalse(parts.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 30 with MultipartConfigElement

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

the class MultiPartInputStreamTest method testFileTooBigThrowsErrorOnGetParts.

@Test
public void testFileTooBigThrowsErrorOnGetParts() throws Exception {
    MultipartConfigElement config = new MultipartConfigElement(_dirname, 40, 1024, 30);
    MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(_multi.getBytes()), _contentType, config, _tmpDir);
    mpis.setDeleteOnExit(true);
    Collection<Part> parts = null;
    try {
        //caused parsing
        parts = mpis.getParts();
        fail("stuff.txt should have been larger than maxFileSize");
    } catch (IllegalStateException e) {
        assertTrue(e.getMessage().startsWith("Multipart Mime part"));
    }
    //test again after the parsing
    try {
        //caused parsing
        parts = mpis.getParts();
        fail("stuff.txt should have been larger than maxFileSize");
    } catch (IllegalStateException e) {
        assertTrue(e.getMessage().startsWith("Multipart Mime part"));
    }
}
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)

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