Detect all common size exceptions from Tomcat and Commons FileUpload 2.x

Closes gh-36317
This commit is contained in:
Juergen Hoeller
2026-02-13 16:19:46 +01:00
parent 9a002a77b2
commit e8e24e65d2
2 changed files with 72 additions and 3 deletions
@@ -123,6 +123,47 @@ class StandardMultipartHttpServletRequestTests {
.isThrownBy(() -> requestWithException(ex)).withCause(ex);
}
@Test // gh-32549
void undertowRequestTooBigException() {
IOException ex = new IOException("Connection terminated as request was larger than 10000");
assertThatExceptionOfType(MaxUploadSizeExceededException.class)
.isThrownBy(() -> requestWithException(ex)).withCause(ex);
}
@Test // gh-36317: Tomcat's Commons FileUpload
void commonsFileSizeLimitExceededException() {
IOException ex = new FileSizeLimitExceededException();
assertThatExceptionOfType(MaxUploadSizeExceededException.class)
.isThrownBy(() -> requestWithException(ex)).withCause(ex);
}
@Test // gh-36317: Tomcat's Commons FileUpload
void commonsFileCountLimitExceededException() {
IOException ex = new FileCountLimitExceededException();
assertThatExceptionOfType(MaxUploadSizeExceededException.class)
.isThrownBy(() -> requestWithException(ex)).withCause(ex);
}
@Test // gh-36317: Commons FileUpload 2.x
void commonsFileUploadByteCountLimitException() {
IOException ex = new FileUploadByteCountLimitException();
assertThatExceptionOfType(MaxUploadSizeExceededException.class)
.isThrownBy(() -> requestWithException(ex)).withCause(ex);
}
@Test // gh-36317: Commons FileUpload 2.x
void commonsFileUploadFileCountLimitException() {
IOException ex = new FileUploadFileCountLimitException();
assertThatExceptionOfType(MaxUploadSizeExceededException.class)
.isThrownBy(() -> requestWithException(ex)).withCause(ex);
}
private static StandardMultipartHttpServletRequest requestWithPart(String name, String disposition, String content) {
MockHttpServletRequest request = new MockHttpServletRequest();
MockPart part = new MockPart(name, null, content.getBytes(StandardCharsets.UTF_8));
@@ -141,4 +182,31 @@ class StandardMultipartHttpServletRequestTests {
return new StandardMultipartHttpServletRequest(request);
}
private static StandardMultipartHttpServletRequest requestWithException(IOException ex) {
MockHttpServletRequest request = new MockHttpServletRequest() {
@Override
public Collection<Part> getParts() throws IOException {
throw ex;
}
};
return new StandardMultipartHttpServletRequest(request);
}
@SuppressWarnings("serial")
private static class FileSizeLimitExceededException extends IOException {
}
@SuppressWarnings("serial")
private static class FileCountLimitExceededException extends IOException {
}
@SuppressWarnings("serial")
private static class FileUploadByteCountLimitException extends IOException {
}
@SuppressWarnings("serial")
private static class FileUploadFileCountLimitException extends IOException {
}
}