Search in sources :

Example 46 with RequestParameters

use of io.vertx.ext.web.validation.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"}
 */
@Test
public void testPathSimpleExplodeString(Vertx vertx, VertxTestContext testContext) {
    loadBuilderAndStartServer(vertx, SPEC_URL, testContext, routerBuilder -> {
        routerBuilder.setOptions(new RouterBuilderOptions().setMountNotImplementedHandler(false));
        routerBuilder.operation("path_simple_explode_string").handler(routingContext -> {
            RequestParameters params = routingContext.get("parsedParameters");
            JsonObject res = new JsonObject();
            testContext.verify(() -> {
                RequestParameter color_path = params.pathParameter("color");
                assertThat(color_path).isNotNull();
                assertThat(color_path.isString()).isTrue();
                assertThat(color_path.getString()).isEqualTo("blue");
                res.put("color", color_path.getString());
            });
            routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("content-type", "application/json; charset=utf-8").end(res.encode());
        });
    }).onComplete(h -> {
        String color_path;
        color_path = "blue";
        apiClient.pathSimpleExplodeString(color_path, (AsyncResult<HttpResponse> ar) -> {
            if (ar.succeeded()) {
                testContext.verify(() -> {
                    assertThat(ar.result().bodyAsJsonObject()).isEqualTo(new JsonObject("{\"color\":\"blue\"}"));
                });
                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) AsyncResult(io.vertx.core.AsyncResult) RequestParameters(io.vertx.ext.web.validation.RequestParameters) Test(org.junit.jupiter.api.Test)

Example 47 with RequestParameters

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

the class RouterBuilderIntegrationTest method mountHandlerEncodedTest.

@Test
public void mountHandlerEncodedTest(Vertx vertx, VertxTestContext testContext) {
    Checkpoint checkpoint = testContext.checkpoint();
    loadBuilderAndStartServer(vertx, "src/test/resources/specs/router_builder_test.yaml", testContext, routerBuilder -> {
        routerBuilder.setOptions(HANDLERS_TESTS_OPTIONS);
        routerBuilder.operation("encodedParamTest").handler(routingContext -> {
            RequestParameters params = routingContext.get("parsedParameters");
            assertThat(params.pathParameter("p1").toString()).isEqualTo("a:b");
            assertThat(params.queryParameter("p2").toString()).isEqualTo("a:b");
            routingContext.response().setStatusCode(200).setStatusMessage(params.pathParameter("p1").toString()).end();
        });
        testContext.completeNow();
    }).onComplete(h -> testRequest(client, HttpMethod.GET, "/foo/a%3Ab?p2=a%3Ab").expect(statusCode(200), statusMessage("a:b")).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 48 with RequestParameters

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

the class RouterBuilderIntegrationTest method testJsonBodyWithDate.

@Test
public void testJsonBodyWithDate(Vertx vertx, VertxTestContext testContext) {
    loadBuilderAndStartServer(vertx, VALIDATION_SPEC, testContext, routerBuilder -> {
        routerBuilder.operation("jsonBodyWithDate").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 obj = new JsonObject();
        obj.put("date", "2018-02-18");
        obj.put("dateTime1", "2018-01-01T10:00:00.0000000000000000000000Z");
        obj.put("dateTime2", "2018-01-01T10:00:00+10:00");
        obj.put("dateTime3", "2018-01-01T10:00:00-10:00");
        testRequest(client, HttpMethod.POST, "/jsonBodyWithDate").expect(statusCode(200)).expect(jsonBodyResponse(obj)).sendJson(obj, 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) JsonObject(io.vertx.core.json.JsonObject) RequestParameters(io.vertx.ext.web.validation.RequestParameters) Test(org.junit.jupiter.api.Test)

Example 49 with RequestParameters

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

the class RouterBuilderIntegrationTest method testQueryExpandedObjectTestOnlyAdditionalProperties.

@Test
public void testQueryExpandedObjectTestOnlyAdditionalProperties(Vertx vertx, VertxTestContext testContext) {
    Checkpoint checkpoint = testContext.checkpoint(3);
    loadBuilderAndStartServer(vertx, VALIDATION_SPEC, testContext, routerBuilder -> {
        routerBuilder.setOptions(HANDLERS_TESTS_OPTIONS);
        routerBuilder.operation("objectTestOnlyAdditionalProperties").handler(routingContext -> {
            RequestParameters params = routingContext.get("parsedParameters");
            testContext.verify(() -> {
                assertThat(params.queryParameter("wellKnownParam").getString()).isEqualTo("hello");
                JsonObject param = params.queryParameter("params").getJsonObject();
                assertThat(param.containsKey("wellKnownParam")).isFalse();
                assertThat(param.getInteger("param1")).isEqualTo(1);
                assertThat(param.getInteger("param2")).isEqualTo(2);
            });
            routingContext.response().setStatusCode(200).end();
        });
    }).onComplete(h -> {
        testRequest(client, HttpMethod.GET, "/queryTests/objectTests/onlyAdditionalProperties?param1=1&param2=2&wellKnownParam=hello").expect(statusCode(200)).send(testContext, checkpoint);
        testRequest(client, HttpMethod.GET, "/queryTests/objectTests/onlyAdditionalProperties?param1=1&param2=a&wellKnownParam=hello").expect(statusCode(400)).expect(badParameterResponse(ParameterProcessorException.ParameterProcessorErrorType.PARSING_ERROR, "params", ParameterLocation.QUERY)).send(testContext, checkpoint);
        testRequest(client, HttpMethod.GET, "/queryTests/objectTests/onlyAdditionalProperties?param1=1&param2=2&wellKnownParam=a").expect(statusCode(400)).expect(badParameterResponse(ParameterProcessorException.ParameterProcessorErrorType.VALIDATION_ERROR, "wellKnownParam", 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 50 with RequestParameters

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

the class OpenAPI3ParametersUnitTest method testPathLabelNoexplodeString.

/**
 * Test: path_label_noexplode_string
 * Expected parameters sent:
 * color: .blue
 * Expected response: {"color":"blue"}
 */
@Test
public void testPathLabelNoexplodeString(Vertx vertx, VertxTestContext testContext) {
    loadBuilderAndStartServer(vertx, SPEC_URL, testContext, routerBuilder -> {
        routerBuilder.setOptions(new RouterBuilderOptions().setMountNotImplementedHandler(false));
        routerBuilder.operation("path_label_noexplode_string").handler(routingContext -> {
            RequestParameters params = routingContext.get("parsedParameters");
            JsonObject res = new JsonObject();
            testContext.verify(() -> {
                RequestParameter color_path = params.pathParameter("color");
                assertThat(color_path).isNotNull();
                assertThat(color_path.isString()).isTrue();
                assertThat(color_path.getString()).isEqualTo("blue");
                res.put("color", color_path.getString());
            });
            routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("content-type", "application/json; charset=utf-8").end(res.encode());
        });
    }).onComplete(h -> {
        String color_path;
        color_path = "blue";
        apiClient.pathLabelNoexplodeString(color_path, (AsyncResult<HttpResponse> ar) -> {
            if (ar.succeeded()) {
                testContext.verify(() -> {
                    assertThat(ar.result().bodyAsJsonObject()).isEqualTo(new JsonObject("{\"color\":\"blue\"}"));
                });
                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) 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