diff --git a/plume-web-jersey/src/test/java/com/coreoz/plume/jersey/control/ContentSizeLimitTest.java b/plume-web-jersey/src/test/java/com/coreoz/plume/jersey/control/ContentSizeLimitTest.java index c98db3d..58baafc 100644 --- a/plume-web-jersey/src/test/java/com/coreoz/plume/jersey/control/ContentSizeLimitTest.java +++ b/plume-web-jersey/src/test/java/com/coreoz/plume/jersey/control/ContentSizeLimitTest.java @@ -2,19 +2,17 @@ import org.junit.Test; -import jakarta.ws.rs.WebApplicationException; import jakarta.ws.rs.client.Entity; import jakarta.ws.rs.core.Application; +import jakarta.ws.rs.core.HttpHeaders; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.client.Invocation.Builder; -import org.glassfish.jersey.client.ClientConfig; import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.test.JerseyTest; import static org.junit.Assert.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; import com.coreoz.plume.jersey.security.control.ContentControlFeature; @@ -35,7 +33,7 @@ public void checkContentSize_withBody_whenWithinDefaultLimit_shouldReturn200() { } @Test - public void checkContentSize_withBody_whenBeyondDefaultLimit_shouldThrow() { + public void checkContentSize_withBody_whenBeyondDefaultLimit_shouldReturn413() { // Generate a byte array of ContentControlFeature.DEFAULT_MAX_SIZE + 1 byte[] data = new byte[ContentControlFeature.DEFAULT_MAX_SIZE + 1]; Builder request = target("/test/upload-default").request(); @@ -43,6 +41,14 @@ public void checkContentSize_withBody_whenBeyondDefaultLimit_shouldThrow() { assertEquals(Response.Status.REQUEST_ENTITY_TOO_LARGE.getStatusCode(), request.post(entity).getStatus()); } + @Test + public void checkContentSize_withBody_whenContentLengthIsWrong_shouldReturn411() { + // Generate a byte array of ContentControlFeature.DEFAULT_MAX_SIZE + 1 + Builder request = target("/test/upload-default").request(); + request.header(HttpHeaders.CONTENT_LENGTH, null); + assertEquals(Response.Status.LENGTH_REQUIRED.getStatusCode(), request.post(null).getStatus()); + } + @Test public void checkContentSize_withBody_whenWithinCustomLimit_shouldReturn200() { byte[] data = "12345".getBytes(); @@ -51,7 +57,7 @@ public void checkContentSize_withBody_whenWithinCustomLimit_shouldReturn200() { } @Test - public void checkContentSize_withBody_whenBeyondCustomLimit_shouldThrow() { + public void checkContentSize_withBody_whenBeyondCustomLimit_shouldReturn413() { // Generate a byte array of CUSTOM_MAX_SIZE + 1 byte[] data = new byte[TestContentSizeResource.CUSTOM_MAX_SIZE + 1]; Builder request = target("/test/upload-custom").request(); @@ -59,4 +65,6 @@ public void checkContentSize_withBody_whenBeyondCustomLimit_shouldThrow() { assertEquals(Response.Status.REQUEST_ENTITY_TOO_LARGE.getStatusCode(), request.post(entity).getStatus()); } + + } diff --git a/plume-web-jersey/src/test/java/com/coreoz/plume/jersey/control/SizeLimitingInputStreamTest.java b/plume-web-jersey/src/test/java/com/coreoz/plume/jersey/control/SizeLimitingInputStreamTest.java index 5014776..81175a1 100644 --- a/plume-web-jersey/src/test/java/com/coreoz/plume/jersey/control/SizeLimitingInputStreamTest.java +++ b/plume-web-jersey/src/test/java/com/coreoz/plume/jersey/control/SizeLimitingInputStreamTest.java @@ -78,4 +78,49 @@ public void testRead_whenEmpty_shouldSuccessWithoutReading() throws IOException assertEquals(-1, bytesRead); } + @Test + public void testRead_withoutOffsetAndLength_shouldSuccess() throws IOException { + byte[] data = "1234567890".getBytes(); // 10 bytes, exactly at the limit + byteArrayInputStream = new ByteArrayInputStream(data); + sizeLimitingInputStream = new SizeLimitingInputStream(byteArrayInputStream, LIMIT); + int result = sizeLimitingInputStream.read(data, 0, 5); + assertEquals(5, result); + } + + @Test + public void testRead_withoutOffsetAndWithoutLength_shouldSuccess() throws IOException { + byte[] data = "1234567890".getBytes(); // 10 bytes, exactly at the limit + byteArrayInputStream = new ByteArrayInputStream(data); + sizeLimitingInputStream = new SizeLimitingInputStream(byteArrayInputStream, LIMIT); + int result = sizeLimitingInputStream.read(); + assertEquals('1', result); + } + + @Test + public void testRead_withOffsetAndLength_shouldSuccess() throws IOException { + byte[] data = "1234567890".getBytes(); // 10 bytes, exactly at the limit + byteArrayInputStream = new ByteArrayInputStream(data); + sizeLimitingInputStream = new SizeLimitingInputStream(byteArrayInputStream, LIMIT); + int result = sizeLimitingInputStream.read(data, 3, 5); + assertEquals(5, result); + } + + @Test + public void testSkip_withinLength_shouldSuccess() throws IOException { + byte[] data = "1234567890".getBytes(); // 10 bytes, exactly at the limit + byteArrayInputStream = new ByteArrayInputStream(data); + sizeLimitingInputStream = new SizeLimitingInputStream(byteArrayInputStream, LIMIT); + long result = sizeLimitingInputStream.skip(3); + assertEquals(3, result); + } + + @Test + public void testSkip_beyondLength_shouldSuccess() throws IOException { + byte[] data = "1234567890".getBytes(); // 10 bytes, exactly at the limit + byteArrayInputStream = new ByteArrayInputStream(data); + sizeLimitingInputStream = new SizeLimitingInputStream(byteArrayInputStream, LIMIT); + long result = sizeLimitingInputStream.skip(11); + assertEquals(10, result); + } + }