Search in sources :

Example 1 with RouterFactoryOptions

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));
}
Also used : RoutingContext(io.vertx.ext.web.RoutingContext) OpenAPI3RouterFactoryImpl(io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RouterFactoryImpl) RouterFactoryOptions(io.vertx.ext.web.api.contract.RouterFactoryOptions)

Example 2 with RouterFactoryOptions

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

Example 3 with RouterFactoryOptions

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

Example 4 with RouterFactoryOptions

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

Example 5 with RouterFactoryOptions

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

Aggregations

RouterFactoryOptions (io.vertx.ext.web.api.contract.RouterFactoryOptions)17 CountDownLatch (java.util.concurrent.CountDownLatch)13 Test (org.junit.Test)12 RequestParameters (io.vertx.ext.web.api.RequestParameters)5 ValidationException (io.vertx.ext.web.api.validation.ValidationException)4 MultiMap (io.vertx.core.MultiMap)3 HttpServerOptions (io.vertx.core.http.HttpServerOptions)3 JsonObject (io.vertx.core.json.JsonObject)3 Router (io.vertx.ext.web.Router)3 OpenAPI3RouterFactory (io.vertx.ext.web.api.contract.openapi3.OpenAPI3RouterFactory)3 HttpClientOptions (io.vertx.core.http.HttpClientOptions)2 RoutingContext (io.vertx.ext.web.RoutingContext)2 Handler (io.vertx.core.Handler)1 HttpMethod (io.vertx.core.http.HttpMethod)1 HttpServer (io.vertx.core.http.HttpServer)1 WebTestWithWebClientBase (io.vertx.ext.web.WebTestWithWebClientBase)1 RequestParameter (io.vertx.ext.web.api.RequestParameter)1 RouterFactoryException (io.vertx.ext.web.api.contract.RouterFactoryException)1 OpenAPI3RouterFactoryImpl (io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RouterFactoryImpl)1 BodyHandler (io.vertx.ext.web.handler.BodyHandler)1