Search in sources :

Example 1 with RequestParameter

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

the class OpenAPI3ParametersUnitTest method testQueryFormNoexplodeObject.

/**
 * Test: query_form_noexplode_object
 * Expected parameters sent:
 * color: color=R,100,G,200,B,150
 * Expected response: {"color":{"R":"100","G":"200","B":"150"}}
 */
@Test
public void testQueryFormNoexplodeObject(Vertx vertx, VertxTestContext testContext) {
    loadBuilderAndStartServer(vertx, SPEC_URL, testContext, routerBuilder -> {
        routerBuilder.setOptions(new RouterBuilderOptions().setMountNotImplementedHandler(false));
        routerBuilder.operation("query_form_noexplode_object").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.isJsonObject()).isTrue();
                res.put("color", color_query.getJsonObject());
            });
            routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("content-type", "application/json; charset=utf-8").end(res.encode());
        });
    }).onComplete(h -> {
        Map<String, Object> color_query;
        color_query = new HashMap<>();
        color_query.put("R", "100");
        color_query.put("G", "200");
        color_query.put("B", "150");
        apiClient.queryFormNoexplodeObject(color_query, (AsyncResult<HttpResponse> ar) -> {
            if (ar.succeeded()) {
                testContext.verify(() -> {
                    assertThat(ar.result().bodyAsJsonObject()).isEqualTo(new JsonObject("{\"color\":{\"R\":\"100\",\"G\":\"200\",\"B\":\"150\"}}"));
                });
                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)

Example 2 with RequestParameter

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

the class OpenAPI3ParametersUnitTest method testQueryFormNoexplodeEmpty.

/**
 * Test: query_form_noexplode_empty
 * Expected parameters sent:
 * color: color=
 * Expected response: {"color":null}
 */
@Test
public void testQueryFormNoexplodeEmpty(Vertx vertx, VertxTestContext testContext) {
    loadBuilderAndStartServer(vertx, SPEC_URL, testContext, routerBuilder -> {
        routerBuilder.setOptions(new RouterBuilderOptions().setMountNotImplementedHandler(false));
        routerBuilder.operation("query_form_noexplode_empty").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.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_query;
        color_query = "";
        apiClient.queryFormNoexplodeEmpty(color_query, (AsyncResult<HttpResponse> ar) -> {
            if (ar.succeeded()) {
                testContext.verify(() -> {
                    assertThat(ar.result().bodyAsJsonObject()).isEqualTo(new JsonObject("{\"color\":null}"));
                });
                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 3 with RequestParameter

use of io.vertx.ext.web.validation.RequestParameter 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 4 with RequestParameter

use of io.vertx.ext.web.validation.RequestParameter 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());
            }
        });
    });
}
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 5 with RequestParameter

use of io.vertx.ext.web.validation.RequestParameter 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());
            }
        });
    });
}
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)49 JsonObject (io.vertx.core.json.JsonObject)49 RequestParameter (io.vertx.ext.web.validation.RequestParameter)49 RequestParameters (io.vertx.ext.web.validation.RequestParameters)49 VertxExtension (io.vertx.junit5.VertxExtension)49 VertxTestContext (io.vertx.junit5.VertxTestContext)49 ArrayList (java.util.ArrayList)49 List (java.util.List)49 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)49 Test (org.junit.jupiter.api.Test)49 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)49 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)1 QueryStringEncoder (io.netty.handler.codec.http.QueryStringEncoder)1 Future (io.vertx.core.Future)1 Handler (io.vertx.core.Handler)1