forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handling a multipart request part as a file based on the content-type
Closes quarkusio#29725
- Loading branch information
Showing
9 changed files
with
165 additions
and
3 deletions.
There are no files selected for viewing
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
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
23 changes: 23 additions & 0 deletions
23
...sions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/MultiPartConfig.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,23 @@ | ||
package io.quarkus.vertx.http.runtime; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import io.quarkus.runtime.annotations.ConfigGroup; | ||
import io.quarkus.runtime.annotations.ConfigItem; | ||
import io.quarkus.runtime.annotations.ConvertWith; | ||
import io.quarkus.runtime.configuration.TrimmedStringConverter; | ||
|
||
/** | ||
* A {@link ConfigGroup} for the settings related to HTTP multipart request handling. | ||
*/ | ||
@ConfigGroup | ||
public class MultiPartConfig { | ||
|
||
/** | ||
* A list of {@code ContentType} to indicate whether a given multipart field should be handled as a file part. | ||
*/ | ||
@ConfigItem | ||
@ConvertWith(TrimmedStringConverter.class) | ||
public Optional<List<String>> fileContentTypes; | ||
} |
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
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
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
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
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
86 changes: 86 additions & 0 deletions
86
...org/jboss/resteasy/reactive/server/vertx/test/multipart/MultipartFileContentTypeTest.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,86 @@ | ||
package org.jboss.resteasy.reactive.server.vertx.test.multipart; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
import java.util.function.Supplier; | ||
|
||
import javax.ws.rs.core.MediaType; | ||
|
||
import org.jboss.resteasy.reactive.server.vertx.test.framework.ResteasyReactiveUnitTest; | ||
import org.jboss.resteasy.reactive.server.vertx.test.multipart.other.OtherPackageFormDataBase; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.restassured.RestAssured; | ||
|
||
public class MultipartFileContentTypeTest extends AbstractMultipartTest { | ||
|
||
private static final Path uploadDir = Paths.get("file-uploads"); | ||
|
||
@RegisterExtension | ||
static ResteasyReactiveUnitTest test = new ResteasyReactiveUnitTest() | ||
.setDeleteUploadedFilesOnEnd(false) | ||
.setUploadPath(uploadDir) | ||
.setFileContentTypes(List.of(MediaType.APPLICATION_OCTET_STREAM, MediaType.APPLICATION_SVG_XML)) | ||
.setArchiveProducer(new Supplier<>() { | ||
@Override | ||
public JavaArchive get() { | ||
return ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(FormDataBase.class, OtherPackageFormDataBase.class, FormData.class, Status.class, | ||
OtherFormData.class, FormDataSameFileName.class, | ||
OtherFormDataBase.class, | ||
MultipartResource.class, OtherMultipartResource.class); | ||
} | ||
|
||
}); | ||
|
||
private final File FILE = new File("./src/test/resources/test.html"); | ||
|
||
@BeforeEach | ||
public void assertEmptyUploads() { | ||
Assertions.assertTrue(isDirectoryEmpty(uploadDir)); | ||
} | ||
|
||
@AfterEach | ||
public void clearDirectory() { | ||
clearDirectory(uploadDir); | ||
} | ||
|
||
@Test | ||
public void testFilePartWithExpectedContentType() throws IOException { | ||
RestAssured.given() | ||
.multiPart("octetStream", null, Files.readAllBytes(FILE.toPath()), MediaType.APPLICATION_OCTET_STREAM) | ||
.multiPart("svgXml", null, Files.readAllBytes(FILE.toPath()), MediaType.APPLICATION_SVG_XML) | ||
.accept("text/plain") | ||
.when() | ||
.post("/multipart/optional") | ||
.then() | ||
.statusCode(200); | ||
|
||
// ensure that the 2 uploaded files where created on disk | ||
Assertions.assertEquals(2, uploadDir.toFile().listFiles().length); | ||
} | ||
|
||
@Test | ||
public void testFilePartWithUnexpectedContentType() throws IOException { | ||
RestAssured.given() | ||
.multiPart("xmlFile", null, Files.readAllBytes(FILE.toPath()), MediaType.APPLICATION_XML) | ||
.accept("text/plain") | ||
.when() | ||
.post("/multipart/optional") | ||
.then() | ||
.statusCode(200); | ||
|
||
// ensure that no files are created as the content-type is not supported as a file part | ||
Assertions.assertEquals(0, uploadDir.toFile().listFiles().length); | ||
} | ||
} |