use of io.vertx.ext.web.api.contract.RouterFactoryOptions in project vertx-web by vert-x3.
the class OpenAPI3RouterFactoryTest method mountSecurityHandlers.
@Test
public void mountSecurityHandlers() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
OpenAPI3RouterFactory.create(this.vertx, "src/test/resources/swaggers/router_factory_test.yaml", openAPI3RouterFactoryAsyncResult -> {
routerFactory = openAPI3RouterFactoryAsyncResult.result();
routerFactory.setOptions(new RouterFactoryOptions().setRequireSecurityHandlers(true));
routerFactory.addHandlerByOperationId("listPetsSecurity", routingContext -> {
routingContext.response().setStatusCode(200).setStatusMessage(routingContext.get("first_level") + "-" + routingContext.get("second_level") + "-" + routingContext.get("third_level_one") + "-" + routingContext.get("third_level_two") + "-Done").end();
});
routerFactory.addSecurityHandler("api_key", routingContext -> routingContext.put("first_level", "User").next());
routerFactory.addSecuritySchemaScopeValidator("second_api_key", "moderator", routingContext -> routingContext.put("second_level", "Moderator").next());
routerFactory.addSecuritySchemaScopeValidator("third_api_key", "admin", routingContext -> routingContext.put("third_level_one", "Admin").next());
routerFactory.addSecuritySchemaScopeValidator("third_api_key", "useless", routingContext -> routingContext.put("third_level_one", "Wrong!").next());
routerFactory.addSecuritySchemaScopeValidator("third_api_key", "super_admin", routingContext -> routingContext.put("third_level_two", "SuperAdmin").next());
latch.countDown();
});
awaitLatch(latch);
startServer();
testRequest(HttpMethod.GET, "/pets_security_test", 200, "User-Moderator-Admin-SuperAdmin-Done");
}
use of io.vertx.ext.web.api.contract.RouterFactoryOptions in project vertx-web by vert-x3.
the class OpenAPI3RouterFactoryTest method customBodyHandlerTest.
/**
* Tests that user can supply customised BodyHandler
* @throws Exception
*/
@Test
public void customBodyHandlerTest() throws Exception {
try {
CountDownLatch latch = new CountDownLatch(1);
OpenAPI3RouterFactory.create(this.vertx, "src/test/resources/swaggers/upload_test.yaml", openAPI3RouterFactoryAsyncResult -> {
try {
if (openAPI3RouterFactoryAsyncResult.succeeded()) {
routerFactory = openAPI3RouterFactoryAsyncResult.result();
routerFactory.setOptions(new RouterFactoryOptions().setRequireSecurityHandlers(false).setBodyHandler(BodyHandler.create("my-uploads")));
routerFactory.addHandlerByOperationId("upload", (h) -> {
h.response().setStatusCode(201).end();
});
} else {
fail(openAPI3RouterFactoryAsyncResult.cause());
}
} finally {
latch.countDown();
}
});
awaitLatch(latch);
startServer();
// We're not uploading a real file, just triggering BodyHandler
MultiMap form = MultiMap.caseInsensitiveMultiMap();
assertFalse(Paths.get("./my-uploads").toFile().exists());
testRequestWithForm(HttpMethod.POST, "/upload", FormType.MULTIPART, form, 201, "Created");
// BodyHandler should create this custom directory for us
assertTrue(Paths.get("./my-uploads").toFile().exists());
} finally {
Paths.get("./my-uploads").toFile().deleteOnExit();
}
}
use of io.vertx.ext.web.api.contract.RouterFactoryOptions in project vertx-web by vert-x3.
the class OpenAPI3RouterFactoryTest method consumesTest.
@Test
public void consumesTest() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
OpenAPI3RouterFactory.create(this.vertx, "src/test/resources/swaggers/produces_consumes_test.yaml", openAPI3RouterFactoryAsyncResult -> {
routerFactory = openAPI3RouterFactoryAsyncResult.result();
routerFactory.setOptions(new RouterFactoryOptions().setMountNotImplementedHandler(false));
routerFactory.addHandlerByOperationId("consumesTest", routingContext -> {
RequestParameters params = routingContext.get("parsedParameters");
if (params.body() != null && params.body().isJsonObject()) {
routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("Content-Type", "application/json").end(params.body().getJsonObject().encode());
} else {
routingContext.response().setStatusCode(200).setStatusMessage("OK").end();
}
});
latch.countDown();
});
awaitLatch(latch);
startServer();
// Json consumes test
JsonObject obj = new JsonObject("{\"name\":\"francesco\"}");
testRequestWithJSON(HttpMethod.POST, "/consumesTest", obj, 200, "OK", obj);
// Form consumes tests
MultiMap form = MultiMap.caseInsensitiveMultiMap();
form.add("name", "francesco");
testRequestWithForm(HttpMethod.POST, "/consumesTest", FormType.FORM_URLENCODED, form, 200, "OK");
testRequestWithForm(HttpMethod.POST, "/consumesTest", FormType.MULTIPART, form, 404, "Not Found");
}
use of io.vertx.ext.web.api.contract.RouterFactoryOptions in project vertx-web by vert-x3.
the class OpenAPI3SchemasTest method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
// Have to stop default server of WebTestBase
stopServer();
startSchemaServer();
CountDownLatch latch = new CountDownLatch(1);
OpenAPI3RouterFactory.create(this.vertx, OAS_PATH, openAPI3RouterFactoryAsyncResult -> {
assertTrue(openAPI3RouterFactoryAsyncResult.succeeded());
assertNull(openAPI3RouterFactoryAsyncResult.cause());
routerFactory = openAPI3RouterFactoryAsyncResult.result();
routerFactory.setOptions(new RouterFactoryOptions().setRequireSecurityHandlers(false).setMountValidationFailureHandler(true).setValidationFailureHandler(FAILURE_HANDLER).setMountNotImplementedHandler(false));
latch.countDown();
});
awaitLatch(latch);
client = vertx.createHttpClient(new HttpClientOptions().setDefaultPort(8080));
}
use of io.vertx.ext.web.api.contract.RouterFactoryOptions in project vertx-web by vert-x3.
the class OpenAPI3Examples method mountOptions.
public void mountOptions(AsyncResult<OpenAPI3RouterFactory> ar) {
OpenAPI3RouterFactory routerFactory = ar.result();
// Create and mount options to router factory
RouterFactoryOptions options = new RouterFactoryOptions().setMountNotImplementedHandler(true).setMountValidationFailureHandler(false);
routerFactory.setOptions(options);
}
Aggregations