use of io.vertx.ext.web.validation.RequestParameters in project vertx-web by vert-x3.
the class OpenAPI3ParametersUnitTest method testQueryFormNoexplodeObject.
/**
* Test: query_form_noexplode_object
* Expected parameters sent:
* color: color=R,100,G,200,B,150
* Expected response: {"color":{"R":"100","G":"200","B":"150"}}
*/
@Test
public void testQueryFormNoexplodeObject(Vertx vertx, VertxTestContext testContext) {
loadBuilderAndStartServer(vertx, SPEC_URL, testContext, routerBuilder -> {
routerBuilder.setOptions(new RouterBuilderOptions().setMountNotImplementedHandler(false));
routerBuilder.operation("query_form_noexplode_object").handler(routingContext -> {
RequestParameters params = routingContext.get("parsedParameters");
JsonObject res = new JsonObject();
testContext.verify(() -> {
RequestParameter color_query = params.queryParameter("color");
assertThat(color_query).isNotNull();
assertThat(color_query.isJsonObject()).isTrue();
res.put("color", color_query.getJsonObject());
});
routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("content-type", "application/json; charset=utf-8").end(res.encode());
});
}).onComplete(h -> {
Map<String, Object> color_query;
color_query = new HashMap<>();
color_query.put("R", "100");
color_query.put("G", "200");
color_query.put("B", "150");
apiClient.queryFormNoexplodeObject(color_query, (AsyncResult<HttpResponse> ar) -> {
if (ar.succeeded()) {
testContext.verify(() -> {
assertThat(ar.result().bodyAsJsonObject()).isEqualTo(new JsonObject("{\"color\":{\"R\":\"100\",\"G\":\"200\",\"B\":\"150\"}}"));
});
testContext.completeNow();
} else {
testContext.failNow(ar.cause());
}
});
});
}
use of io.vertx.ext.web.validation.RequestParameters in project vertx-web by vert-x3.
the class OpenAPI3ParametersUnitTest method testQueryFormNoexplodeEmpty.
/**
* Test: query_form_noexplode_empty
* Expected parameters sent:
* color: color=
* Expected response: {"color":null}
*/
@Test
public void testQueryFormNoexplodeEmpty(Vertx vertx, VertxTestContext testContext) {
loadBuilderAndStartServer(vertx, SPEC_URL, testContext, routerBuilder -> {
routerBuilder.setOptions(new RouterBuilderOptions().setMountNotImplementedHandler(false));
routerBuilder.operation("query_form_noexplode_empty").handler(routingContext -> {
RequestParameters params = routingContext.get("parsedParameters");
JsonObject res = new JsonObject();
testContext.verify(() -> {
RequestParameter color_query = params.queryParameter("color");
assertThat(color_query).isNotNull();
assertThat(color_query.isEmpty()).isTrue();
res.putNull("color");
});
routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("content-type", "application/json; charset=utf-8").end(res.encode());
});
}).onComplete(h -> {
String color_query;
color_query = "";
apiClient.queryFormNoexplodeEmpty(color_query, (AsyncResult<HttpResponse> ar) -> {
if (ar.succeeded()) {
testContext.verify(() -> {
assertThat(ar.result().bodyAsJsonObject()).isEqualTo(new JsonObject("{\"color\":null}"));
});
testContext.completeNow();
} else {
testContext.failNow(ar.cause());
}
});
});
}
use of io.vertx.ext.web.validation.RequestParameters in project vertx-web by vert-x3.
the class RouterBuilderIntegrationTest method testQueryParameterArrayDefaultStyle.
@Test
public void testQueryParameterArrayDefaultStyle(Vertx vertx, VertxTestContext testContext) {
Checkpoint checkpoint = testContext.checkpoint(2);
loadBuilderAndStartServer(vertx, VALIDATION_SPEC, testContext, routerBuilder -> {
routerBuilder.setOptions(HANDLERS_TESTS_OPTIONS);
routerBuilder.operation("arrayTest").handler(routingContext -> {
RequestParameters params = routingContext.get("parsedParameters");
String serialized = params.queryParameter("parameter").getJsonArray().stream().map(Object::toString).collect(Collectors.joining(","));
routingContext.response().setStatusMessage(serialized).end();
});
}).onComplete(h -> {
String serialized = String.join(",", "4", "2", "26");
testRequest(client, HttpMethod.GET, "/queryTests/arrayTests/default?parameter=" + serialized).expect(statusCode(200), statusMessage(serialized)).send(testContext, checkpoint);
testRequest(client, HttpMethod.GET, "/queryTests/arrayTests/default?parameter=" + String.join(",", "4", "1", "26")).expect(statusCode(400)).expect(badParameterResponse(ParameterProcessorException.ParameterProcessorErrorType.VALIDATION_ERROR, "parameter", ParameterLocation.QUERY)).send(testContext, checkpoint);
});
}
use of io.vertx.ext.web.validation.RequestParameters in project vertx-web by vert-x3.
the class RouterBuilderIntegrationTest method mountHandlersOrderTest.
@Test
public void mountHandlersOrderTest(Vertx vertx, VertxTestContext testContext) {
Checkpoint checkpoint = testContext.checkpoint(2);
loadBuilderAndStartServer(vertx, "src/test/resources/specs/test_order_spec.yaml", testContext, routerBuilder -> {
routerBuilder.setOptions(new RouterBuilderOptions().setMountNotImplementedHandler(false));
routerBuilder.operation("showSpecialProduct").handler(routingContext -> routingContext.response().setStatusMessage("special").end());
routerBuilder.operation("showProductById").handler(routingContext -> {
RequestParameters params = routingContext.get("parsedParameters");
routingContext.response().setStatusMessage(params.pathParameter("id").getInteger().toString()).end();
});
testContext.completeNow();
}).onComplete(h -> {
testRequest(client, HttpMethod.GET, "/product/special").expect(statusCode(200), statusMessage("special")).send(testContext, checkpoint);
testRequest(client, HttpMethod.GET, "/product/123").expect(statusCode(200), statusMessage("123")).send(testContext, checkpoint);
});
}
use of io.vertx.ext.web.validation.RequestParameters in project vertx-web by vert-x3.
the class RouterBuilderIntegrationTest method testFormArrayParameter.
@Test
public void testFormArrayParameter(Vertx vertx, VertxTestContext testContext) {
Checkpoint checkpoint = testContext.checkpoint(2);
loadBuilderAndStartServer(vertx, VALIDATION_SPEC, testContext, routerBuilder -> {
routerBuilder.setOptions(HANDLERS_TESTS_OPTIONS);
routerBuilder.operation("formArrayTest").handler(routingContext -> {
RequestParameters params = routingContext.get("parsedParameters");
String serialized = params.body().getJsonObject().getJsonArray("values").stream().map(Object::toString).collect(Collectors.joining(","));
routingContext.response().setStatusMessage(params.body().getJsonObject().getString("id") + serialized).end();
});
}).onComplete(h -> {
testRequest(client, HttpMethod.POST, "/formTests/arraytest").expect(statusCode(200), statusMessage("a+b+c" + "10,8,4")).sendURLEncodedForm(MultiMap.caseInsensitiveMultiMap().add("id", "a+b+c").add("values", (Iterable<String>) Arrays.asList("10", "8", "4")), testContext, checkpoint);
testRequest(client, HttpMethod.POST, "/formTests/arraytest").expect(statusCode(400)).expect(badBodyResponse(BodyProcessorException.BodyProcessorErrorType.PARSING_ERROR)).sendURLEncodedForm(MultiMap.caseInsensitiveMultiMap().add("id", "id").add("values", (Iterable<String>) Arrays.asList("10", "bla", "4")), testContext, checkpoint);
});
}
Aggregations