Search in sources :

Example 26 with RequestParameters

use of io.vertx.ext.web.api.RequestParameters in project vertx-web by vert-x3.

the class OpenAPI3ParametersUnitTest method testQueryPipeDelimitedNoexplodeObject.

/**
 * Test: query_pipeDelimited_noexplode_object
 * Expected parameters sent:
 * color: R|100|G|200|B|150
 * Expected response: {"color":{"R":"100","G":"200","B":"150"}}
 * @throws Exception
 */
@Test
public void testQueryPipeDelimitedNoexplodeObject() throws Exception {
    routerFactory.addHandlerByOperationId("query_pipeDelimited_noexplode_object", routingContext -> {
        RequestParameters params = routingContext.get("parsedParameters");
        JsonObject res = new JsonObject();
        RequestParameter color_query = params.queryParameter("color");
        assertNotNull(color_query);
        assertTrue(color_query.isObject());
        Map<String, String> map = new HashMap<>();
        for (String key : color_query.getObjectKeys()) map.put(key, color_query.getObjectValue(key).getString());
        res.put("color", map);
        routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("content-type", "application/json; charset=utf-8").end(res.encode());
    });
    CountDownLatch latch = new CountDownLatch(1);
    Map<String, Object> color_query;
    color_query = new HashMap<>();
    color_query.put("R", "100");
    color_query.put("G", "200");
    color_query.put("B", "150");
    startServer();
    apiClient.queryPipeDelimitedNoexplodeObject(color_query, (AsyncResult<HttpResponse> ar) -> {
        if (ar.succeeded()) {
            assertEquals(200, ar.result().statusCode());
            assertTrue("Expected: " + new JsonObject("{\"color\":{\"R\":\"100\",\"G\":\"200\",\"B\":\"150\"}}").encode() + " Actual: " + ar.result().bodyAsJsonObject().encode(), new JsonObject("{\"color\":{\"R\":\"100\",\"G\":\"200\",\"B\":\"150\"}}").equals(ar.result().bodyAsJsonObject()));
        } else {
            assertTrue(ar.cause().getMessage(), false);
        }
        latch.countDown();
    });
    awaitLatch(latch);
}
Also used : HashMap(java.util.HashMap) RequestParameter(io.vertx.ext.web.api.RequestParameter) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncResult(io.vertx.core.AsyncResult) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Example 27 with RequestParameters

use of io.vertx.ext.web.api.RequestParameters in project vertx-web by vert-x3.

the class OpenAPI3ParametersUnitTest method testPathSimpleExplodeString.

/**
 * Test: path_simple_explode_string
 * Expected parameters sent:
 * color: blue
 * Expected response: {"color":"blue"}
 * @throws Exception
 */
@Test
public void testPathSimpleExplodeString() throws Exception {
    routerFactory.addHandlerByOperationId("path_simple_explode_string", routingContext -> {
        RequestParameters params = routingContext.get("parsedParameters");
        JsonObject res = new JsonObject();
        RequestParameter color_path = params.pathParameter("color");
        assertNotNull(color_path);
        assertTrue(color_path.isString());
        assertEquals(color_path.getString(), "blue");
        res.put("color", color_path.getString());
        routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("content-type", "application/json; charset=utf-8").end(res.encode());
    });
    CountDownLatch latch = new CountDownLatch(1);
    String color_path;
    color_path = "blue";
    startServer();
    apiClient.pathSimpleExplodeString(color_path, (AsyncResult<HttpResponse> ar) -> {
        if (ar.succeeded()) {
            assertEquals(200, ar.result().statusCode());
            assertTrue("Expected: " + new JsonObject("{\"color\":\"blue\"}").encode() + " Actual: " + ar.result().bodyAsJsonObject().encode(), new JsonObject("{\"color\":\"blue\"}").equals(ar.result().bodyAsJsonObject()));
        } else {
            assertTrue(ar.cause().getMessage(), false);
        }
        latch.countDown();
    });
    awaitLatch(latch);
}
Also used : RequestParameter(io.vertx.ext.web.api.RequestParameter) JsonObject(io.vertx.core.json.JsonObject) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncResult(io.vertx.core.AsyncResult) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Example 28 with RequestParameters

use of io.vertx.ext.web.api.RequestParameters 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 29 with RequestParameters

use of io.vertx.ext.web.api.RequestParameters in project vertx-web by vert-x3.

the class OpenAPI3RouterFactoryTest method mountCustomValidationFailureHandler.

@Test
public void mountCustomValidationFailureHandler() 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(HANDLERS_TESTS_OPTIONS.setValidationFailureHandler(routingContext -> routingContext.response().setStatusCode(400).setStatusMessage("Very very Bad Request").end()));
        routerFactory.addHandlerByOperationId("listPets", routingContext -> {
            routingContext.response().setStatusCode(200).setStatusMessage(((RequestParameters) routingContext.get("parsedParameters")).queryParameter("limit").toString()).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) MultiMap(io.vertx.core.MultiMap) Router(io.vertx.ext.web.Router) RouterFactoryException(io.vertx.ext.web.api.contract.RouterFactoryException) Test(org.junit.Test) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) Collectors(java.util.stream.Collectors) RouterFactoryOptions(io.vertx.ext.web.api.contract.RouterFactoryOptions) CountDownLatch(java.util.concurrent.CountDownLatch) URLEncoder(java.net.URLEncoder) List(java.util.List) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) HttpMethod(io.vertx.core.http.HttpMethod) RequestParameters(io.vertx.ext.web.api.RequestParameters) HttpServerOptions(io.vertx.core.http.HttpServerOptions) JsonObject(io.vertx.core.json.JsonObject) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Handler(io.vertx.core.Handler) WebTestWithWebClientBase(io.vertx.ext.web.WebTestWithWebClientBase) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 30 with RequestParameters

use of io.vertx.ext.web.api.RequestParameters 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)

Aggregations

RequestParameters (io.vertx.ext.web.api.RequestParameters)84 Test (org.junit.Test)79 JsonObject (io.vertx.core.json.JsonObject)59 RequestParameter (io.vertx.ext.web.api.RequestParameter)58 CountDownLatch (java.util.concurrent.CountDownLatch)52 AsyncResult (io.vertx.core.AsyncResult)48 HashMap (java.util.HashMap)32 ArrayList (java.util.ArrayList)24 RouterFactoryOptions (io.vertx.ext.web.api.contract.RouterFactoryOptions)22 HttpServerOptions (io.vertx.core.http.HttpServerOptions)21 Operation (io.swagger.v3.oas.models.Operation)20 OpenAPI3RequestValidationHandlerImpl (io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RequestValidationHandlerImpl)20 Handler (io.vertx.core.Handler)18 RoutingContext (io.vertx.ext.web.RoutingContext)18 Paths (java.nio.file.Paths)18 List (java.util.List)18 Collectors (java.util.stream.Collectors)18 OpenAPI (io.swagger.v3.oas.models.OpenAPI)17 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)17 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)17