Search in sources :

Example 51 with TestRequest

use of io.vertx.ext.web.validation.testutils.TestRequest 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 52 with TestRequest

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

the class RouterBuilderIntegrationTest method mountNotImplementedHandler.

@Test
public void mountNotImplementedHandler(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).setMountNotImplementedHandler(true));
        routerBuilder.operation("showPetById").handler(RoutingContext::next);
    }).onComplete(h -> testRequest(client, HttpMethod.GET, "/pets").expect(statusCode(501), statusMessage("Not Implemented")).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) Test(org.junit.jupiter.api.Test)

Example 53 with TestRequest

use of io.vertx.ext.web.validation.testutils.TestRequest 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 54 with TestRequest

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

the class RouterBuilderSecurityWithFactoryTest method mountOrWithLastSuccessful.

@Test
public void mountOrWithLastSuccessful(Vertx vertx, VertxTestContext testContext) {
    Checkpoint checkpoint = testContext.checkpoint();
    loadBuilderAndStartServer(vertx, SECURITY_TESTS, testContext, routerBuilder -> {
        routerBuilder.setOptions(FACTORY_OPTIONS);
        routerBuilder.operation("listPetsOrSecurity").handler(routingContext -> routingContext.response().setStatusCode(200).setStatusMessage(concatenateRoutingContextEntries(routingContext, "api_key", "second_api_key", "third_api_key")).end());
        routerBuilder.securityHandler("api_key", mockFailingAuthHandler(routingContext -> routingContext.put("api_key", "1")));
        routerBuilder.securityHandler("second_api_key", mockFailingAuthHandler(routingContext -> routingContext.put("second_api_key", "2")));
        routerBuilder.securityHandler("third_api_key", mockSuccessfulAuthHandler(routingContext -> routingContext.put("third_api_key", "3")));
    }).onComplete(h -> testRequest(client, HttpMethod.GET, "/pets_or_security").expect(statusCode(200), statusMessage("1-2-3")).send(testContext, checkpoint));
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) Arrays(java.util.Arrays) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) OAuth2Options(io.vertx.ext.auth.oauth2.OAuth2Options) Router(io.vertx.ext.web.Router) RoutingContext(io.vertx.ext.web.RoutingContext) Timeout(io.vertx.junit5.Timeout) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) JsonObject(io.vertx.core.json.JsonObject) Route(io.vertx.ext.web.Route) Vertx(io.vertx.core.Vertx) TestRequest(io.vertx.ext.web.validation.testutils.TestRequest) OAuth2FlowType(io.vertx.ext.auth.oauth2.OAuth2FlowType) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) Objects(java.util.Objects) io.vertx.ext.web.handler(io.vertx.ext.web.handler) AbstractMap(java.util.AbstractMap) User(io.vertx.ext.auth.User) HttpMethod(io.vertx.core.http.HttpMethod) Condition(org.assertj.core.api.Condition) Checkpoint(io.vertx.junit5.Checkpoint) OAuth2Auth(io.vertx.ext.auth.oauth2.OAuth2Auth) Handler(io.vertx.core.Handler) Assertions.assertThatCode(org.assertj.core.api.Assertions.assertThatCode) Checkpoint(io.vertx.junit5.Checkpoint) Test(org.junit.jupiter.api.Test)

Example 55 with TestRequest

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

the class RouterBuilderSecurityWithFactoryTest method mountOrWithAllFailing.

@Test
public void mountOrWithAllFailing(Vertx vertx, VertxTestContext testContext) {
    Checkpoint checkpoint = testContext.checkpoint();
    loadBuilderAndStartServer(vertx, SECURITY_TESTS, testContext, routerBuilder -> {
        routerBuilder.setOptions(FACTORY_OPTIONS);
        routerBuilder.operation("listPetsOrSecurity").handler(routingContext -> routingContext.response().setStatusCode(200).setStatusMessage(concatenateRoutingContextEntries(routingContext, "api_key", "second_api_key", "third_api_key")).end());
        routerBuilder.securityHandler("api_key", mockFailingAuthHandler(routingContext -> routingContext.put("api_key", "1")));
        routerBuilder.securityHandler("second_api_key", mockFailingAuthHandler(routingContext -> routingContext.put("second_api_key", "2")));
        routerBuilder.securityHandler("third_api_key", mockFailingAuthHandler(routingContext -> routingContext.put("third_api_key", "3")));
    }, new AbstractMap.SimpleImmutableEntry<>(401, routingContext -> routingContext.response().setStatusCode(401).setStatusMessage(concatenateRoutingContextEntries(routingContext, "api_key", "second_api_key", "third_api_key")).end())).onComplete(h -> testRequest(client, HttpMethod.GET, "/pets_or_security").expect(statusCode(401), statusMessage("1-2-3")).send(testContext, checkpoint));
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) Arrays(java.util.Arrays) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) OAuth2Options(io.vertx.ext.auth.oauth2.OAuth2Options) Router(io.vertx.ext.web.Router) RoutingContext(io.vertx.ext.web.RoutingContext) Timeout(io.vertx.junit5.Timeout) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) JsonObject(io.vertx.core.json.JsonObject) Route(io.vertx.ext.web.Route) Vertx(io.vertx.core.Vertx) TestRequest(io.vertx.ext.web.validation.testutils.TestRequest) OAuth2FlowType(io.vertx.ext.auth.oauth2.OAuth2FlowType) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) Objects(java.util.Objects) io.vertx.ext.web.handler(io.vertx.ext.web.handler) AbstractMap(java.util.AbstractMap) User(io.vertx.ext.auth.User) HttpMethod(io.vertx.core.http.HttpMethod) Condition(org.assertj.core.api.Condition) Checkpoint(io.vertx.junit5.Checkpoint) OAuth2Auth(io.vertx.ext.auth.oauth2.OAuth2Auth) Handler(io.vertx.core.Handler) Assertions.assertThatCode(org.assertj.core.api.Assertions.assertThatCode) Checkpoint(io.vertx.junit5.Checkpoint) 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