Search in sources :

Example 46 with RequestParameters

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

the class OpenAPI3ValidationTest method testFormArrayParameter.

@Test
public void testFormArrayParameter() throws Exception {
    Operation op = testSpec.getPaths().get("/formTests/arraytest").getPost();
    if (op.getParameters() == null)
        op.setParameters(new ArrayList<>());
    OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(op, op.getParameters(), testSpec);
    loadHandlers("/formTests/arraytest", HttpMethod.POST, false, validationHandler, (routingContext) -> {
        RequestParameters params = routingContext.get("parsedParameters");
        List<String> result = new ArrayList<>();
        for (RequestParameter r : params.formParameter("values").getArray()) result.add(r.getInteger().toString());
        routingContext.response().setStatusMessage(params.formParameter("id").getString() + serializeInCSVStringArray(result)).end();
    });
    String id = "anId";
    List<String> valuesArray = new ArrayList<>();
    for (int i = 0; i < 4; i++) valuesArray.add(getSuccessSample(ParameterType.INT).getInteger().toString());
    String values = serializeInCSVStringArray(valuesArray);
    MultiMap form = MultiMap.caseInsensitiveMultiMap();
    form.add("id", id);
    form.add("values", values);
    testRequestWithForm(HttpMethod.POST, "/formTests/arraytest", FormType.FORM_URLENCODED, form, 200, id + values);
}
Also used : MultiMap(io.vertx.core.MultiMap) OpenAPI3RequestValidationHandlerImpl(io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RequestValidationHandlerImpl) RequestParameter(io.vertx.ext.web.api.RequestParameter) ArrayList(java.util.ArrayList) Operation(io.swagger.v3.oas.models.Operation) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Example 47 with RequestParameters

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

the class HTTPRequestValidationTest method testFormURLEncodedOverrideWithIncludedTypes.

@Test
public void testFormURLEncodedOverrideWithIncludedTypes() throws Exception {
    HTTPRequestValidationHandler validationHandler = HTTPRequestValidationHandler.create().addFormParam("parameter", ParameterType.INT, true).addQueryParam("parameter", ParameterType.INT, true);
    router.route().handler(BodyHandler.create());
    router.post("/testFormParam").handler(validationHandler);
    router.post("/testFormParam").handler(routingContext -> {
        RequestParameters params = routingContext.get("parsedParameters");
        routingContext.response().setStatusMessage(params.formParameter("parameter").getInteger().toString()).end();
    }).failureHandler(generateFailureHandler(false));
    String formParam = getSuccessSample(ParameterType.INT).getInteger().toString();
    String queryParam = getSuccessSample(ParameterType.INT).getInteger().toString();
    MultiMap form = MultiMap.caseInsensitiveMultiMap();
    form.add("parameter", formParam);
    testRequestWithForm(HttpMethod.POST, "/testFormParam?parameter=" + queryParam, FormType.FORM_URLENCODED, form, 200, formParam);
}
Also used : QueryStringEncoder(io.netty.handler.codec.http.QueryStringEncoder) URLEncoder(java.net.URLEncoder) HttpMethod(io.vertx.core.http.HttpMethod) MultiMap(io.vertx.core.MultiMap) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test) BodyHandler(io.vertx.ext.web.handler.BodyHandler) MultiMap(io.vertx.core.MultiMap) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Example 48 with RequestParameters

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

the class HTTPRequestValidationTest method testQueryParamsArrayAndPathParamsWithIncludedTypes.

@Test
public void testQueryParamsArrayAndPathParamsWithIncludedTypes() throws Exception {
    HTTPRequestValidationHandler validationHandler = HTTPRequestValidationHandler.create().addPathParam("pathParam1", ParameterType.INT).addQueryParamsArray("awesomeArray", ParameterType.EMAIL, true).addQueryParam("anotherParam", ParameterType.DOUBLE, true);
    router.get("/testQueryParams/:pathParam1").handler(validationHandler);
    router.get("/testQueryParams/:pathParam1").handler(routingContext -> {
        RequestParameters params = routingContext.get("parsedParameters");
        routingContext.response().setStatusMessage(params.pathParameter("pathParam1").getInteger().toString() + params.queryParameter("awesomeArray").getArray().size() + params.queryParameter("anotherParam").getDouble().toString()).end();
    }).failureHandler(generateFailureHandler(false));
    String pathParam = getSuccessSample(ParameterType.INT).getInteger().toString();
    String arrayValue1 = getSuccessSample(ParameterType.EMAIL).getString();
    String arrayValue2 = getSuccessSample(ParameterType.EMAIL).getString();
    String anotherParam = getSuccessSample(ParameterType.DOUBLE).getDouble().toString();
    QueryStringEncoder encoder = new QueryStringEncoder("/testQueryParams/" + URLEncoder.encode(pathParam, "UTF-8"));
    encoder.addParam("awesomeArray", arrayValue1);
    encoder.addParam("awesomeArray", arrayValue2);
    encoder.addParam("anotherParam", anotherParam);
    testRequest(HttpMethod.GET, encoder.toString(), 200, pathParam + "2" + anotherParam);
}
Also used : QueryStringEncoder(io.netty.handler.codec.http.QueryStringEncoder) URLEncoder(java.net.URLEncoder) HttpMethod(io.vertx.core.http.HttpMethod) MultiMap(io.vertx.core.MultiMap) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test) BodyHandler(io.vertx.ext.web.handler.BodyHandler) QueryStringEncoder(io.netty.handler.codec.http.QueryStringEncoder) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Example 49 with RequestParameters

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

the class HTTPRequestValidationTest method testQueryParamsWithIncludedTypes.

@Test
public void testQueryParamsWithIncludedTypes() throws Exception {
    HTTPRequestValidationHandler validationHandler = HTTPRequestValidationHandler.create().addQueryParam("param1", ParameterType.BOOL, true).addQueryParam("param2", ParameterType.INT, true);
    router.get("/testQueryParams").handler(validationHandler);
    router.get("/testQueryParams").handler(routingContext -> {
        RequestParameters params = routingContext.get("parsedParameters");
        routingContext.response().setStatusMessage(params.queryParameter("param1").getBoolean().toString() + params.queryParameter("param2").getInteger().toString()).end();
    }).failureHandler(generateFailureHandler(false));
    QueryStringEncoder encoder = new QueryStringEncoder("/testQueryParams");
    String param1 = getSuccessSample(ParameterType.BOOL).getBoolean().toString();
    String param2 = getSuccessSample(ParameterType.INT).getInteger().toString();
    encoder.addParam("param1", param1);
    encoder.addParam("param2", param2);
    testRequest(HttpMethod.GET, encoder.toString(), 200, param1 + param2);
}
Also used : QueryStringEncoder(io.netty.handler.codec.http.QueryStringEncoder) URLEncoder(java.net.URLEncoder) HttpMethod(io.vertx.core.http.HttpMethod) MultiMap(io.vertx.core.MultiMap) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test) BodyHandler(io.vertx.ext.web.handler.BodyHandler) QueryStringEncoder(io.netty.handler.codec.http.QueryStringEncoder) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Example 50 with RequestParameters

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

the class ValidationExamples method example3.

public void example3(RoutingContext routingContext) {
    RequestParameters params = routingContext.get("parsedParameters");
    RequestParameter awesomeParameter = params.queryParameter("awesomeParameter");
    if (awesomeParameter != null) {
        if (!awesomeParameter.isEmpty()) {
            // Parameter exists and isn't empty
            // ParameterTypeValidator mapped the parameter in equivalent language object
            Integer awesome = awesomeParameter.getInteger();
        } else {
        // Parameter exists, but it's empty
        }
    } else {
    // Parameter doesn't exist (it's not required)
    }
}
Also used : RequestParameter(io.vertx.ext.web.api.RequestParameter) RequestParameters(io.vertx.ext.web.api.RequestParameters)

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