Search in sources :

Example 76 with RequestParameters

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

the class OpenAPI3ValidationTest method testNullJson.

@Test
public void testNullJson() throws Exception {
    Operation op = testSpec.getPaths().get("/pets").getPost();
    OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(op, op.getParameters(), testSpec);
    loadHandlers("/pets", HttpMethod.POST, true, validationHandler, (routingContext) -> {
        RequestParameters params = routingContext.get("parsedParameters");
        routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("Content-Type", "application/json").end(params.body().getJsonObject().encode());
    });
    // An empty body should be a non parsable json, not an empty object invalid
    testRequestWithJSON(HttpMethod.POST, "/pets", null, 400, errorMessage(ValidationException.ErrorType.JSON_INVALID));
    // An empty json object should be invalid, because some fields are required
    testRequestWithJSON(HttpMethod.POST, "/pets", new JsonObject(), 400, errorMessage(ValidationException.ErrorType.JSON_INVALID));
}
Also used : OpenAPI3RequestValidationHandlerImpl(io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RequestValidationHandlerImpl) JsonObject(io.vertx.core.json.JsonObject) Operation(io.swagger.v3.oas.models.Operation) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Example 77 with RequestParameters

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

the class OpenAPI3ValidationTest method testAllOfQueryParamWithDefault.

@Test
public void testAllOfQueryParamWithDefault() throws Exception {
    Operation op = testSpec.getPaths().get("/queryTests/allOfTest").getGet();
    OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(op, op.getParameters(), testSpec);
    loadHandlers("/queryTests/allOfTest", HttpMethod.GET, false, validationHandler, (routingContext) -> {
        RequestParameters params = routingContext.get("parsedParameters");
        routingContext.response().setStatusMessage(params.queryParameter("parameter").getObjectValue("a").getInteger().toString() + params.queryParameter("parameter").getObjectValue("b").getBoolean().toString()).end();
    });
    String a = "5";
    String b = "";
    String parameter = "parameter=a," + a + ",b," + b;
    testRequest(HttpMethod.GET, "/queryTests/allOfTest?" + parameter, 200, a + "false");
}
Also used : OpenAPI3RequestValidationHandlerImpl(io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RequestValidationHandlerImpl) Operation(io.swagger.v3.oas.models.Operation) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Example 78 with RequestParameters

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

the class OpenAPI3ValidationTest method testQueryParameterByteFormat.

@Test
public void testQueryParameterByteFormat() throws Exception {
    Operation op = testSpec.getPaths().get("/queryTests/byteFormat").getGet();
    OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(op, op.getParameters(), testSpec);
    loadHandlers("/queryTests/byteFormat", HttpMethod.GET, false, validationHandler, (routingContext) -> {
        RequestParameters params = routingContext.get("parsedParameters");
        routingContext.response().setStatusMessage(params.queryParameter("parameter").getString()).end();
    });
    testRequest(HttpMethod.GET, "/queryTests/byteFormat?parameter=Zm9vYmFyCg==", 200, "Zm9vYmFyCg==");
}
Also used : OpenAPI3RequestValidationHandlerImpl(io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RequestValidationHandlerImpl) Operation(io.swagger.v3.oas.models.Operation) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Example 79 with RequestParameters

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

the class HTTPRequestValidationTest method testFormMultipartParamWithIncludedTypes.

@Test
public void testFormMultipartParamWithIncludedTypes() throws Exception {
    HTTPRequestValidationHandler validationHandler = HTTPRequestValidationHandler.create().addFormParam("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();
    MultiMap form = MultiMap.caseInsensitiveMultiMap();
    form.add("parameter", formParam);
    testRequestWithForm(HttpMethod.POST, "/testFormParam", FormType.MULTIPART, 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 80 with RequestParameters

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

the class HTTPRequestValidationTest method testQueryParamsFailureWithIncludedTypes.

@Test
public void testQueryParamsFailureWithIncludedTypes() 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());
    }).failureHandler(generateFailureHandler(true));
    QueryStringEncoder encoder = new QueryStringEncoder("/testQueryParams");
    encoder.addParam("param1", getFailureSample(ParameterType.BOOL));
    encoder.addParam("param2", getFailureSample(ParameterType.INT));
    testRequest(HttpMethod.GET, encoder.toString(), 400, "failure:NO_MATCH");
}
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)

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