garydgregory commented on code in PR #481:
URL: 
https://github.com/apache/commons-fileupload/pull/481#discussion_r3652551714


##########
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:
   Hi @ppkarwasz 
   
   Thank you for your review.
   
   I updated the tests in commit 01f472aebcb18c724cee8a884c297d780001eecc to 
use `@ParameterizedTest`.
   
   How does `multipart/alternative` make sense in the context of uploading 
files?



-- 
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]

Reply via email to