use of io.vertx.ext.web.multipart.MultipartForm in project vertx-web by vert-x3.
the class OpenAPI3ValidationTest method testComplexMultipart.
@Test
public void testComplexMultipart() throws Exception {
Operation op = testSpec.getPaths().get("/multipart/complex").getPost();
OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(op, op.getParameters(), testSpec, refsCache);
loadHandlers("/multipart/complex", HttpMethod.POST, false, validationHandler, (routingContext) -> {
RequestParameters params = routingContext.get("parsedParameters");
assertNotNull(params.formParameter("param2").getJsonObject());
assertEquals("Willy", params.formParameter("param2").getJsonObject().getString("name"));
assertEquals(4, params.formParameter("param4").getArray().size());
assertEquals((Integer) 2, params.formParameter("param5").getInteger());
routingContext.response().setStatusMessage("ok").end();
});
JsonObject pet = new JsonObject();
pet.put("id", 14612);
pet.put("name", "Willy");
List<String> valuesArray = new ArrayList<>();
for (int i = 0; i < 4; i++) valuesArray.add(getSuccessSample(ParameterType.FLOAT).getFloat().toString());
MultipartForm form = MultipartForm.create().textFileUpload("param1", "random.txt", "src/test/resources/random.txt", "text/plain").attribute("param2", pet.encode()).textFileUpload("param3", "random.csv", "src/test/resources/random.txt", "text/csv").attribute("param4", serializeInCSVStringArray(valuesArray)).attribute("param5", "2").binaryFileUpload("param1Binary", "random-file", "src/test/resources/random-file", "text/plain");
testRequestWithMultipartForm(HttpMethod.POST, "/multipart/complex", form, 200, "ok");
}
use of io.vertx.ext.web.multipart.MultipartForm in project vertx-web by vert-x3.
the class OpenAPI3RouterFactoryTest method commaSeparatedMultipartEncoding.
@Test
public void commaSeparatedMultipartEncoding() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
OpenAPI3RouterFactory.create(this.vertx, "src/test/resources/swaggers/multipart.yaml", openAPI3RouterFactoryAsyncResult -> {
if (openAPI3RouterFactoryAsyncResult.succeeded()) {
routerFactory = openAPI3RouterFactoryAsyncResult.result();
routerFactory.setBodyHandler(BodyHandler.create(tempFolder.getRoot().getAbsolutePath()));
routerFactory.setOptions(new RouterFactoryOptions().setRequireSecurityHandlers(false));
routerFactory.addHandlerByOperationId("testMultipartMultiple", (ctx) -> {
RequestParameters params = ctx.get("parsedParameters");
ctx.response().setStatusCode(200).setStatusMessage(params.formParameter("type").getString()).end();
});
latch.countDown();
} else {
fail(openAPI3RouterFactoryAsyncResult.cause());
}
});
awaitLatch(latch);
startServer();
MultipartForm form1 = MultipartForm.create().binaryFileUpload("file1", "random-file", "src/test/resources/random-file", "application/octet-stream").attribute("type", "application/octet-stream");
testRequestWithMultipartForm(HttpMethod.POST, "/testMultipartMultiple", form1, 200, "application/octet-stream");
MultipartForm form2 = MultipartForm.create().binaryFileUpload("file1", "random.txt", "src/test/resources/random.txt", "text/plain").attribute("type", "text/plain");
testRequestWithMultipartForm(HttpMethod.POST, "/testMultipartMultiple", form2, 200, "text/plain");
MultipartForm form3 = MultipartForm.create().binaryFileUpload("file1", "random.txt", "src/test/resources/random.txt", "application/json").attribute("type", "application/json");
testRequestWithMultipartForm(HttpMethod.POST, "/testMultipartMultiple", form3, 400, "Bad Request");
}
use of io.vertx.ext.web.multipart.MultipartForm in project vertx-web by vert-x3.
the class OpenAPI3MultipleFilesValidationTest method testComplexMultipart.
@Test
public void testComplexMultipart() throws Exception {
Operation op = testSpec.getPaths().get("/multipart/complex").getPost();
OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(op, op.getParameters(), testSpec, refsCache);
loadHandlers("/multipart/complex", HttpMethod.POST, false, validationHandler, (routingContext) -> {
RequestParameters params = routingContext.get("parsedParameters");
assertNotNull(params.formParameter("param2").getJsonObject());
assertEquals("Willy", params.formParameter("param2").getJsonObject().getString("name"));
assertEquals(4, params.formParameter("param4").getArray().size());
assertEquals((Integer) 2, params.formParameter("param5").getInteger());
routingContext.response().setStatusMessage("ok").end();
});
JsonObject pet = new JsonObject();
pet.put("id", 14612);
pet.put("name", "Willy");
List<String> valuesArray = new ArrayList<>();
for (int i = 0; i < 4; i++) valuesArray.add(getSuccessSample(ParameterType.FLOAT).getFloat().toString());
MultipartForm form = MultipartForm.create().textFileUpload("param1", "random.txt", "src/test/resources/random.txt", "text/plain").attribute("param2", pet.encode()).textFileUpload("param3", "random.csv", "src/test/resources/random.txt", "text/csv").attribute("param4", serializeInCSVStringArray(valuesArray)).attribute("param5", "2").binaryFileUpload("param1Binary", "random-file", "src/test/resources/random-file", "text/plain");
testRequestWithMultipartForm(HttpMethod.POST, "/multipart/complex", form, 200, "ok");
}
use of io.vertx.ext.web.multipart.MultipartForm in project vertx-web by vert-x3.
the class OpenAPI3RouterFactoryTest method wildcardMultipartEncoding.
@Test
public void wildcardMultipartEncoding() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
OpenAPI3RouterFactory.create(this.vertx, "src/test/resources/swaggers/multipart.yaml", openAPI3RouterFactoryAsyncResult -> {
if (openAPI3RouterFactoryAsyncResult.succeeded()) {
routerFactory = openAPI3RouterFactoryAsyncResult.result();
routerFactory.setOptions(new RouterFactoryOptions().setRequireSecurityHandlers(false));
routerFactory.setBodyHandler(BodyHandler.create(tempFolder.getRoot().getAbsolutePath()));
routerFactory.addHandlerByOperationId("testMultipartWildcard", (ctx) -> {
RequestParameters params = ctx.get("parsedParameters");
ctx.response().setStatusCode(200).setStatusMessage(params.formParameter("type").getString()).end();
});
latch.countDown();
} else {
fail(openAPI3RouterFactoryAsyncResult.cause());
}
});
awaitLatch(latch);
startServer();
MultipartForm form1 = MultipartForm.create().binaryFileUpload("file1", "random.txt", "src/test/resources/random.txt", "text/plain").attribute("type", "text/plain");
testRequestWithMultipartForm(HttpMethod.POST, "/testMultipartWildcard", form1, 200, "text/plain");
MultipartForm form2 = MultipartForm.create().binaryFileUpload("file1", "random.csv", "src/test/resources/random.csv", "text/csv").attribute("type", "text/csv");
testRequestWithMultipartForm(HttpMethod.POST, "/testMultipartWildcard", form2, 200, "text/csv");
MultipartForm form3 = MultipartForm.create().binaryFileUpload("file1", "random.txt", "src/test/resources/random.txt", "application/json").attribute("type", "application/json");
testRequestWithMultipartForm(HttpMethod.POST, "/testMultipartWildcard", form3, 400, "Bad Request");
}
use of io.vertx.ext.web.multipart.MultipartForm in project vertx-web by vert-x3.
the class WebClientExamples method sendMultipartWithFileUpload.
public void sendMultipartWithFileUpload(WebClient client) {
MultipartForm form = MultipartForm.create().attribute("imageDescription", "a very nice image").binaryFileUpload("imageFile", "image.jpg", "/path/to/image", "image/jpeg");
// Submit the form as a multipart form body
client.post(8080, "myserver.mycompany.com", "/some-uri").sendMultipartForm(form).onSuccess(res -> {
// OK
});
}
Aggregations