Search in sources :

Example 6 with RequestParameters

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

the class RouterBuilderIntegrationTest method wildcardMultipartEncoding.

@Test
public void wildcardMultipartEncoding(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("testMultipartWildcard").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.txt", "src/test/resources/random.txt", "text/plain").attribute("type", "text/plain");
        testRequest(client, HttpMethod.POST, "/testMultipartWildcard").expect(statusCode(200), statusMessage("text/plain")).sendMultipartForm(form1, testContext, checkpoint);
        MultipartForm form2 = MultipartForm.create().binaryFileUpload("file1", "random.csv", "src/test/resources/random.csv", "text/csv").attribute("type", "text/csv");
        testRequest(client, HttpMethod.POST, "/testMultipartWildcard").expect(statusCode(200), statusMessage("text/csv")).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, "/testMultipartWildcard").expect(statusCode(400)).sendMultipartForm(form3, testContext, checkpoint);
    });
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) Arrays(java.util.Arrays) RequestParameters(io.vertx.ext.web.validation.RequestParameters) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MultiMap(io.vertx.core.MultiMap) Router(io.vertx.ext.web.Router) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) BodyProcessorException(io.vertx.ext.web.validation.BodyProcessorException) Timeout(io.vertx.junit5.Timeout) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MultipartForm(io.vertx.ext.web.multipart.MultipartForm) ParameterLocation(io.vertx.ext.web.validation.impl.ParameterLocation) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) JsonObject(io.vertx.core.json.JsonObject) RequestParameter(io.vertx.ext.web.validation.RequestParameter) Route(io.vertx.ext.web.Route) QueryStringEncoder(io.netty.handler.codec.http.QueryStringEncoder) StaticHandler(io.vertx.ext.web.handler.StaticHandler) Vertx(io.vertx.core.Vertx) TestRequest(io.vertx.ext.web.validation.testutils.TestRequest) HttpHeaders(io.vertx.core.http.HttpHeaders) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) Stream(java.util.stream.Stream) ValidationTestUtils(io.vertx.ext.web.validation.testutils.ValidationTestUtils) Buffer(io.vertx.core.buffer.Buffer) HttpMethod(io.vertx.core.http.HttpMethod) ParameterProcessorException(io.vertx.ext.web.validation.ParameterProcessorException) Condition(org.assertj.core.api.Condition) Checkpoint(io.vertx.junit5.Checkpoint) Pattern(java.util.regex.Pattern) Handler(io.vertx.core.Handler) Checkpoint(io.vertx.junit5.Checkpoint) MultipartForm(io.vertx.ext.web.multipart.MultipartForm) RequestParameters(io.vertx.ext.web.validation.RequestParameters) Test(org.junit.jupiter.api.Test)

Example 7 with RequestParameters

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

the class RouterBuilderIntegrationTest method testAllOfQueryParam.

@Test
public void testAllOfQueryParam(Vertx vertx, VertxTestContext testContext) {
    Checkpoint checkpoint = testContext.checkpoint(4);
    loadBuilderAndStartServer(vertx, VALIDATION_SPEC, testContext, routerBuilder -> {
        routerBuilder.setOptions(HANDLERS_TESTS_OPTIONS);
        routerBuilder.operation("alloftest").handler(routingContext -> {
            RequestParameters params = routingContext.get("parsedParameters");
            routingContext.response().setStatusMessage("" + params.queryParameter("parameter").getJsonObject().getInteger("a") + params.queryParameter("parameter").getJsonObject().getBoolean("b")).end();
        });
    }).onComplete(h -> {
        testRequest(client, HttpMethod.GET, "/queryTests/allOfTest?parameter=a,5,b,true").expect(statusCode(200), statusMessage("5true")).send(testContext, checkpoint);
        testRequest(client, HttpMethod.GET, "/queryTests/allOfTest?parameter=a,5,b,").expect(statusCode(400)).expect(badParameterResponse(ParameterProcessorException.ParameterProcessorErrorType.VALIDATION_ERROR, "parameter", ParameterLocation.QUERY)).send(testContext, checkpoint);
        testRequest(client, HttpMethod.GET, "/queryTests/allOfTest?parameter=a,5").expect(statusCode(200), statusMessage("5false")).send(testContext, checkpoint);
        testRequest(client, HttpMethod.GET, "/queryTests/allOfTest?parameter=a,5,b,bla").expect(statusCode(400)).expect(badParameterResponse(ParameterProcessorException.ParameterProcessorErrorType.PARSING_ERROR, "parameter", ParameterLocation.QUERY)).send(testContext, checkpoint);
    });
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) Arrays(java.util.Arrays) RequestParameters(io.vertx.ext.web.validation.RequestParameters) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MultiMap(io.vertx.core.MultiMap) Router(io.vertx.ext.web.Router) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) BodyProcessorException(io.vertx.ext.web.validation.BodyProcessorException) Timeout(io.vertx.junit5.Timeout) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MultipartForm(io.vertx.ext.web.multipart.MultipartForm) ParameterLocation(io.vertx.ext.web.validation.impl.ParameterLocation) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) JsonObject(io.vertx.core.json.JsonObject) RequestParameter(io.vertx.ext.web.validation.RequestParameter) Route(io.vertx.ext.web.Route) QueryStringEncoder(io.netty.handler.codec.http.QueryStringEncoder) StaticHandler(io.vertx.ext.web.handler.StaticHandler) Vertx(io.vertx.core.Vertx) TestRequest(io.vertx.ext.web.validation.testutils.TestRequest) HttpHeaders(io.vertx.core.http.HttpHeaders) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) Stream(java.util.stream.Stream) ValidationTestUtils(io.vertx.ext.web.validation.testutils.ValidationTestUtils) Buffer(io.vertx.core.buffer.Buffer) HttpMethod(io.vertx.core.http.HttpMethod) ParameterProcessorException(io.vertx.ext.web.validation.ParameterProcessorException) Condition(org.assertj.core.api.Condition) Checkpoint(io.vertx.junit5.Checkpoint) Pattern(java.util.regex.Pattern) Handler(io.vertx.core.Handler) Checkpoint(io.vertx.junit5.Checkpoint) RequestParameters(io.vertx.ext.web.validation.RequestParameters) Test(org.junit.jupiter.api.Test)

Example 8 with RequestParameters

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

the class RouterBuilderIntegrationTest method testQueryOptionalFormExplodeObject.

/**
 * Test: query_optional_form_explode_object
 */
@Test
public void testQueryOptionalFormExplodeObject(Vertx vertx, VertxTestContext testContext) {
    Checkpoint checkpoint = testContext.checkpoint(2);
    loadBuilderAndStartServer(vertx, VALIDATION_SPEC, testContext, routerBuilder -> {
        routerBuilder.operation("query_form_explode_object").handler(routingContext -> {
            RequestParameters params = routingContext.get("parsedParameters");
            routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("content-type", "application/json").end(params.queryParameter("color").getJsonObject().encode());
        });
    }).onComplete(h -> {
        testRequest(client, HttpMethod.GET, "/query/form/explode/object?R=100&G=200&B=150&alpha=50").expect(statusCode(200)).expect(jsonBodyResponse(new JsonObject("{\"R\":\"100\",\"G\":\"200\",\"B\":\"150\",\"alpha\":50}"))).send(testContext, checkpoint);
        testRequest(client, HttpMethod.GET, "/query/form/explode/object?R=100&G=200&B=150&alpha=aaa").expect(statusCode(400)).expect(badParameterResponse(ParameterProcessorException.ParameterProcessorErrorType.PARSING_ERROR, "color", ParameterLocation.QUERY)).send(testContext, checkpoint);
    });
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) Arrays(java.util.Arrays) RequestParameters(io.vertx.ext.web.validation.RequestParameters) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MultiMap(io.vertx.core.MultiMap) Router(io.vertx.ext.web.Router) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) BodyProcessorException(io.vertx.ext.web.validation.BodyProcessorException) Timeout(io.vertx.junit5.Timeout) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MultipartForm(io.vertx.ext.web.multipart.MultipartForm) ParameterLocation(io.vertx.ext.web.validation.impl.ParameterLocation) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) JsonObject(io.vertx.core.json.JsonObject) RequestParameter(io.vertx.ext.web.validation.RequestParameter) Route(io.vertx.ext.web.Route) QueryStringEncoder(io.netty.handler.codec.http.QueryStringEncoder) StaticHandler(io.vertx.ext.web.handler.StaticHandler) Vertx(io.vertx.core.Vertx) TestRequest(io.vertx.ext.web.validation.testutils.TestRequest) HttpHeaders(io.vertx.core.http.HttpHeaders) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) Stream(java.util.stream.Stream) ValidationTestUtils(io.vertx.ext.web.validation.testutils.ValidationTestUtils) Buffer(io.vertx.core.buffer.Buffer) HttpMethod(io.vertx.core.http.HttpMethod) ParameterProcessorException(io.vertx.ext.web.validation.ParameterProcessorException) Condition(org.assertj.core.api.Condition) Checkpoint(io.vertx.junit5.Checkpoint) Pattern(java.util.regex.Pattern) Handler(io.vertx.core.Handler) Checkpoint(io.vertx.junit5.Checkpoint) JsonObject(io.vertx.core.json.JsonObject) RequestParameters(io.vertx.ext.web.validation.RequestParameters) Test(org.junit.jupiter.api.Test)

Example 9 with RequestParameters

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

the class RouterBuilderIntegrationTest method testAllowEmptyValueBooleanQueryParameter.

@Test
public void testAllowEmptyValueBooleanQueryParameter(Vertx vertx, VertxTestContext testContext) {
    Checkpoint checkpoint = testContext.checkpoint(3);
    loadBuilderAndStartServer(vertx, VALIDATION_SPEC, testContext, routerBuilder -> {
        routerBuilder.operation("testDefaultBoolean").handler(routingContext -> routingContext.response().setStatusMessage("" + ((RequestParameters) routingContext.get("parsedParameters")).queryParameter("parameter").toString()).end());
    }).onComplete(h -> {
        testRequest(client, HttpMethod.GET, "/queryTests/defaultBoolean?parameter").expect(statusCode(200), statusMessage("false")).send(testContext, checkpoint);
        testRequest(client, HttpMethod.GET, "/queryTests/defaultBoolean").expect(statusCode(200), statusMessage("false")).send(testContext, checkpoint);
        testRequest(client, HttpMethod.GET, "/queryTests/defaultBoolean?parameter=true").expect(statusCode(200), statusMessage("true")).send(testContext, checkpoint);
    });
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) Arrays(java.util.Arrays) RequestParameters(io.vertx.ext.web.validation.RequestParameters) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MultiMap(io.vertx.core.MultiMap) Router(io.vertx.ext.web.Router) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) BodyProcessorException(io.vertx.ext.web.validation.BodyProcessorException) Timeout(io.vertx.junit5.Timeout) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MultipartForm(io.vertx.ext.web.multipart.MultipartForm) ParameterLocation(io.vertx.ext.web.validation.impl.ParameterLocation) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) JsonObject(io.vertx.core.json.JsonObject) RequestParameter(io.vertx.ext.web.validation.RequestParameter) Route(io.vertx.ext.web.Route) QueryStringEncoder(io.netty.handler.codec.http.QueryStringEncoder) StaticHandler(io.vertx.ext.web.handler.StaticHandler) Vertx(io.vertx.core.Vertx) TestRequest(io.vertx.ext.web.validation.testutils.TestRequest) HttpHeaders(io.vertx.core.http.HttpHeaders) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) Stream(java.util.stream.Stream) ValidationTestUtils(io.vertx.ext.web.validation.testutils.ValidationTestUtils) Buffer(io.vertx.core.buffer.Buffer) HttpMethod(io.vertx.core.http.HttpMethod) ParameterProcessorException(io.vertx.ext.web.validation.ParameterProcessorException) Condition(org.assertj.core.api.Condition) Checkpoint(io.vertx.junit5.Checkpoint) Pattern(java.util.regex.Pattern) Handler(io.vertx.core.Handler) Checkpoint(io.vertx.junit5.Checkpoint) RequestParameters(io.vertx.ext.web.validation.RequestParameters) Test(org.junit.jupiter.api.Test)

Example 10 with RequestParameters

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

the class RouterBuilderIntegrationTest method consumesTest.

@Test
public void consumesTest(Vertx vertx, VertxTestContext testContext) {
    Checkpoint checkpoint = testContext.checkpoint(4);
    loadBuilderAndStartServer(vertx, "src/test/resources/specs/produces_consumes_test.yaml", testContext, routerBuilder -> {
        routerBuilder.setOptions(new RouterBuilderOptions().setMountNotImplementedHandler(false));
        routerBuilder.operation("consumesTest").handler(routingContext -> {
            RequestParameters params = routingContext.get("parsedParameters");
            if (params.body() != null && params.body().isJsonObject()) {
                routingContext.response().setStatusCode(200).putHeader("Content-Type", "application/json").end(params.body().getJsonObject().encode());
            } else {
                routingContext.response().setStatusCode(200).end();
            }
        });
    }).onComplete(h -> {
        JsonObject obj = new JsonObject().put("name", "francesco");
        testRequest(client, HttpMethod.POST, "/consumesTest").expect(statusCode(200)).expect(jsonBodyResponse(obj)).sendJson(obj, testContext, checkpoint);
        MultiMap form = MultiMap.caseInsensitiveMultiMap();
        form.add("name", "francesco");
        testRequest(client, HttpMethod.POST, "/consumesTest").expect(statusCode(200)).expect(jsonBodyResponse(obj)).sendURLEncodedForm(form, testContext, checkpoint);
        MultipartForm multipartForm = MultipartForm.create();
        form.add("name", "francesco");
        testRequest(client, HttpMethod.POST, "/consumesTest").expect(statusCode(400)).expect(badBodyResponse(BodyProcessorException.BodyProcessorErrorType.MISSING_MATCHING_BODY_PROCESSOR)).sendMultipartForm(multipartForm, testContext, checkpoint);
        testRequest(client, HttpMethod.POST, "/consumesTest").expect(statusCode(400)).expect(failurePredicateResponse()).send(testContext, checkpoint);
    });
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) Arrays(java.util.Arrays) RequestParameters(io.vertx.ext.web.validation.RequestParameters) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MultiMap(io.vertx.core.MultiMap) Router(io.vertx.ext.web.Router) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) BodyProcessorException(io.vertx.ext.web.validation.BodyProcessorException) Timeout(io.vertx.junit5.Timeout) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MultipartForm(io.vertx.ext.web.multipart.MultipartForm) ParameterLocation(io.vertx.ext.web.validation.impl.ParameterLocation) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) JsonObject(io.vertx.core.json.JsonObject) RequestParameter(io.vertx.ext.web.validation.RequestParameter) Route(io.vertx.ext.web.Route) QueryStringEncoder(io.netty.handler.codec.http.QueryStringEncoder) StaticHandler(io.vertx.ext.web.handler.StaticHandler) Vertx(io.vertx.core.Vertx) TestRequest(io.vertx.ext.web.validation.testutils.TestRequest) HttpHeaders(io.vertx.core.http.HttpHeaders) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) Stream(java.util.stream.Stream) ValidationTestUtils(io.vertx.ext.web.validation.testutils.ValidationTestUtils) Buffer(io.vertx.core.buffer.Buffer) HttpMethod(io.vertx.core.http.HttpMethod) ParameterProcessorException(io.vertx.ext.web.validation.ParameterProcessorException) Condition(org.assertj.core.api.Condition) Checkpoint(io.vertx.junit5.Checkpoint) Pattern(java.util.regex.Pattern) Handler(io.vertx.core.Handler) MultiMap(io.vertx.core.MultiMap) Checkpoint(io.vertx.junit5.Checkpoint) JsonObject(io.vertx.core.json.JsonObject) MultipartForm(io.vertx.ext.web.multipart.MultipartForm) RequestParameters(io.vertx.ext.web.validation.RequestParameters) Test(org.junit.jupiter.api.Test)

Aggregations

Vertx (io.vertx.core.Vertx)68 JsonObject (io.vertx.core.json.JsonObject)68 RequestParameter (io.vertx.ext.web.validation.RequestParameter)68 RequestParameters (io.vertx.ext.web.validation.RequestParameters)68 VertxExtension (io.vertx.junit5.VertxExtension)68 VertxTestContext (io.vertx.junit5.VertxTestContext)68 ArrayList (java.util.ArrayList)68 List (java.util.List)68 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)68 Test (org.junit.jupiter.api.Test)68 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)68 AsyncResult (io.vertx.core.AsyncResult)48 HttpResponse (io.vertx.ext.web.client.HttpResponse)48 HashMap (java.util.HashMap)48 Map (java.util.Map)48 BeforeEach (org.junit.jupiter.api.BeforeEach)48 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)20 QueryStringEncoder (io.netty.handler.codec.http.QueryStringEncoder)20 Future (io.vertx.core.Future)20 Handler (io.vertx.core.Handler)20