-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
21300 return 413 when file to big (#201)
* add exception mapper for file controller * add file payload test * use test property source
- Loading branch information
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
src/main/java/ca/gc/aafc/objectstore/api/exceptionmapping/PayloadToLargeExceptionMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
60
src/test/java/ca/gc/aafc/objectstore/api/file/FilePayloadIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |