|
| 1 | +package io.quarkus.resteasy.reactive.server.test.multipart; |
| 2 | + |
| 3 | +import static org.hamcrest.Matchers.is; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.util.HashMap; |
| 7 | + |
| 8 | +import jakarta.ws.rs.POST; |
| 9 | +import jakarta.ws.rs.Path; |
| 10 | +import jakarta.ws.rs.container.ContainerRequestContext; |
| 11 | +import jakarta.ws.rs.container.ContainerRequestFilter; |
| 12 | +import jakarta.ws.rs.container.PreMatching; |
| 13 | +import jakarta.ws.rs.core.HttpHeaders; |
| 14 | +import jakarta.ws.rs.core.MediaType; |
| 15 | +import jakarta.ws.rs.ext.Provider; |
| 16 | + |
| 17 | +import org.hamcrest.MatcherAssert; |
| 18 | +import org.jboss.resteasy.reactive.PartType; |
| 19 | +import org.jboss.resteasy.reactive.RestForm; |
| 20 | +import org.jboss.shrinkwrap.api.ShrinkWrap; |
| 21 | +import org.jboss.shrinkwrap.api.spec.JavaArchive; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 24 | + |
| 25 | +import io.quarkus.test.QuarkusUnitTest; |
| 26 | +import io.restassured.RestAssured; |
| 27 | + |
| 28 | +/** |
| 29 | + * Verifies that changes to the Content-Type header in a ContainerRequestFilter is propagated to the Multipart handling |
| 30 | + */ |
| 31 | +public class MultipartContentTypeHeaderUpdateTest { |
| 32 | + |
| 33 | + public static final String TO_BE_MULTIPART_MARKER = "application/to-be-multipart"; |
| 34 | + |
| 35 | + @RegisterExtension |
| 36 | + static QuarkusUnitTest test = new QuarkusUnitTest() |
| 37 | + .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) |
| 38 | + .addClasses(Resource.class, Input.class, MultipartDataInputTest.Result.class, |
| 39 | + SetMultipartContentTypeFilter.class)); |
| 40 | + |
| 41 | + @Test |
| 42 | + public void test() throws IOException { |
| 43 | + var uploadedContent = RestAssured |
| 44 | + .given() |
| 45 | + .contentType(TO_BE_MULTIPART_MARKER) |
| 46 | + .body(""" |
| 47 | + --content_boundary\r |
| 48 | + Content-Disposition: form-data; name="text"; filename="my-file.txt"\r |
| 49 | + \r |
| 50 | + content\r |
| 51 | + --content_boundary--\r |
| 52 | + """.getBytes()) |
| 53 | + .post("/test") |
| 54 | + .then() |
| 55 | + .extract() |
| 56 | + .asString(); |
| 57 | + |
| 58 | + MatcherAssert.assertThat(uploadedContent, is("content")); |
| 59 | + } |
| 60 | + |
| 61 | + @Path("/test") |
| 62 | + public static class Resource { |
| 63 | + |
| 64 | + @POST |
| 65 | + public String testMultipart(Input input) { |
| 66 | + return input.text; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public static class Input { |
| 71 | + @RestForm("text") |
| 72 | + @PartType(MediaType.APPLICATION_OCTET_STREAM) |
| 73 | + public String text; |
| 74 | + } |
| 75 | + |
| 76 | + @Provider |
| 77 | + @PreMatching |
| 78 | + public static class SetMultipartContentTypeFilter implements ContainerRequestFilter { |
| 79 | + |
| 80 | + @Override |
| 81 | + public void filter(ContainerRequestContext containerRequestContext) { |
| 82 | + var mediaType = containerRequestContext.getMediaType(); |
| 83 | + if (TO_BE_MULTIPART_MARKER.equals(mediaType.getType() + "/" + mediaType.getSubtype())) { |
| 84 | + var parameters = new HashMap<>(mediaType.getParameters()); |
| 85 | + parameters.put("boundary", "content_boundary"); |
| 86 | + var multipartContentType = new MediaType("multipart", "form-data", parameters); |
| 87 | + containerRequestContext.getHeaders().putSingle(HttpHeaders.CONTENT_TYPE, multipartContentType.toString()); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments