ppkarwasz commented on code in PR #481:
URL:
https://github.com/apache/commons-fileupload/pull/481#discussion_r3652594812
##########
commons-fileupload2-jakarta-servlet5/src/test/java/org/apache/commons/fileupload2/jakarta/servlet5/JakartaServletFileUploadGetItemIteratorTest.java:
##########
@@ -126,6 +127,43 @@ void testEmptyMultipartBody() throws Exception {
assertFalse(iter.hasNext(), "Expected no items in an empty multipart
body");
}
+ /**
+ * When the number of uploaded files exceeds {@code maxFileCount},
iterating must throw a {@link FileUploadFileCountLimitException} with the
expected
+ * permitted count.
+ */
+ @Test
+ void testFileCountLimitExceededThrowsException() throws Exception {
+ final long maxFileCount = 2;
+ final var body = buildMultiFileParts((int) maxFileCount + 1);
+ final HttpServletRequest request = new
JakartaMockHttpServletRequest(body, CONTENT_TYPE);
+ final var upload = newUpload();
+ upload.setMaxFileCount(maxFileCount);
+ final FileItemInputIterator iter = upload.getItemIterator(request);
+ // Consume allowed items first
+ for (int i = 0; i < maxFileCount; i++) {
+ assertTrue(iter.hasNext());
+ iter.next();
+ }
+ // The next hasNext() call triggers the limit check
+ final var ex = assertThrows(FileUploadFileCountLimitException.class,
iter::hasNext);
+ assertEquals(maxFileCount, ex.getPermitted());
+ }
+
+ /**
+ * A file-count limit of 1 allows exactly one file; attempting to move to
the second must throw {@link FileUploadFileCountLimitException}.
+ */
+ @Test
+ void testFileCountLimitOfOneThrowsOnSecondFile() throws Exception {
+ final var body = buildMultiFileParts(2);
+ final HttpServletRequest request = new
JakartaMockHttpServletRequest(body, CONTENT_TYPE);
+ final var upload = newUpload();
+ upload.setMaxFileCount(1L);
+ final FileItemInputIterator iter = upload.getItemIterator(request);
+ assertTrue(iter.hasNext());
+ iter.next(); // consume the first (allowed) item
+ assertThrows(FileUploadFileCountLimitException.class, iter::hasNext);
+ }
Review Comment:
Sorry, I meant `multipart/related`.
I didn't run the tests with coverage, but I believe line 175 is not covered
by any new test.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]