Search in sources :

Example 6 with MultipartForm

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");
}
Also used : OpenAPI3RequestValidationHandlerImpl(io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RequestValidationHandlerImpl) ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) MultipartForm(io.vertx.ext.web.multipart.MultipartForm) Operation(io.swagger.v3.oas.models.Operation) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Example 7 with MultipartForm

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");
}
Also used : RouterFactoryOptions(io.vertx.ext.web.api.contract.RouterFactoryOptions) MultipartForm(io.vertx.ext.web.multipart.MultipartForm) CountDownLatch(java.util.concurrent.CountDownLatch) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Example 8 with MultipartForm

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");
}
Also used : OpenAPI3RequestValidationHandlerImpl(io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RequestValidationHandlerImpl) ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) MultipartForm(io.vertx.ext.web.multipart.MultipartForm) Operation(io.swagger.v3.oas.models.Operation) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Example 9 with MultipartForm

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");
}
Also used : RouterFactoryOptions(io.vertx.ext.web.api.contract.RouterFactoryOptions) MultipartForm(io.vertx.ext.web.multipart.MultipartForm) CountDownLatch(java.util.concurrent.CountDownLatch) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Example 10 with MultipartForm

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
    });
}
Also used : MultipartForm(io.vertx.ext.web.multipart.MultipartForm)

Aggregations

MultipartForm (io.vertx.ext.web.multipart.MultipartForm)15 JsonObject (io.vertx.core.json.JsonObject)10 Buffer (io.vertx.core.buffer.Buffer)9 JsonArray (io.vertx.core.json.JsonArray)7 Test (org.junit.Test)7 MultiMap (io.vertx.core.MultiMap)5 ArrayList (java.util.ArrayList)5 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)3 QueryStringEncoder (io.netty.handler.codec.http.QueryStringEncoder)3 HttpPostRequestEncoder (io.netty.handler.codec.http.multipart.HttpPostRequestEncoder)3 Future (io.vertx.core.Future)3 Handler (io.vertx.core.Handler)3 Vertx (io.vertx.core.Vertx)3 HttpHeaders (io.vertx.core.http.HttpHeaders)3 HttpMethod (io.vertx.core.http.HttpMethod)3 ReadStream (io.vertx.core.streams.ReadStream)3 Route (io.vertx.ext.web.Route)3 Router (io.vertx.ext.web.Router)3 RoutingContext (io.vertx.ext.web.RoutingContext)3 RequestParameters (io.vertx.ext.web.api.RequestParameters)3