Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import io.quarkus.resteasy.reactive.server.test.multipart.other.OtherPackageFormDataBase;
import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;
import io.restassured.builder.MultiPartSpecBuilder;
import io.restassured.specification.MultiPartSpecification;

public class MultipartInputTest extends AbstractMultipartTest {

Expand Down Expand Up @@ -240,6 +242,25 @@ public void testSameNameParam() {
Assertions.assertEquals(4, uploadDir.toFile().listFiles().length);
}

@Test
public void testExtraHeader() {
MultiPartSpecification formPart = new MultiPartSpecBuilder(TXT_FILE)
.header("extra-header", "extra-value")
.mimeType("text/plain")
.build();
RestAssured.given()
.multiPart(formPart)
.accept("text/plain")
.when()
.post("/multipart/extra-header")
.then()
.statusCode(200)
.body(equalTo("extra-value"));

// ensure that the 3 uploaded files where created on disk
Assertions.assertEquals(1, uploadDir.toFile().listFiles().length);
}

private String filePath(File file) {
return file.toPath().toAbsolutePath().toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import jakarta.ws.rs.core.MediaType;

import org.jboss.resteasy.reactive.RestResponse;
import org.jboss.resteasy.reactive.common.util.QuarkusMultivaluedHashMap;
import org.jboss.resteasy.reactive.server.multipart.MultipartFormDataOutput;

@Path("/multipart/output")
Expand Down Expand Up @@ -57,15 +58,17 @@ public RestResponse<MultipartOutputResponse> restResponse() {
@Path("/with-form-data")
@Produces(MediaType.MULTIPART_FORM_DATA)
public RestResponse<MultipartFormDataOutput> withFormDataOutput() {
QuarkusMultivaluedHashMap<String, Object> headers = new QuarkusMultivaluedHashMap<>();
headers.putSingle("extra-header", "extra-value");
MultipartFormDataOutput form = new MultipartFormDataOutput();
form.addFormData("name", RESPONSE_NAME, MediaType.TEXT_PLAIN_TYPE);
form.addFormData("part-with-filename", RESPONSE_FILENAME, MediaType.TEXT_PLAIN_TYPE, "file.txt");
form.addFormData("part-with-filename", RESPONSE_FILENAME, MediaType.TEXT_PLAIN_TYPE, "file2.txt");
form.addFormData("part-with-filename", RESPONSE_FILENAME, MediaType.TEXT_PLAIN_TYPE, "file2.txt", headers);
form.addFormData("custom-surname", RESPONSE_SURNAME, MediaType.TEXT_PLAIN_TYPE);
form.addFormData("custom-status", RESPONSE_STATUS, MediaType.TEXT_PLAIN_TYPE)
.getHeaders().putSingle("extra-header", "extra-value");
.getHeaders().putAll(headers);
form.addFormData("values", RESPONSE_VALUES, MediaType.TEXT_PLAIN_TYPE);
form.addFormData("active", RESPONSE_ACTIVE, MediaType.TEXT_PLAIN_TYPE);
form.addFormData("active", RESPONSE_ACTIVE, MediaType.TEXT_PLAIN_TYPE, headers);
return RestResponse.ResponseBuilder.ok(form).header("foo", "bar").build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,13 @@ public void testWithFormData() {
String body = extractable.asString();
assertContainsValue(body, "name", MediaType.TEXT_PLAIN, MultipartOutputResource.RESPONSE_NAME);
assertContainsValue(body, "part-with-filename", MediaType.TEXT_PLAIN, "filename=\"file.txt\"");
assertContainsValue(body, "part-with-filename", MediaType.TEXT_PLAIN, "filename=\"file2.txt\"");
assertContainsValue(body, "part-with-filename", MediaType.TEXT_PLAIN, "filename=\"file2.txt\"",
"extra-header: extra-value");
assertContainsValue(body, "custom-surname", MediaType.TEXT_PLAIN, MultipartOutputResource.RESPONSE_SURNAME);
assertContainsValue(body, "custom-status", MediaType.TEXT_PLAIN, MultipartOutputResource.RESPONSE_STATUS);
assertContainsValue(body, "active", MediaType.TEXT_PLAIN, MultipartOutputResource.RESPONSE_ACTIVE);
assertContainsValue(body, "custom-status", MediaType.TEXT_PLAIN, MultipartOutputResource.RESPONSE_STATUS,
"extra-header: extra-value");
assertContainsValue(body, "active", MediaType.TEXT_PLAIN, MultipartOutputResource.RESPONSE_ACTIVE,
"extra-header: extra-value");
assertContainsValue(body, "values", MediaType.TEXT_PLAIN, "[one, two]");

assertThat(extractable.header("Content-Type")).contains("boundary=");
Expand Down Expand Up @@ -182,4 +185,12 @@ private void assertContainsValue(String response, String name, String contentTyp
&& line.contains(String.format(EXPECTED_CONTENT_TYPE_PART, contentType))
&& line.contains(value.toString()));
}

private void assertContainsValue(String response, String name, String contentType, Object value, String header) {
String[] lines = response.split("--");
assertThat(lines).anyMatch(line -> line.contains(String.format(EXPECTED_CONTENT_DISPOSITION_PART, name))
&& line.contains(String.format(EXPECTED_CONTENT_TYPE_PART, contentType))
&& line.contains(value.toString())
&& line.contains(header));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,12 @@ public String optional(FormData formData) {
+ (formData.txtFile != null);
}

@POST
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("/extra-header")
public String extraHeader(@RestForm("file") FileUpload file) {
return file.getHeaders().get("extra-header").get(0);
}

}
Loading