Code cleanup.
Changed the way the test directory was created: it was based on a millisecond timestamp, but the tests run fast and it was possible that two tests were creating the directory within the same millisecond. Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
This commit is contained in:
parent
ed7f337856
commit
01438e6f91
|
@ -25,13 +25,14 @@ import java.io.IOException;
|
|||
import java.io.InputStream;
|
||||
import java.util.Base64;
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import javax.servlet.MultipartConfigElement;
|
||||
import javax.servlet.ReadListener;
|
||||
import javax.servlet.ServletInputStream;
|
||||
import javax.servlet.http.Part;
|
||||
|
||||
import org.eclipse.jetty.http.MultiPartFormInputStream.MultiPart;
|
||||
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
|
||||
import org.eclipse.jetty.util.IO;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -51,17 +52,14 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
|
|||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
/**
|
||||
* MultiPartInputStreamTest
|
||||
*/
|
||||
public class MultiPartFormInputStreamTest
|
||||
{
|
||||
private static final AtomicInteger testCounter = new AtomicInteger();
|
||||
private static final String FILENAME = "stuff.txt";
|
||||
protected String _contentType = "multipart/form-data, boundary=AaB03x";
|
||||
protected String _multi = createMultipartRequestString(FILENAME);
|
||||
// TODO: move to testing dir concept
|
||||
protected String _dirname = System.getProperty("java.io.tmpdir") + File.separator + "myfiles-" + TimeUnit.NANOSECONDS.toMillis(System.nanoTime());
|
||||
protected File _tmpDir = new File(_dirname);
|
||||
protected File _tmpDir = MavenTestingUtils.getTargetTestingDir(String.valueOf(testCounter.incrementAndGet()));
|
||||
protected String _dirname = _tmpDir.getAbsolutePath();
|
||||
|
||||
public MultiPartFormInputStreamTest()
|
||||
{
|
||||
|
@ -70,7 +68,6 @@ public class MultiPartFormInputStreamTest
|
|||
|
||||
@Test
|
||||
public void testBadMultiPartRequest()
|
||||
throws Exception
|
||||
{
|
||||
String boundary = "X0Y0";
|
||||
String str = "--" + boundary + "\r\n" +
|
||||
|
@ -89,14 +86,13 @@ public class MultiPartFormInputStreamTest
|
|||
mpis.setDeleteOnExit(true);
|
||||
|
||||
IOException x = assertThrows(IOException.class,
|
||||
() -> mpis.getParts(),
|
||||
mpis::getParts,
|
||||
"Incomplete Multipart");
|
||||
assertThat(x.getMessage(), startsWith("Incomplete"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFinalBoundaryOnly()
|
||||
throws Exception
|
||||
public void testFinalBoundaryOnly() throws Exception
|
||||
{
|
||||
String delimiter = "\r\n";
|
||||
final String boundary = "MockMultiPartTestBoundary";
|
||||
|
@ -119,8 +115,7 @@ public class MultiPartFormInputStreamTest
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testEmpty()
|
||||
throws Exception
|
||||
public void testEmpty() throws Exception
|
||||
{
|
||||
String delimiter = "\r\n";
|
||||
final String boundary = "MockMultiPartTestBoundary";
|
||||
|
@ -139,8 +134,7 @@ public class MultiPartFormInputStreamTest
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testNoBoundaryRequest()
|
||||
throws Exception
|
||||
public void testNoBoundaryRequest() throws Exception
|
||||
{
|
||||
String str = "--\r\n" +
|
||||
"Content-Disposition: form-data; name=\"fileName\"\r\n" +
|
||||
|
@ -200,8 +194,7 @@ public class MultiPartFormInputStreamTest
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testNonMultiPartRequest()
|
||||
throws Exception
|
||||
public void testNonMultiPartRequest() throws Exception
|
||||
{
|
||||
MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 3072, 50);
|
||||
MultiPartFormInputStream mpis = new MultiPartFormInputStream(new ByteArrayInputStream(_multi.getBytes()),
|
||||
|
@ -214,7 +207,6 @@ public class MultiPartFormInputStreamTest
|
|||
|
||||
@Test
|
||||
public void testNoBody()
|
||||
throws Exception
|
||||
{
|
||||
String body = "";
|
||||
|
||||
|
@ -225,13 +217,12 @@ public class MultiPartFormInputStreamTest
|
|||
_tmpDir);
|
||||
mpis.setDeleteOnExit(true);
|
||||
|
||||
IOException x = assertThrows(IOException.class, () -> mpis.getParts());
|
||||
IOException x = assertThrows(IOException.class, mpis::getParts);
|
||||
assertThat(x.getMessage(), containsString("Missing initial multi part boundary"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBodyAlreadyConsumed()
|
||||
throws Exception
|
||||
public void testBodyAlreadyConsumed() throws Exception
|
||||
{
|
||||
ServletInputStream is = new ServletInputStream()
|
||||
{
|
||||
|
@ -272,7 +263,6 @@ public class MultiPartFormInputStreamTest
|
|||
|
||||
@Test
|
||||
public void testWhitespaceBodyWithCRLF()
|
||||
throws Exception
|
||||
{
|
||||
String whitespace = " \n\n\n\r\n\r\n\r\n\r\n";
|
||||
|
||||
|
@ -282,13 +272,12 @@ public class MultiPartFormInputStreamTest
|
|||
config,
|
||||
_tmpDir);
|
||||
mpis.setDeleteOnExit(true);
|
||||
IOException x = assertThrows(IOException.class, () -> mpis.getParts());
|
||||
IOException x = assertThrows(IOException.class, mpis::getParts);
|
||||
assertThat(x.getMessage(), containsString("Missing initial multi part boundary"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWhitespaceBody()
|
||||
throws Exception
|
||||
{
|
||||
String whitespace = " ";
|
||||
|
||||
|
@ -298,13 +287,12 @@ public class MultiPartFormInputStreamTest
|
|||
config,
|
||||
_tmpDir);
|
||||
mpis.setDeleteOnExit(true);
|
||||
IOException x = assertThrows(IOException.class, () -> mpis.getParts());
|
||||
IOException x = assertThrows(IOException.class, mpis::getParts);
|
||||
assertThat(x.getMessage(), containsString("Missing initial"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLeadingWhitespaceBodyWithCRLF()
|
||||
throws Exception
|
||||
public void testLeadingWhitespaceBodyWithCRLF() throws Exception
|
||||
{
|
||||
String body = " \n\n\n\r\n\r\n\r\n\r\n" +
|
||||
"--AaB03x\r\n" +
|
||||
|
@ -380,8 +368,7 @@ public class MultiPartFormInputStreamTest
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testNoLimits()
|
||||
throws Exception
|
||||
public void testNoLimits() throws Exception
|
||||
{
|
||||
MultipartConfigElement config = new MultipartConfigElement(_dirname);
|
||||
MultiPartFormInputStream mpis = new MultiPartFormInputStream(new ByteArrayInputStream(_multi.getBytes()),
|
||||
|
@ -395,7 +382,6 @@ public class MultiPartFormInputStreamTest
|
|||
|
||||
@Test
|
||||
public void testRequestTooBig()
|
||||
throws Exception
|
||||
{
|
||||
MultipartConfigElement config = new MultipartConfigElement(_dirname, 60, 100, 50);
|
||||
MultiPartFormInputStream mpis = new MultiPartFormInputStream(new ByteArrayInputStream(_multi.getBytes()),
|
||||
|
@ -404,13 +390,12 @@ public class MultiPartFormInputStreamTest
|
|||
_tmpDir);
|
||||
mpis.setDeleteOnExit(true);
|
||||
|
||||
IllegalStateException x = assertThrows(IllegalStateException.class, () -> mpis.getParts());
|
||||
IllegalStateException x = assertThrows(IllegalStateException.class, mpis::getParts);
|
||||
assertThat(x.getMessage(), containsString("Request exceeds maxRequestSize"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRequestTooBigThrowsErrorOnGetParts()
|
||||
throws Exception
|
||||
{
|
||||
MultipartConfigElement config = new MultipartConfigElement(_dirname, 60, 100, 50);
|
||||
MultiPartFormInputStream mpis = new MultiPartFormInputStream(new ByteArrayInputStream(_multi.getBytes()),
|
||||
|
@ -420,17 +405,16 @@ public class MultiPartFormInputStreamTest
|
|||
mpis.setDeleteOnExit(true);
|
||||
|
||||
//cause parsing
|
||||
IllegalStateException x = assertThrows(IllegalStateException.class, () -> mpis.getParts());
|
||||
IllegalStateException x = assertThrows(IllegalStateException.class, mpis::getParts);
|
||||
assertThat(x.getMessage(), containsString("Request exceeds maxRequestSize"));
|
||||
|
||||
//try again
|
||||
x = assertThrows(IllegalStateException.class, () -> mpis.getParts());
|
||||
x = assertThrows(IllegalStateException.class, mpis::getParts);
|
||||
assertThat(x.getMessage(), containsString("Request exceeds maxRequestSize"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFileTooBig()
|
||||
throws Exception
|
||||
{
|
||||
MultipartConfigElement config = new MultipartConfigElement(_dirname, 40, 1024, 30);
|
||||
MultiPartFormInputStream mpis = new MultiPartFormInputStream(new ByteArrayInputStream(_multi.getBytes()),
|
||||
|
@ -439,14 +423,13 @@ public class MultiPartFormInputStreamTest
|
|||
_tmpDir);
|
||||
mpis.setDeleteOnExit(true);
|
||||
IllegalStateException x = assertThrows(IllegalStateException.class,
|
||||
() -> mpis.getParts(),
|
||||
mpis::getParts,
|
||||
"stuff.txt should have been larger than maxFileSize");
|
||||
assertThat(x.getMessage(), startsWith("Multipart Mime part"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFileTooBigThrowsErrorOnGetParts()
|
||||
throws Exception
|
||||
{
|
||||
MultipartConfigElement config = new MultipartConfigElement(_dirname, 40, 1024, 30);
|
||||
MultiPartFormInputStream mpis = new MultiPartFormInputStream(new ByteArrayInputStream(_multi.getBytes()),
|
||||
|
@ -456,13 +439,13 @@ public class MultiPartFormInputStreamTest
|
|||
mpis.setDeleteOnExit(true);
|
||||
// Caused parsing
|
||||
IllegalStateException x = assertThrows(IllegalStateException.class,
|
||||
() -> mpis.getParts(),
|
||||
mpis::getParts,
|
||||
"stuff.txt should have been larger than maxFileSize");
|
||||
assertThat(x.getMessage(), startsWith("Multipart Mime part"));
|
||||
|
||||
//test again after the parsing
|
||||
x = assertThrows(IllegalStateException.class,
|
||||
() -> mpis.getParts(),
|
||||
mpis::getParts,
|
||||
"stuff.txt should have been larger than maxFileSize");
|
||||
assertThat(x.getMessage(), startsWith("Multipart Mime part"));
|
||||
}
|
||||
|
@ -520,8 +503,7 @@ public class MultiPartFormInputStreamTest
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testLFOnlyRequest()
|
||||
throws Exception
|
||||
public void testLFOnlyRequest() throws Exception
|
||||
{
|
||||
String str = "--AaB03x\n" +
|
||||
"content-disposition: form-data; name=\"field1\"\n" +
|
||||
|
@ -556,7 +538,6 @@ public class MultiPartFormInputStreamTest
|
|||
|
||||
@Test
|
||||
public void testCROnlyRequest()
|
||||
throws Exception
|
||||
{
|
||||
String str = "--AaB03x\r" +
|
||||
"content-disposition: form-data; name=\"field1\"\r" +
|
||||
|
@ -576,14 +557,13 @@ public class MultiPartFormInputStreamTest
|
|||
mpis.setDeleteOnExit(true);
|
||||
|
||||
IllegalStateException x = assertThrows(IllegalStateException.class,
|
||||
() -> mpis.getParts(),
|
||||
mpis::getParts,
|
||||
"Improper EOL");
|
||||
assertThat(x.getMessage(), containsString("Bad EOL"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCRandLFMixRequest()
|
||||
throws Exception
|
||||
{
|
||||
String str = "--AaB03x\r" +
|
||||
"content-disposition: form-data; name=\"field1\"\r" +
|
||||
|
@ -604,7 +584,7 @@ public class MultiPartFormInputStreamTest
|
|||
mpis.setDeleteOnExit(true);
|
||||
|
||||
IllegalStateException x = assertThrows(IllegalStateException.class,
|
||||
() -> mpis.getParts(),
|
||||
mpis::getParts,
|
||||
"Improper EOL");
|
||||
assertThat(x.getMessage(), containsString("Bad EOL"));
|
||||
}
|
||||
|
@ -626,7 +606,7 @@ public class MultiPartFormInputStreamTest
|
|||
_tmpDir);
|
||||
mpis.setDeleteOnExit(true);
|
||||
IllegalStateException x = assertThrows(IllegalStateException.class,
|
||||
() -> mpis.getParts(),
|
||||
mpis::getParts,
|
||||
"Header Line Exceeded Max Length");
|
||||
assertThat(x.getMessage(), containsString("Header Line Exceeded Max Length"));
|
||||
}
|
||||
|
@ -724,8 +704,7 @@ public class MultiPartFormInputStreamTest
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testWriteFilesIfContentDispositionFilename()
|
||||
throws Exception
|
||||
public void testWriteFilesIfContentDispositionFilename() throws Exception
|
||||
{
|
||||
String s = "--AaB03x\r\n" +
|
||||
"content-disposition: form-data; name=\"field1\"; filename=\"frooble.txt\"\r\n" +
|
||||
|
@ -823,8 +802,7 @@ public class MultiPartFormInputStreamTest
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testMultiSameNames()
|
||||
throws Exception
|
||||
public void testMultiSameNames() throws Exception
|
||||
{
|
||||
String sameNames = "--AaB03x\r\n" +
|
||||
"content-disposition: form-data; name=\"stuff\"; filename=\"stuff1.txt\"\r\n" +
|
||||
|
@ -946,8 +924,7 @@ public class MultiPartFormInputStreamTest
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testGeneratedForm()
|
||||
throws Exception
|
||||
public void testGeneratedForm() throws Exception
|
||||
{
|
||||
String contentType = "multipart/form-data, boundary=WebKitFormBoundary7MA4YWf7OaKlSxkTrZu0gW";
|
||||
String body = "Content-Type: multipart/form-data; boundary=WebKitFormBoundary7MA4YWf7OaKlSxkTrZu0gW\r\n" +
|
||||
|
|
Loading…
Reference in New Issue