Search in sources :

Example 6 with MultiPart

use of org.eclipse.jetty.util.MultiPartInputStreamParser.MultiPart in project jetty.project by eclipse.

the class MultiPartInputStreamTest method testFileTooBig.

@Test
public void testFileTooBig() 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 {
        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)

Example 7 with MultiPart

use of org.eclipse.jetty.util.MultiPartInputStreamParser.MultiPart in project jetty.project by eclipse.

the class MultiPartInputStreamTest method testNoBoundaryRequest.

@Test
public void testNoBoundaryRequest() throws Exception {
    String str = "--\r\n" + "Content-Disposition: form-data; name=\"fileName\"\r\n" + "Content-Type: text/plain; charset=US-ASCII\r\n" + "Content-Transfer-Encoding: 8bit\r\n" + "\r\n" + "abc\r\n" + "--\r\n" + "Content-Disposition: form-data; name=\"desc\"\r\n" + "Content-Type: text/plain; charset=US-ASCII\r\n" + "Content-Transfer-Encoding: 8bit\r\n" + "\r\n" + "123\r\n" + "--\r\n" + "Content-Disposition: form-data; name=\"title\"\r\n" + "Content-Type: text/plain; charset=US-ASCII\r\n" + "Content-Transfer-Encoding: 8bit\r\n" + "\r\n" + "ttt\r\n" + "--\r\n" + "Content-Disposition: form-data; name=\"datafile5239138112980980385.txt\"; filename=\"datafile5239138112980980385.txt\"\r\n" + "Content-Type: application/octet-stream; charset=ISO-8859-1\r\n" + "Content-Transfer-Encoding: binary\r\n" + "\r\n" + "000\r\n" + "----\r\n";
    MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 3072, 50);
    MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(str.getBytes()), "multipart/form-data", config, _tmpDir);
    mpis.setDeleteOnExit(true);
    Collection<Part> parts = mpis.getParts();
    assertThat(parts.size(), is(4));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Part fileName = mpis.getPart("fileName");
    assertThat(fileName, notNullValue());
    assertThat(fileName.getSize(), is(3L));
    IO.copy(fileName.getInputStream(), baos);
    assertThat(baos.toString("US-ASCII"), is("abc"));
    baos = new ByteArrayOutputStream();
    Part desc = mpis.getPart("desc");
    assertThat(desc, notNullValue());
    assertThat(desc.getSize(), is(3L));
    IO.copy(desc.getInputStream(), baos);
    assertThat(baos.toString("US-ASCII"), is("123"));
    baos = new ByteArrayOutputStream();
    Part title = mpis.getPart("title");
    assertThat(title, notNullValue());
    assertThat(title.getSize(), is(3L));
    IO.copy(title.getInputStream(), baos);
    assertThat(baos.toString("US-ASCII"), is("ttt"));
}
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 8 with MultiPart

use of org.eclipse.jetty.util.MultiPartInputStreamParser.MultiPart in project jetty.project by eclipse.

the class MultiPartInputStreamTest method testCharsetEncoding.

@Test
public void testCharsetEncoding() throws Exception {
    String contentType = "multipart/form-data; boundary=TheBoundary; charset=ISO-8859-1";
    String str = "--TheBoundary\r" + "content-disposition: form-data; name=\"field1\"\r" + "\r" + "\nJoe Blow\n" + "\r" + "--TheBoundary--\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(1));
}
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

ByteArrayInputStream (java.io.ByteArrayInputStream)8 MultipartConfigElement (javax.servlet.MultipartConfigElement)8 Part (javax.servlet.http.Part)8 MultiPart (org.eclipse.jetty.util.MultiPartInputStreamParser.MultiPart)8 Test (org.junit.Test)7 File (java.io.File)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ServletException (javax.servlet.ServletException)1 ServletInputStream (javax.servlet.ServletInputStream)1