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