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);
});
}
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);
});
}
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);
});
}
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();
}
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);
});
}
Aggregations