Search in sources :

Example 6 with RouterFactoryOptions

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

Example 7 with RouterFactoryOptions

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

Example 8 with RouterFactoryOptions

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

Example 9 with RouterFactoryOptions

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

Example 10 with RouterFactoryOptions

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

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