Skip to content

Commit

Permalink
Add a unit test for the content control feature with GET request
Browse files Browse the repository at this point in the history
  • Loading branch information
bvasseur-urw committed Sep 20, 2024
1 parent f6a9e0f commit 52ed253
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.io.InputStream;
import java.lang.reflect.AnnotatedElement;

import jakarta.inject.Singleton;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.container.DynamicFeature;
import jakarta.ws.rs.container.ResourceInfo;
Expand All @@ -13,14 +12,11 @@
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ReaderInterceptor;
import jakarta.ws.rs.ext.ReaderInterceptorContext;
import lombok.extern.slf4j.Slf4j;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Singleton
@Slf4j
public class ContentControlFeature implements DynamicFeature {

private static final Logger logger = LoggerFactory.getLogger(ContentControlFeature.class);
public static final int DEFAULT_MAX_SIZE = 500 * 1024; // 500 KB
private final Integer maxSize;

Expand Down Expand Up @@ -66,11 +62,7 @@ public Object aroundReadFrom(ReaderInterceptorContext context) throws IOExceptio
try {
headerContentLength = Integer.parseInt(context.getHeaders().getFirst(HttpHeaders.CONTENT_LENGTH));
} catch (NumberFormatException e) {
throw new WebApplicationException(
Response.status(Response.Status.LENGTH_REQUIRED)
.entity("Content-Length header is missing or invalid.")
.build()
);
headerContentLength = maxSize; // default value for GET or chunked body
}
if (headerContentLength > maxSize) {
throw new WebApplicationException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected Application configure() {
public void checkContentSize_withBody_whenWithinDefaultLimit_shouldReturn200() {
byte[] data = "12345".getBytes();
Response response = target("/test/upload-default").request().post(Entity.entity(data, MediaType.APPLICATION_OCTET_STREAM));
assertEquals(200, response.getStatus());
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
}

@Test
Expand All @@ -50,11 +50,17 @@ public void checkContentSize_withBody_whenContentLengthIsWrong_shouldReturn411()
assertEquals(Response.Status.LENGTH_REQUIRED.getStatusCode(), request.post(null).getStatus());
}

@Test
public void checkContentSize_withoutBody_whenDefaultLimit_shouldReturn200() {
Builder request = target("/test/upload-default").request();
assertEquals(Response.Status.OK.getStatusCode(), request.get().getStatus());
}

@Test
public void checkContentSize_withBody_whenWithinCustomLimit_shouldReturn200() {
byte[] data = "12345".getBytes();
Response response = target("/test/upload-custom").request().post(Entity.entity(data, MediaType.APPLICATION_OCTET_STREAM));
assertEquals(200, response.getStatus());
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
}

@Test
Expand All @@ -66,6 +72,12 @@ public void checkContentSize_withBody_whenBeyondCustomLimit_shouldReturn413() {
assertEquals(Response.Status.REQUEST_ENTITY_TOO_LARGE.getStatusCode(), request.post(entity).getStatus());
}

@Test
public void checkContentSize_withoutBody_whenCustomLimit_shouldReturn200() {
Builder request = target("/test/upload-custom").request();
assertEquals(Response.Status.OK.getStatusCode(), request.get().getStatus());
}

@Test
public void checkMaxSize_whenCustomControlFeature_shouldSuccess() {
// Custom max size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.coreoz.plume.jersey.security.control.ContentSizeLimit;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.core.Response;
Expand All @@ -17,11 +18,24 @@ public Response uploadDefaultLimit(byte[] data) {
return Response.ok("Upload successful").build();
}

@GET
@Path("/upload-default")
public Response getDefaultLimit(byte[] data) {
return Response.ok("get successful").build();
}

@POST
@Path("/upload-custom")
@ContentSizeLimit(CUSTOM_MAX_SIZE)
public Response uploadCustomLimit(byte[] data) {
return Response.ok("Upload successful").build();
}

@GET
@Path("/upload-custom")
@ContentSizeLimit(CUSTOM_MAX_SIZE)
public Response getCustomLimit(byte[] data) {
return Response.ok("get successful").build();
}

}

0 comments on commit 52ed253

Please sign in to comment.