use of io.vertx.ext.web.api.contract.RouterFactoryOptions in project vertx-web by vert-x3.
the class OpenAPI3ParametersUnitTest method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
// Have to stop default server of WebTestBase
stopServer();
apiClient = new io.vertx.ext.web.api.contract.openapi3.ApiClient(webClient);
routerFactory = new OpenAPI3RouterFactoryImpl(this.vertx, spec);
routerFactory.setOptions(new RouterFactoryOptions().setRequireSecurityHandlers(false).setMountValidationFailureHandler(true).setValidationFailureHandler(generateFailureHandler()).setMountNotImplementedHandler(false));
}
use of io.vertx.ext.web.api.contract.RouterFactoryOptions in project vertx-web by vert-x3.
the class OpenAPI3RouterFactoryTest method requireGlobalSecurityHandler.
@Test
public void requireGlobalSecurityHandler() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
OpenAPI3RouterFactory.create(this.vertx, "src/test/resources/swaggers/global_security_test.yaml", openAPI3RouterFactoryAsyncResult -> {
routerFactory = openAPI3RouterFactoryAsyncResult.result();
routerFactory.setOptions(new RouterFactoryOptions().setRequireSecurityHandlers(true));
routerFactory.addHandlerByOperationId("listPets", routingContext -> {
routingContext.response().setStatusCode(200).setStatusMessage(routingContext.get("message") + "-OK").end();
});
latch.countDown();
});
awaitLatch(latch);
assertThrow(routerFactory::getRouter, RouterFactoryException.class);
routerFactory.addSecurityHandler("global_api_key", routingContext -> routingContext.put("message", "Global").next());
routerFactory.addSecurityHandler("api_key", routingContext -> routingContext.put("message", routingContext.get("message") + "-Local").next());
startServer();
testRequest(HttpMethod.GET, "/pets", 200, "Global-Local-OK");
}
use of io.vertx.ext.web.api.contract.RouterFactoryOptions in project vertx-web by vert-x3.
the class OpenAPI3RouterFactoryTest method notMountValidationFailureHandler.
@Test
public void notMountValidationFailureHandler() 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(false).setMountValidationFailureHandler(false));
routerFactory.addHandlerByOperationId("listPets", routingContext -> {
routingContext.response().setStatusCode(200).setStatusMessage(((RequestParameters) routingContext.get("parsedParameters")).queryParameter("limit").toString()).end();
});
routerFactory.addFailureHandlerByOperationId("listPets", routingContext -> {
routingContext.response().setStatusCode((routingContext.failure() instanceof ValidationException) ? 400 : 500).setStatusMessage((routingContext.failure() instanceof ValidationException) ? "Very very Bad Request" : "Error").end();
});
latch.countDown();
});
awaitLatch(latch);
startServer();
testRequest(HttpMethod.GET, "/pets?limit=hello", 400, "Very very Bad Request");
testRequest(HttpMethod.GET, "/pets?limit=10", 200, "10");
}
use of io.vertx.ext.web.api.contract.RouterFactoryOptions in project vertx-web by vert-x3.
the class OpenAPI3RouterFactoryTest method mountHandlersOrderTest.
@Test
public void mountHandlersOrderTest() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
OpenAPI3RouterFactory.create(this.vertx, "src/test/resources/swaggers/test_order_spec.yaml", openAPI3RouterFactoryAsyncResult -> {
assertTrue(openAPI3RouterFactoryAsyncResult.succeeded());
routerFactory = openAPI3RouterFactoryAsyncResult.result();
routerFactory.setOptions(new RouterFactoryOptions().setMountNotImplementedHandler(false));
routerFactory.addHandlerByOperationId("showSpecialProduct", routingContext -> routingContext.response().setStatusMessage("special").end());
routerFactory.addFailureHandlerByOperationId("showSpecialProduct", generateFailureHandler(false));
routerFactory.addHandlerByOperationId("showProductById", routingContext -> {
RequestParameters params = routingContext.get("parsedParameters");
routingContext.response().setStatusMessage(params.pathParameter("id").getInteger().toString()).end();
});
routerFactory.addFailureHandlerByOperationId("showProductById", generateFailureHandler(false));
latch.countDown();
});
awaitLatch(latch);
startServer();
testRequest(HttpMethod.GET, "/product/special", 200, "special");
testRequest(HttpMethod.GET, "/product/123", 200, "123");
stopServer();
}
use of io.vertx.ext.web.api.contract.RouterFactoryOptions in project vertx-web by vert-x3.
the class OpenAPI3RouterFactoryTest method requireSecurityHandler.
@Test
public void requireSecurityHandler() 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("listPets", routingContext -> {
routingContext.response().setStatusCode(200).setStatusMessage(routingContext.get("message") + "OK").end();
});
latch.countDown();
});
awaitLatch(latch);
assertThrow(routerFactory::getRouter, RouterFactoryException.class);
routerFactory.addSecurityHandler("api_key", routingContext -> routingContext.next());
routerFactory.addSecurityHandler("second_api_key", routingContext -> routingContext.next());
routerFactory.addSecurityHandler("third_api_key", routingContext -> routingContext.next());
assertNotThrow(routerFactory::getRouter, RouterFactoryException.class);
}
Aggregations