Merge remote-tracking branch 'eclipse/jetty-9.4.x-1027-Multipart' into jetty-9.4.x-1027-Multipart

This commit is contained in:
Lachlan Roberts 2018-03-27 10:18:42 +11:00
commit 0bf253bbbf
1 changed files with 83 additions and 40 deletions

View File

@ -35,9 +35,11 @@ import java.security.DigestOutputStream;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Objects; import java.util.Objects;
import java.util.function.Function;
import javax.servlet.MultipartConfigElement; import javax.servlet.MultipartConfigElement;
import javax.servlet.http.Part; import javax.servlet.http.Part;
@ -45,13 +47,16 @@ import javax.servlet.http.Part;
import org.eclipse.jetty.toolchain.test.Hex; import org.eclipse.jetty.toolchain.test.Hex;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils; import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.toolchain.test.TestingDir; import org.eclipse.jetty.toolchain.test.TestingDir;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.IO; import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.QuotedStringTokenizer; import org.eclipse.jetty.util.QuotedStringTokenizer;
import org.eclipse.jetty.util.StringUtil; import org.eclipse.jetty.util.StringUtil;
import org.hamcrest.Matchers;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.Parameterized; import org.junit.runners.Parameterized;
import org.openjdk.jmh.runner.RunnerException;
@RunWith(Parameterized.class) @RunWith(Parameterized.class)
public class MultiPartParsingTest public class MultiPartParsingTest
@ -151,25 +156,63 @@ public class MultiPartParsingTest
} }
@Test @Test
public void testParse() throws IOException, NoSuchAlgorithmException public void testUtilParse() throws Exception
{
Path outputDir = testingDir.getEmptyPathDir();
MultipartConfigElement config = newMultipartConfigElement(outputDir);
try (InputStream in = Files.newInputStream(multipartRawFile))
{
org.eclipse.jetty.util.MultiPartInputStreamParser parser = new org.eclipse.jetty.util.MultiPartInputStreamParser(in,multipartExpectations.contentType,config,outputDir.toFile());
checkParts(parser.getParts(),s->
{
try
{
return parser.getPart(s);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
});
}
}
@Test
public void testHttpParse() throws Exception
{ {
Path outputDir = testingDir.getEmptyPathDir(); Path outputDir = testingDir.getEmptyPathDir();
MultipartConfigElement config = newMultipartConfigElement(outputDir); MultipartConfigElement config = newMultipartConfigElement(outputDir);
try (InputStream in = Files.newInputStream(multipartRawFile)) try (InputStream in = Files.newInputStream(multipartRawFile))
{ {
MultiPartInputStreamParser parser = new MultiPartInputStreamParser(in, multipartExpectations.contentType, config, outputDir.toFile()); MultiPartInputStreamParser parser = new MultiPartInputStreamParser(in, multipartExpectations.contentType, config, outputDir.toFile());
parser.parse();
checkParts(parser.getParts(),s->
{
try
{
return parser.getPart(s);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
});
}
}
private void checkParts(Collection<Part> parts, Function<String, Part> getPart) throws Exception
{
// Evaluate Count // Evaluate Count
if (multipartExpectations.partCount >= 0) if (multipartExpectations.partCount >= 0)
{ {
assertThat("Mulitpart.parts.size", parser.getParts().size(), is(multipartExpectations.partCount)); assertThat("Mulitpart.parts.size", parts.size(), is(multipartExpectations.partCount));
} }
// Evaluate expected Contents // Evaluate expected Contents
for (NameValue expected : multipartExpectations.partContainsContents) for (NameValue expected : multipartExpectations.partContainsContents)
{ {
Part part = parser.getPart(expected.name); Part part = getPart.apply(expected.name);
assertThat("Part[" + expected.name + "]", part, is(notNullValue())); assertThat("Part[" + expected.name + "]", part, is(notNullValue()));
try (InputStream partInputStream = part.getInputStream()) try (InputStream partInputStream = part.getInputStream())
{ {
@ -182,7 +225,7 @@ public class MultiPartParsingTest
// Evaluate expected filenames // Evaluate expected filenames
for (NameValue expected : multipartExpectations.partFilenames) for (NameValue expected : multipartExpectations.partFilenames)
{ {
Part part = parser.getPart(expected.name); Part part = getPart.apply(expected.name);
assertThat("Part[" + expected.name + "]", part, is(notNullValue())); assertThat("Part[" + expected.name + "]", part, is(notNullValue()));
assertThat("Part[" + expected.name + "]", part.getSubmittedFileName(), is(expected.value)); assertThat("Part[" + expected.name + "]", part.getSubmittedFileName(), is(expected.value));
} }
@ -190,8 +233,9 @@ public class MultiPartParsingTest
// Evaluate expected contents checksums // Evaluate expected contents checksums
for (NameValue expected : multipartExpectations.partSha1sums) for (NameValue expected : multipartExpectations.partSha1sums)
{ {
Part part = parser.getPart(expected.name); Part part = getPart.apply(expected.name);
assertThat("Part[" + expected.name + "]", part, is(notNullValue())); assertThat("Part[" + expected.name + "]", part, is(notNullValue()));
// System.err.println(BufferUtil.toDetailString(BufferUtil.toBuffer(IO.readBytes(part.getInputStream()))));
MessageDigest digest = MessageDigest.getInstance("SHA1"); MessageDigest digest = MessageDigest.getInstance("SHA1");
try (InputStream partInputStream = part.getInputStream(); try (InputStream partInputStream = part.getInputStream();
NoOpOutputStream noop = new NoOpOutputStream(); NoOpOutputStream noop = new NoOpOutputStream();
@ -199,8 +243,7 @@ public class MultiPartParsingTest
{ {
IO.copy(partInputStream, digester); IO.copy(partInputStream, digester);
String actualSha1sum = Hex.asHex(digest.digest()).toLowerCase(Locale.US); String actualSha1sum = Hex.asHex(digest.digest()).toLowerCase(Locale.US);
assertThat("Part[" + expected.name + "].sha1sum", actualSha1sum, containsString(expected.value)); assertThat("Part[" + expected.name + "].sha1sum", actualSha1sum, Matchers.equalToIgnoringCase(expected.value));
}
} }
} }
} }