Skip to content

Commit

Permalink
Add unit test for sizelimitinginputstream for more coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
bvasseur-urw committed Sep 19, 2024
1 parent ec1660f commit c7b35bb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@

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.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;

Expand Down Expand Up @@ -59,4 +56,6 @@ public void checkContentSize_withBody_whenBeyondCustomLimit_shouldThrow() {
assertEquals(Response.Status.REQUEST_ENTITY_TOO_LARGE.getStatusCode(), request.post(entity).getStatus());
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,40 @@ 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_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);
}

}

0 comments on commit c7b35bb

Please sign in to comment.