Search in sources :

Example 16 with TestRequest

use of io.vertx.ext.web.validation.testutils.TestRequest 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);
    });
}
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) RequestParameter(io.vertx.ext.web.validation.RequestParameter) BodyHandler(io.vertx.ext.web.handler.BodyHandler) StaticHandler(io.vertx.ext.web.handler.StaticHandler) Handler(io.vertx.core.Handler) JsonObject(io.vertx.core.json.JsonObject) RequestParameters(io.vertx.ext.web.validation.RequestParameters) Test(org.junit.jupiter.api.Test)

Example 17 with TestRequest

use of io.vertx.ext.web.validation.testutils.TestRequest 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);
    });
}
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) ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) QueryStringEncoder(io.netty.handler.codec.http.QueryStringEncoder) RequestParameters(io.vertx.ext.web.validation.RequestParameters) Test(org.junit.jupiter.api.Test)

Example 18 with TestRequest

use of io.vertx.ext.web.validation.testutils.TestRequest 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);
    });
}
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 19 with TestRequest

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

the class RouterBuilderIntegrationTest method concretePathsMatchFirst.

@Test
public void concretePathsMatchFirst(Vertx vertx, VertxTestContext testContext) {
    // make sure /pets/mine is matched first when there is a choice between /pets/{petId} and /pets/mine
    // see https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#patterned-fields
    // also test situations (working in Vert.x 3) not strictly defined by the spec but agreed by one OpenAPI top contributor:
    // https://stackoverflow.com/questions/67382759/are-those-openapi-3-paths-ambiguous
    Checkpoint checkpoint = testContext.checkpoint(4);
    loadBuilderAndStartServer(vertx, "src/test/resources/specs/path_matching_order.yaml", testContext, routerBuilder -> {
        routerBuilder.setOptions(HANDLERS_TESTS_OPTIONS);
        routerBuilder.operation("searchPets").handler(routingContext -> {
            routingContext.response().setStatusMessage("searchPets").end();
        });
        routerBuilder.operation("searchPetsInShop").handler(routingContext -> {
            routingContext.response().setStatusMessage("searchPetsInShop").end();
        });
        routerBuilder.operation("addPet").handler(routingContext -> {
            routingContext.response().setStatusMessage("addPet").end();
        });
        routerBuilder.operation("addPetToShop").handler(routingContext -> {
            routingContext.response().setStatusMessage("addPetToShop").end();
        });
    }).onComplete(h -> {
        testRequest(client, HttpMethod.POST, "/pets/wolfie").expect(statusCode(200), statusMessage("addPet")).sendJson(new JsonObject(), testContext, checkpoint);
        testRequest(client, HttpMethod.POST, "/pets/_search").expect(statusCode(200), statusMessage("searchPets")).send(testContext, checkpoint);
        testRequest(client, HttpMethod.POST, "/shops/mine/pets/wolfie").expect(statusCode(200), statusMessage("addPetToShop")).sendJson(new JsonObject(), testContext, checkpoint);
        testRequest(client, HttpMethod.POST, "/shops/mine/pets/_search").expect(statusCode(200), statusMessage("searchPetsInShop")).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) Test(org.junit.jupiter.api.Test)

Example 20 with TestRequest

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

the class RouterBuilderIntegrationTest method exposeConfigurationTest.

@Test
public void exposeConfigurationTest(Vertx vertx, VertxTestContext testContext) {
    Checkpoint checkpoint = testContext.checkpoint();
    loadBuilderAndStartServer(vertx, "src/test/resources/specs/router_builder_test.yaml", testContext, routerBuilder -> {
        routerBuilder.setOptions(new RouterBuilderOptions().setRequireSecurityHandlers(false).setOperationModelKey("fooBarKey"));
        routerBuilder.operation("listPets").handler(routingContext -> {
            JsonObject operation = routingContext.get("fooBarKey");
            routingContext.response().setStatusCode(200).setStatusMessage(operation.getString("operationId")).end();
        });
    }).onComplete(h -> testRequest(client, HttpMethod.GET, "/pets").expect(statusCode(200), statusMessage("listPets")).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) Test(org.junit.jupiter.api.Test)

Aggregations

Vertx (io.vertx.core.Vertx)56 HttpMethod (io.vertx.core.http.HttpMethod)56 TestRequest (io.vertx.ext.web.validation.testutils.TestRequest)56 Checkpoint (io.vertx.junit5.Checkpoint)56 VertxTestContext (io.vertx.junit5.VertxTestContext)56 Test (org.junit.jupiter.api.Test)56 JsonObject (io.vertx.core.json.JsonObject)55 VertxExtension (io.vertx.junit5.VertxExtension)50 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)50 Future (io.vertx.core.Future)47 Timeout (io.vertx.junit5.Timeout)47 Handler (io.vertx.core.Handler)46 Route (io.vertx.ext.web.Route)46 Router (io.vertx.ext.web.Router)46 RoutingContext (io.vertx.ext.web.RoutingContext)46 Arrays (java.util.Arrays)46 Collectors (java.util.stream.Collectors)46 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)46 Condition (org.assertj.core.api.Condition)46 ArrayList (java.util.ArrayList)36