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);
}
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);
}
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);
}
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);
}
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)
}
}
Aggregations