Skip to content

Commit

Permalink
21300 return 413 when file to big (#201)
Browse files Browse the repository at this point in the history
* add exception mapper for file controller
* add file payload test
* use test property source
  • Loading branch information
Jkeyuk authored Jan 4, 2021
1 parent 4f4f9e1 commit db76970
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ca.gc.aafc.objectstore.api.exceptionmapping;

import org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

@ControllerAdvice
public class PayloadToLargeExceptionMapper {

@ResponseStatus(value = HttpStatus.PAYLOAD_TOO_LARGE, reason = "Payload to large")
@ExceptionHandler(MaxUploadSizeExceededException.class)
public void maxUploadSizeExceededException() {
//do nothing
}

@ResponseStatus(value = HttpStatus.PAYLOAD_TOO_LARGE, reason = "Payload to large")
@ExceptionHandler(FileSizeLimitExceededException.class)
public void fileSizeLimitExceededException() {
//do nothing
}
}
60 changes: 60 additions & 0 deletions src/test/java/ca/gc/aafc/objectstore/api/file/FilePayloadIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package ca.gc.aafc.objectstore.api.file;

import ca.gc.aafc.dina.testsupport.BaseRestAssuredTest;
import ca.gc.aafc.dina.testsupport.PostgresTestContainerInitializer;
import ca.gc.aafc.objectstore.api.DinaAuthenticatedUserConfig;
import ca.gc.aafc.objectstore.api.MinioTestConfiguration;
import ca.gc.aafc.objectstore.api.ObjectStoreApiLauncher;
import io.restassured.RestAssured;
import io.restassured.builder.MultiPartSpecBuilder;
import io.restassured.http.Header;
import io.restassured.specification.MultiPartSpecification;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.transaction.annotation.Transactional;

import java.security.SecureRandom;

@Import(MinioTestConfiguration.class)
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = ObjectStoreApiLauncher.class)
@TestPropertySource(properties = {
"spring.config.additional-location=classpath:application-test.yml",
"spring.servlet.multipart.max-file-size=1KB"})
@Transactional
@ContextConfiguration(initializers = {PostgresTestContainerInitializer.class})
public class FilePayloadIT {

@LocalServerPort
protected int testPort;

private final static String bucketUnderTest = DinaAuthenticatedUserConfig.ROLES_PER_GROUPS.keySet()
.stream()
.findFirst()
.get();

@Test
public void fileUpload_WhenPayloadToLarge_ReturnsPayLoadToLarge() {
byte[] bytes = new byte[20048];
new SecureRandom().nextBytes(bytes);

MultiPartSpecification file = new MultiPartSpecBuilder(bytes)
.mimeType("text/plain")
.fileName("testFile")
.controlName("file")
.build();

RestAssured.given()
.header(new Header("content-type", "multipart/form-data"))
.port(this.testPort)
.multiPart(file)
.when().post("/api/v1/file/" + bucketUnderTest).then()
.statusCode(413);
}

}

0 comments on commit db76970

Please sign in to comment.