Search in sources :

Example 1 with MultipartForm

use of io.vertx.ext.web.multipart.MultipartForm in project vertx-web by vert-x3.

the class RouterBuilderIntegrationTest method wildcardMultipartEncoding.

@Test
public void wildcardMultipartEncoding(Vertx vertx, VertxTestContext testContext) {
    Checkpoint checkpoint = testContext.checkpoint(3);
    loadBuilderAndStartServer(vertx, "src/test/resources/specs/multipart.yaml", testContext, routerBuilder -> {
        routerBuilder.setOptions(new RouterBuilderOptions().setRequireSecurityHandlers(false));
        routerBuilder.operation("testMultipartWildcard").handler(routingContext -> {
            RequestParameters params = routingContext.get("parsedParameters");
            routingContext.response().setStatusCode(200).setStatusMessage(params.body().getJsonObject().getString("type")).end();
        });
    }).onComplete(h -> {
        MultipartForm form1 = MultipartForm.create().binaryFileUpload("file1", "random.txt", "src/test/resources/random.txt", "text/plain").attribute("type", "text/plain");
        testRequest(client, HttpMethod.POST, "/testMultipartWildcard").expect(statusCode(200), statusMessage("text/plain")).sendMultipartForm(form1, testContext, checkpoint);
        MultipartForm form2 = MultipartForm.create().binaryFileUpload("file1", "random.csv", "src/test/resources/random.csv", "text/csv").attribute("type", "text/csv");
        testRequest(client, HttpMethod.POST, "/testMultipartWildcard").expect(statusCode(200), statusMessage("text/csv")).sendMultipartForm(form2, testContext, checkpoint);
        MultipartForm form3 = MultipartForm.create().binaryFileUpload("file1", "random.txt", "src/test/resources/random.txt", "application/json").attribute("type", "application/json");
        testRequest(client, HttpMethod.POST, "/testMultipartWildcard").expect(statusCode(400)).sendMultipartForm(form3, testContext, checkpoint);
    });
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) Arrays(java.util.Arrays) RequestParameters(io.vertx.ext.web.validation.RequestParameters) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MultiMap(io.vertx.core.MultiMap) Router(io.vertx.ext.web.Router) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) BodyProcessorException(io.vertx.ext.web.validation.BodyProcessorException) Timeout(io.vertx.junit5.Timeout) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MultipartForm(io.vertx.ext.web.multipart.MultipartForm) ParameterLocation(io.vertx.ext.web.validation.impl.ParameterLocation) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) JsonObject(io.vertx.core.json.JsonObject) RequestParameter(io.vertx.ext.web.validation.RequestParameter) Route(io.vertx.ext.web.Route) QueryStringEncoder(io.netty.handler.codec.http.QueryStringEncoder) StaticHandler(io.vertx.ext.web.handler.StaticHandler) Vertx(io.vertx.core.Vertx) TestRequest(io.vertx.ext.web.validation.testutils.TestRequest) HttpHeaders(io.vertx.core.http.HttpHeaders) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) Stream(java.util.stream.Stream) ValidationTestUtils(io.vertx.ext.web.validation.testutils.ValidationTestUtils) Buffer(io.vertx.core.buffer.Buffer) HttpMethod(io.vertx.core.http.HttpMethod) ParameterProcessorException(io.vertx.ext.web.validation.ParameterProcessorException) Condition(org.assertj.core.api.Condition) Checkpoint(io.vertx.junit5.Checkpoint) Pattern(java.util.regex.Pattern) Handler(io.vertx.core.Handler) Checkpoint(io.vertx.junit5.Checkpoint) MultipartForm(io.vertx.ext.web.multipart.MultipartForm) RequestParameters(io.vertx.ext.web.validation.RequestParameters) Test(org.junit.jupiter.api.Test)

Example 2 with MultipartForm

use of io.vertx.ext.web.multipart.MultipartForm in project vertx-web by vert-x3.

the class RouterBuilderIntegrationTest method consumesTest.

@Test
public void consumesTest(Vertx vertx, VertxTestContext testContext) {
    Checkpoint checkpoint = testContext.checkpoint(4);
    loadBuilderAndStartServer(vertx, "src/test/resources/specs/produces_consumes_test.yaml", testContext, routerBuilder -> {
        routerBuilder.setOptions(new RouterBuilderOptions().setMountNotImplementedHandler(false));
        routerBuilder.operation("consumesTest").handler(routingContext -> {
            RequestParameters params = routingContext.get("parsedParameters");
            if (params.body() != null && params.body().isJsonObject()) {
                routingContext.response().setStatusCode(200).putHeader("Content-Type", "application/json").end(params.body().getJsonObject().encode());
            } else {
                routingContext.response().setStatusCode(200).end();
            }
        });
    }).onComplete(h -> {
        JsonObject obj = new JsonObject().put("name", "francesco");
        testRequest(client, HttpMethod.POST, "/consumesTest").expect(statusCode(200)).expect(jsonBodyResponse(obj)).sendJson(obj, testContext, checkpoint);
        MultiMap form = MultiMap.caseInsensitiveMultiMap();
        form.add("name", "francesco");
        testRequest(client, HttpMethod.POST, "/consumesTest").expect(statusCode(200)).expect(jsonBodyResponse(obj)).sendURLEncodedForm(form, testContext, checkpoint);
        MultipartForm multipartForm = MultipartForm.create();
        form.add("name", "francesco");
        testRequest(client, HttpMethod.POST, "/consumesTest").expect(statusCode(400)).expect(badBodyResponse(BodyProcessorException.BodyProcessorErrorType.MISSING_MATCHING_BODY_PROCESSOR)).sendMultipartForm(multipartForm, testContext, checkpoint);
        testRequest(client, HttpMethod.POST, "/consumesTest").expect(statusCode(400)).expect(failurePredicateResponse()).send(testContext, checkpoint);
    });
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) Arrays(java.util.Arrays) RequestParameters(io.vertx.ext.web.validation.RequestParameters) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MultiMap(io.vertx.core.MultiMap) Router(io.vertx.ext.web.Router) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) BodyProcessorException(io.vertx.ext.web.validation.BodyProcessorException) Timeout(io.vertx.junit5.Timeout) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MultipartForm(io.vertx.ext.web.multipart.MultipartForm) ParameterLocation(io.vertx.ext.web.validation.impl.ParameterLocation) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) JsonObject(io.vertx.core.json.JsonObject) RequestParameter(io.vertx.ext.web.validation.RequestParameter) Route(io.vertx.ext.web.Route) QueryStringEncoder(io.netty.handler.codec.http.QueryStringEncoder) StaticHandler(io.vertx.ext.web.handler.StaticHandler) Vertx(io.vertx.core.Vertx) TestRequest(io.vertx.ext.web.validation.testutils.TestRequest) HttpHeaders(io.vertx.core.http.HttpHeaders) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) Stream(java.util.stream.Stream) ValidationTestUtils(io.vertx.ext.web.validation.testutils.ValidationTestUtils) Buffer(io.vertx.core.buffer.Buffer) HttpMethod(io.vertx.core.http.HttpMethod) ParameterProcessorException(io.vertx.ext.web.validation.ParameterProcessorException) Condition(org.assertj.core.api.Condition) Checkpoint(io.vertx.junit5.Checkpoint) Pattern(java.util.regex.Pattern) Handler(io.vertx.core.Handler) MultiMap(io.vertx.core.MultiMap) Checkpoint(io.vertx.junit5.Checkpoint) JsonObject(io.vertx.core.json.JsonObject) MultipartForm(io.vertx.ext.web.multipart.MultipartForm) RequestParameters(io.vertx.ext.web.validation.RequestParameters) Test(org.junit.jupiter.api.Test)

Example 3 with MultipartForm

use of io.vertx.ext.web.multipart.MultipartForm in project vertx-web by vert-x3.

the class RouterBuilderIntegrationTest method commaSeparatedMultipartEncoding.

@Test
public void commaSeparatedMultipartEncoding(Vertx vertx, VertxTestContext testContext) {
    Checkpoint checkpoint = testContext.checkpoint(3);
    loadBuilderAndStartServer(vertx, "src/test/resources/specs/multipart.yaml", testContext, routerBuilder -> {
        routerBuilder.setOptions(new RouterBuilderOptions().setRequireSecurityHandlers(false));
        routerBuilder.operation("testMultipartMultiple").handler(routingContext -> {
            RequestParameters params = routingContext.get("parsedParameters");
            routingContext.response().setStatusCode(200).setStatusMessage(params.body().getJsonObject().getString("type")).end();
        });
    }).onComplete(h -> {
        MultipartForm form1 = MultipartForm.create().binaryFileUpload("file1", "random-file", "src/test/resources/random-file", "application/octet-stream").attribute("type", "application/octet-stream");
        testRequest(client, HttpMethod.POST, "/testMultipartMultiple").expect(statusCode(200), statusMessage("application/octet-stream")).sendMultipartForm(form1, testContext, checkpoint);
        MultipartForm form2 = MultipartForm.create().binaryFileUpload("file1", "random.txt", "src/test/resources/random.txt", "text/plain").attribute("type", "text/plain");
        testRequest(client, HttpMethod.POST, "/testMultipartMultiple").expect(statusCode(200), statusMessage("text/plain")).sendMultipartForm(form2, testContext, checkpoint);
        MultipartForm form3 = MultipartForm.create().binaryFileUpload("file1", "random.txt", "src/test/resources/random.txt", "application/json").attribute("type", "application/json");
        testRequest(client, HttpMethod.POST, "/testMultipartMultiple").expect(statusCode(400)).sendMultipartForm(form3, testContext, checkpoint);
    });
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) Arrays(java.util.Arrays) RequestParameters(io.vertx.ext.web.validation.RequestParameters) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MultiMap(io.vertx.core.MultiMap) Router(io.vertx.ext.web.Router) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) BodyProcessorException(io.vertx.ext.web.validation.BodyProcessorException) Timeout(io.vertx.junit5.Timeout) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MultipartForm(io.vertx.ext.web.multipart.MultipartForm) ParameterLocation(io.vertx.ext.web.validation.impl.ParameterLocation) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) JsonObject(io.vertx.core.json.JsonObject) RequestParameter(io.vertx.ext.web.validation.RequestParameter) Route(io.vertx.ext.web.Route) QueryStringEncoder(io.netty.handler.codec.http.QueryStringEncoder) StaticHandler(io.vertx.ext.web.handler.StaticHandler) Vertx(io.vertx.core.Vertx) TestRequest(io.vertx.ext.web.validation.testutils.TestRequest) HttpHeaders(io.vertx.core.http.HttpHeaders) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) Stream(java.util.stream.Stream) ValidationTestUtils(io.vertx.ext.web.validation.testutils.ValidationTestUtils) Buffer(io.vertx.core.buffer.Buffer) HttpMethod(io.vertx.core.http.HttpMethod) ParameterProcessorException(io.vertx.ext.web.validation.ParameterProcessorException) Condition(org.assertj.core.api.Condition) Checkpoint(io.vertx.junit5.Checkpoint) Pattern(java.util.regex.Pattern) Handler(io.vertx.core.Handler) Checkpoint(io.vertx.junit5.Checkpoint) MultipartForm(io.vertx.ext.web.multipart.MultipartForm) RequestParameters(io.vertx.ext.web.validation.RequestParameters) Test(org.junit.jupiter.api.Test)

Example 4 with MultipartForm

use of io.vertx.ext.web.multipart.MultipartForm in project vertx-web by vert-x3.

the class WebClientTest method testFileUploadFormMultipart.

private void testFileUploadFormMultipart(MultipartForm form, List<Upload> toUpload, boolean multipartMixed, BiConsumer<HttpServerRequest, List<Upload>> checker) throws Exception {
    File[] testFiles = new File[toUpload.size()];
    for (int i = 0; i < testFiles.length; i++) {
        Upload upload = toUpload.get(i);
        if (upload.file) {
            String name = upload.filename;
            testFiles[i] = testFolder.newFile(name);
            vertx.fileSystem().writeFileBlocking(testFiles[i].getPath(), upload.data);
            form.textFileUpload(toUpload.get(i).name, toUpload.get(i).filename, testFiles[i].getPath(), "text/plain");
        } else {
            form.textFileUpload(toUpload.get(i).name, toUpload.get(i).filename, upload.data, "text/plain");
        }
    }
    server.requestHandler(req -> {
        req.setExpectMultipart(true);
        List<Upload> uploads = new ArrayList<>();
        req.uploadHandler(upload -> {
            Buffer fileBuffer = Buffer.buffer();
            assertEquals("text/plain", upload.contentType());
            upload.handler(fileBuffer::appendBuffer);
            upload.endHandler(v -> {
                uploads.add(new Upload(upload.name(), upload.filename(), true, upload.charset(), fileBuffer));
            });
        });
        req.endHandler(v -> {
            checker.accept(req, uploads);
            req.response().end();
        });
    });
    startServer();
    HttpRequest<Buffer> builder = webClient.post("somepath");
    builder.multipartMixed(multipartMixed);
    builder.sendMultipartForm(form, onSuccess(resp -> complete()));
    await();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) AsyncFile(io.vertx.core.file.AsyncFile) CoreMatchers(org.hamcrest.CoreMatchers) ResponsePredicate(io.vertx.ext.web.client.predicate.ResponsePredicate) java.util(java.util) ErrorConverter(io.vertx.ext.web.client.predicate.ErrorConverter) URLDecoder(java.net.URLDecoder) io.vertx.core(io.vertx.core) DecodeException(io.vertx.core.json.DecodeException) HttpPostRequestEncoder(io.netty.handler.codec.http.multipart.HttpPostRequestEncoder) BodyCodec(io.vertx.ext.web.codec.BodyCodec) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ResponsePredicateResult(io.vertx.ext.web.client.predicate.ResponsePredicateResult) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) MultipartForm(io.vertx.ext.web.multipart.MultipartForm) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WriteStream(io.vertx.core.streams.WriteStream) ReadStream(io.vertx.core.streams.ReadStream) BiConsumer(java.util.function.BiConsumer) JsonObject(io.vertx.core.json.JsonObject) ConnectException(java.net.ConnectException) SocketAddress(io.vertx.core.net.SocketAddress) ProxyOptions(io.vertx.core.net.ProxyOptions) UsernamePasswordCredentials(io.vertx.ext.auth.authentication.UsernamePasswordCredentials) OpenOptions(io.vertx.core.file.OpenOptions) MalformedURLException(java.net.MalformedURLException) Files(java.nio.file.Files) Test(org.junit.Test) io.vertx.core.http(io.vertx.core.http) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) StandardCharsets(java.nio.charset.StandardCharsets) Consumer(java.util.function.Consumer) JsonArray(io.vertx.core.json.JsonArray) HtdigestCredentials(io.vertx.ext.auth.htdigest.HtdigestCredentials) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) TokenCredentials(io.vertx.ext.auth.authentication.TokenCredentials) Repeat(io.vertx.test.core.Repeat) URLEncoder(java.net.URLEncoder) Buffer(io.vertx.core.buffer.Buffer) ProxyType(io.vertx.core.net.ProxyType) NetServer(io.vertx.core.net.NetServer) WineAndCheese(io.vertx.ext.web.client.jackson.WineAndCheese) Cert(io.vertx.test.tls.Cert) AsyncFile(io.vertx.core.file.AsyncFile) File(java.io.File)

Example 5 with MultipartForm

use of io.vertx.ext.web.multipart.MultipartForm in project vertx-web by vert-x3.

the class WebClientTest method testFileUploadFormMultipart.

private void testFileUploadFormMultipart(int size, boolean memory) throws Exception {
    Buffer content = Buffer.buffer(TestUtils.randomAlphaString(size));
    Upload upload;
    if (memory) {
        upload = Upload.memoryUpload("test", "test.txt", content);
    } else {
        upload = Upload.fileUpload("test", "test.txt", content);
    }
    MultipartForm form = MultipartForm.create().attribute("toolkit", "vert.x").attribute("runtime", "jvm");
    testFileUploadFormMultipart(form, Collections.singletonList(upload), true, (req, uploads) -> {
        assertEquals("vert.x", req.getFormAttribute("toolkit"));
        assertEquals("jvm", req.getFormAttribute("runtime"));
        assertEquals(1, uploads.size());
        assertEquals("test", uploads.get(0).name);
        assertEquals("test.txt", uploads.get(0).filename);
        assertEquals(content, uploads.get(0).data);
    });
}
Also used : Buffer(io.vertx.core.buffer.Buffer) 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