Search in sources :

Example 41 with RequestParameters

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

the class RouterBuilderIntegrationTest method testJsonBody.

@Test
public void testJsonBody(Vertx vertx, VertxTestContext testContext) {
    Checkpoint checkpoint = testContext.checkpoint(4);
    loadBuilderAndStartServer(vertx, VALIDATION_SPEC, testContext, routerBuilder -> {
        routerBuilder.operation("jsonBodyTest").handler(routingContext -> {
            RequestParameters params = routingContext.get("parsedParameters");
            routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("Content-Type", "application/json").end(params.body().getJsonObject().encode());
        });
    }).onComplete(h -> {
        JsonObject valid = new JsonObject().put("id", "anId").put("values", new JsonArray().add(5).add(10).add(2));
        testRequest(client, HttpMethod.POST, "/jsonBodyTest/sampleTest").expect(statusCode(200), jsonBodyResponse(valid)).sendJson(valid, testContext, checkpoint);
        testRequest(client, HttpMethod.POST, "/jsonBodyTest/sampleTest").with(requestHeader("content-type", "application/json; charset=utf-8")).expect(statusCode(200), jsonBodyResponse(valid)).sendBuffer(valid.toBuffer(), testContext, checkpoint);
        testRequest(client, HttpMethod.POST, "/jsonBodyTest/sampleTest").with(requestHeader("content-type", "application/superapplication+json")).expect(statusCode(200), jsonBodyResponse(valid)).sendBuffer(valid.toBuffer(), testContext, checkpoint);
        JsonObject invalid = new JsonObject().put("id", "anId").put("values", new JsonArray().add(5).add("bla").add(2));
        testRequest(client, HttpMethod.POST, "/jsonBodyTest/sampleTest").expect(statusCode(400)).expect(badBodyResponse(BodyProcessorException.BodyProcessorErrorType.VALIDATION_ERROR)).sendJson(invalid, 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) JsonArray(io.vertx.core.json.JsonArray) 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 42 with RequestParameters

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

the class RouterBuilderIntegrationTest method testComplexMultipart.

@Timeout(2000)
@Test
public void testComplexMultipart(Vertx vertx, VertxTestContext testContext) {
    Checkpoint checkpoint = testContext.checkpoint(2);
    loadBuilderAndStartServer(vertx, VALIDATION_SPEC, testContext, routerBuilder -> {
        routerBuilder.setOptions(HANDLERS_TESTS_OPTIONS);
        routerBuilder.operation("complexMultipartRequest").handler(routingContext -> {
            RequestParameters params = routingContext.get("parsedParameters");
            if (params.body() == null) {
                routingContext.response().setStatusCode(200).end();
            } else {
                routingContext.response().putHeader("content-type", "application/json").setStatusCode(200).end(params.body().getJsonObject().toBuffer());
            }
        });
    }).onComplete(h -> {
        JsonObject pet = new JsonObject();
        pet.put("id", 14612);
        pet.put("name", "Willy");
        MultipartForm form = MultipartForm.create().textFileUpload("param1", "random.txt", "src/test/resources/random.txt", "text/plain").attribute("param2", pet.encode()).textFileUpload("param3", "random.csv", "src/test/resources/random.txt", "text/csv").attribute("param4", "1.2").attribute("param4", "5.2").attribute("param4", "6.2").attribute("param5", "2").binaryFileUpload("param1NotRealBinary", "random-file", "src/test/resources/random-file", "text/plain").binaryFileUpload("param1Binary", "random-file", "src/test/resources/random-file", "application/octet-stream");
        JsonObject expected = new JsonObject().put("param2", pet).put("param4", new JsonArray().add(1.2).add(5.2).add(6.2)).put("param5", 2);
        testRequest(client, HttpMethod.POST, "/multipart/complex").expect(statusCode(200)).expect(jsonBodyResponse(expected)).sendMultipartForm(form, testContext, checkpoint);
        testRequest(client, HttpMethod.POST, "/multipart/complex").expect(statusCode(200)).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) JsonArray(io.vertx.core.json.JsonArray) 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) Timeout(io.vertx.junit5.Timeout)

Example 43 with RequestParameters

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

the class RouterBuilderIntegrationTest method testQueryParameterAnyOf.

@Test
public void testQueryParameterAnyOf(Vertx vertx, VertxTestContext testContext) {
    Checkpoint checkpoint = testContext.checkpoint(5);
    loadBuilderAndStartServer(vertx, VALIDATION_SPEC, testContext, routerBuilder -> {
        routerBuilder.operation("anyOfTest").handler(routingContext -> routingContext.response().setStatusMessage(((RequestParameters) routingContext.get("parsedParameters")).queryParameter("parameter").toString()).end());
    }).onComplete(h -> {
        testRequest(client, HttpMethod.GET, "/queryTests/anyOfTest?parameter=true").expect(statusCode(200), statusMessage("true")).send(testContext, checkpoint);
        testRequest(client, HttpMethod.GET, "/queryTests/anyOfTest?parameter=5").expect(statusCode(200), statusMessage("5")).send(testContext, checkpoint);
        testRequest(client, HttpMethod.GET, "/queryTests/anyOfTest?parameter=5,4").expect(statusCode(200), statusMessage(new JsonArray().add(5).add(4).encode())).send(testContext, checkpoint);
        testRequest(client, HttpMethod.GET, "/queryTests/anyOfTest?parameter=a,5").expect(statusCode(200), statusMessage(new JsonObject().put("a", 5).encode())).send(testContext, checkpoint);
        testRequest(client, HttpMethod.GET, "/queryTests/anyOfTest?parameter=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) JsonArray(io.vertx.core.json.JsonArray) Checkpoint(io.vertx.junit5.Checkpoint) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.jupiter.api.Test)

Example 44 with RequestParameters

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

the class RouterBuilderIntegrationTest method producesTest.

@Test
public void producesTest(Vertx vertx, VertxTestContext testContext) {
    Checkpoint checkpoint = testContext.checkpoint(2);
    loadBuilderAndStartServer(vertx, "src/test/resources/specs/produces_consumes_test.yaml", testContext, routerBuilder -> {
        routerBuilder.setOptions(new RouterBuilderOptions().setMountNotImplementedHandler(false));
        routerBuilder.operation("producesTest").handler(routingContext -> {
            if (((RequestParameters) routingContext.get("parsedParameters")).queryParameter("fail").getBoolean())
                routingContext.response().putHeader("content-type", "text/plain").setStatusCode(500).end("Hate it");
            else
                // ResponseContentTypeHandler does the job for me
                routingContext.response().setStatusCode(200).end("{}");
        });
    }).onComplete(h -> {
        String acceptableContentTypes = String.join(", ", "application/json", "text/plain");
        testRequest(client, HttpMethod.GET, "/producesTest").with(requestHeader("Accept", acceptableContentTypes)).expect(statusCode(200), responseHeader("Content-type", "application/json")).send(testContext, checkpoint);
        testRequest(client, HttpMethod.GET, "/producesTest?fail=true").with(requestHeader("Accept", acceptableContentTypes)).expect(statusCode(500), responseHeader("Content-type", "text/plain")).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 45 with RequestParameters

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

the class OpenAPI3ParametersUnitTest method testHeaderSimpleNoexplodeArray.

/**
 * Test: header_simple_noexplode_array
 * Expected parameters sent:
 * color: blue,black,brown
 * Expected response: {"color":["blue","black","brown"]}
 */
@Test
public void testHeaderSimpleNoexplodeArray(Vertx vertx, VertxTestContext testContext) {
    loadBuilderAndStartServer(vertx, SPEC_URL, testContext, routerBuilder -> {
        routerBuilder.setOptions(new RouterBuilderOptions().setMountNotImplementedHandler(false));
        routerBuilder.operation("header_simple_noexplode_array").handler(routingContext -> {
            RequestParameters params = routingContext.get("parsedParameters");
            JsonObject res = new JsonObject();
            testContext.verify(() -> {
                RequestParameter color_header = params.headerParameter("color");
                assertThat(color_header).isNotNull();
                assertThat(color_header.isJsonArray()).isTrue();
                res.put("color", color_header.getJsonArray());
            });
            routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("content-type", "application/json; charset=utf-8").end(res.encode());
        });
    }).onComplete(h -> {
        List<Object> color_header;
        color_header = new ArrayList<>();
        color_header.add("blue");
        color_header.add("black");
        color_header.add("brown");
        apiClient.headerSimpleNoexplodeArray(color_header, (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());
            }
        });
    });
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) HttpResponse(io.vertx.ext.web.client.HttpResponse) RequestParameters(io.vertx.ext.web.validation.RequestParameters) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Vertx(io.vertx.core.Vertx) HashMap(java.util.HashMap) VertxExtension(io.vertx.junit5.VertxExtension) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) List(java.util.List) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) RequestParameter(io.vertx.ext.web.validation.RequestParameter) AsyncResult(io.vertx.core.AsyncResult) RequestParameter(io.vertx.ext.web.validation.RequestParameter) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) 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