use of io.vertx.ext.web.validation.RequestParameters in project vertx-web by vert-x3.
the class RouterBuilderIntegrationTest method testSharedRequestBody.
@Test
public void testSharedRequestBody(Vertx vertx, VertxTestContext testContext) {
Checkpoint checkpoint = testContext.checkpoint(2);
loadBuilderAndStartServer(vertx, "src/test/resources/specs/shared_request_body.yaml", testContext, routerBuilder -> {
routerBuilder.setOptions(HANDLERS_TESTS_OPTIONS);
final Handler<RoutingContext> handler = routingContext -> {
RequestParameters params = routingContext.get("parsedParameters");
RequestParameter body = params.body();
JsonObject jsonBody = body.getJsonObject();
routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("Content-Type", "application/json").end(jsonBody.toBuffer());
};
routerBuilder.operation("thisWayWorks").handler(handler);
routerBuilder.operation("thisWayBroken").handler(handler);
}).onComplete(h -> {
JsonObject obj = new JsonObject().put("id", "aaa").put("name", "bla");
testRequest(client, HttpMethod.POST, "/v1/working").expect(statusCode(200)).expect(jsonBodyResponse(obj)).sendJson(obj, testContext, checkpoint);
testRequest(client, HttpMethod.POST, "/v1/notworking").expect(statusCode(200)).expect(jsonBodyResponse(obj)).sendJson(obj, testContext, checkpoint);
});
}
use of io.vertx.ext.web.validation.RequestParameters in project vertx-web by vert-x3.
the class RouterBuilderIntegrationTest method testQueryParameterArrayExploded.
@Test
public void testQueryParameterArrayExploded(Vertx vertx, VertxTestContext testContext) {
loadBuilderAndStartServer(vertx, VALIDATION_SPEC, testContext, routerBuilder -> {
routerBuilder.operation("arrayTestFormExploded").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 -> {
QueryStringEncoder encoder = new QueryStringEncoder("/queryTests/arrayTests/formExploded");
List<String> values = new ArrayList<>();
values.add("4");
values.add("2");
values.add("26");
for (String s : values) {
encoder.addParam("parameter", s);
}
String serialized = String.join(",", values);
testRequest(client, HttpMethod.GET, encoder.toString()).expect(statusCode(200), statusMessage(serialized)).send(testContext);
});
}
use of io.vertx.ext.web.validation.RequestParameters in project vertx-web by vert-x3.
the class RouterBuilderIntegrationTest method commaSeparatedMultipartEncoding.
@Test
public void commaSeparatedMultipartEncoding(Vertx vertx, VertxTestContext testContext) {
Checkpoint checkpoint = testContext.checkpoint(3);
loadBuilderAndStartServer(vertx, "src/test/resources/specs/multipart.yaml", testContext, routerBuilder -> {
routerBuilder.setOptions(new RouterBuilderOptions().setRequireSecurityHandlers(false));
routerBuilder.operation("testMultipartMultiple").handler(routingContext -> {
RequestParameters params = routingContext.get("parsedParameters");
routingContext.response().setStatusCode(200).setStatusMessage(params.body().getJsonObject().getString("type")).end();
});
}).onComplete(h -> {
MultipartForm form1 = MultipartForm.create().binaryFileUpload("file1", "random-file", "src/test/resources/random-file", "application/octet-stream").attribute("type", "application/octet-stream");
testRequest(client, HttpMethod.POST, "/testMultipartMultiple").expect(statusCode(200), statusMessage("application/octet-stream")).sendMultipartForm(form1, testContext, checkpoint);
MultipartForm form2 = MultipartForm.create().binaryFileUpload("file1", "random.txt", "src/test/resources/random.txt", "text/plain").attribute("type", "text/plain");
testRequest(client, HttpMethod.POST, "/testMultipartMultiple").expect(statusCode(200), statusMessage("text/plain")).sendMultipartForm(form2, testContext, checkpoint);
MultipartForm form3 = MultipartForm.create().binaryFileUpload("file1", "random.txt", "src/test/resources/random.txt", "application/json").attribute("type", "application/json");
testRequest(client, HttpMethod.POST, "/testMultipartMultiple").expect(statusCode(400)).sendMultipartForm(form3, testContext, checkpoint);
});
}
use of io.vertx.ext.web.validation.RequestParameters in project vertx-web by vert-x3.
the class OpenAPI3ParametersUnitTest method testCookieFormExplodeEmpty.
/**
* Test: cookie_form_explode_empty
* Expected parameters sent:
* color: color=
* Expected response: {"color":null}
*/
@Test
public void testCookieFormExplodeEmpty(Vertx vertx, VertxTestContext testContext) {
loadBuilderAndStartServer(vertx, SPEC_URL, testContext, routerBuilder -> {
routerBuilder.setOptions(new RouterBuilderOptions().setMountNotImplementedHandler(false));
routerBuilder.operation("cookie_form_explode_empty").handler(routingContext -> {
RequestParameters params = routingContext.get("parsedParameters");
JsonObject res = new JsonObject();
testContext.verify(() -> {
RequestParameter color_cookie = params.cookieParameter("color");
assertThat(color_cookie).isNotNull();
assertThat(color_cookie.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_cookie;
color_cookie = "";
apiClient.cookieFormExplodeEmpty(color_cookie, (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 OpenAPI3ParametersUnitTest method testQueryPipeDelimitedNoexplodeArray.
/**
* Test: query_pipeDelimited_noexplode_array
* Expected parameters sent:
* color: blue|black|brown
* Expected response: {"color":["blue","black","brown"]}
*/
@Test
public void testQueryPipeDelimitedNoexplodeArray(Vertx vertx, VertxTestContext testContext) {
loadBuilderAndStartServer(vertx, SPEC_URL, testContext, routerBuilder -> {
routerBuilder.setOptions(new RouterBuilderOptions().setMountNotImplementedHandler(false));
routerBuilder.operation("query_pipeDelimited_noexplode_array").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.isJsonArray()).isTrue();
res.put("color", color_query.getJsonArray());
});
routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("content-type", "application/json; charset=utf-8").end(res.encode());
});
}).onComplete(h -> {
List<Object> color_query;
color_query = new ArrayList<>();
color_query.add("blue");
color_query.add("black");
color_query.add("brown");
apiClient.queryPipeDelimitedNoexplodeArray(color_query, (AsyncResult<HttpResponse> ar) -> {
if (ar.succeeded()) {
testContext.verify(() -> {
assertThat(ar.result().bodyAsJsonObject()).isEqualTo(new JsonObject("{\"color\":[\"blue\",\"black\",\"brown\"]}"));
});
testContext.completeNow();
} else {
testContext.failNow(ar.cause());
}
});
});
}
Aggregations