Search in sources :

Example 41 with TestRequest

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

the class RouterBuilderIntegrationTest method multipleWildcardMultipartEncoding.

@Test
public void multipleWildcardMultipartEncoding(Vertx vertx, VertxTestContext testContext) {
    Checkpoint checkpoint = testContext.checkpoint(6);
    loadBuilderAndStartServer(vertx, "src/test/resources/specs/multipart.yaml", testContext, routerBuilder -> {
        routerBuilder.setOptions(new RouterBuilderOptions().setRequireSecurityHandlers(false));
        routerBuilder.operation("testMultipartMultipleWildcard").handler(routingContext -> {
            routingContext.response().setStatusCode(200).end();
        });
    }).onComplete(h -> {
        testRequest(client, HttpMethod.POST, "/testMultipartMultipleWildcard").expect(statusCode(200)).sendMultipartForm(MultipartForm.create().binaryFileUpload("file1", "random.txt", "src/test/resources/random.txt", "text/plain").binaryFileUpload("file2", "random.txt", "src/test/resources/random.txt", "text/json"), testContext, checkpoint);
        testRequest(client, HttpMethod.POST, "/testMultipartMultipleWildcard").expect(statusCode(200)).sendMultipartForm(MultipartForm.create().binaryFileUpload("file1", "random.txt", "src/test/resources/random.txt", "application/json").binaryFileUpload("file2", "random.txt", "src/test/resources/random.txt", "text/json"), testContext, checkpoint);
        testRequest(client, HttpMethod.POST, "/testMultipartMultipleWildcard").expect(statusCode(400)).sendMultipartForm(MultipartForm.create().binaryFileUpload("file1", "random.txt", "src/test/resources/random.txt", "text/json").binaryFileUpload("file2", "random.txt", "src/test/resources/random.txt", "text/json"), testContext, checkpoint);
        testRequest(client, HttpMethod.POST, "/testMultipartMultipleWildcard").expect(statusCode(200)).sendMultipartForm(MultipartForm.create().binaryFileUpload("file1", "random.txt", "src/test/resources/random.txt", "text/plain").binaryFileUpload("file2", "random.txt", "src/test/resources/random.txt", "text/json"), testContext, checkpoint);
        testRequest(client, HttpMethod.POST, "/testMultipartMultipleWildcard").expect(statusCode(200)).sendMultipartForm(MultipartForm.create().binaryFileUpload("file1", "random.txt", "src/test/resources/random.txt", "text/plain").binaryFileUpload("file2", "random.txt", "src/test/resources/random.txt", "application/json"), testContext, checkpoint);
        testRequest(client, HttpMethod.POST, "/testMultipartMultipleWildcard").expect(statusCode(400)).sendMultipartForm(MultipartForm.create().binaryFileUpload("file1", "random.txt", "src/test/resources/random.txt", "text/plain").binaryFileUpload("file2", "random.txt", "src/test/resources/random.txt", "bla/json"), 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 42 with TestRequest

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

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

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

the class RouterBuilderSecurityWithFactoryTest method mountSingle.

@Test
public void mountSingle(Vertx vertx, VertxTestContext testContext) {
    Checkpoint checkpoint = testContext.checkpoint();
    loadBuilderAndStartServer(vertx, SECURITY_TESTS, testContext, routerBuilder -> {
        routerBuilder.setOptions(FACTORY_OPTIONS).securityHandler("api_key").bindBlocking(config -> mockSuccessfulAuthHandler(routingContext -> routingContext.put("api_key", "1"))).operation("listPetsSingleSecurity").handler(routingContext -> {
            routingContext.response().setStatusCode(200).setStatusMessage(concatenateRoutingContextEntries(routingContext, "api_key")).end();
        });
    }).onComplete(h -> testRequest(client, HttpMethod.GET, "/pets_single_security").expect(statusCode(200), statusMessage("1")).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 45 with TestRequest

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

the class RouterBuilderSecurityWithFactoryTest method mountOrAndMixed.

@Test
public void mountOrAndMixed(Vertx vertx, VertxTestContext testContext) {
    Checkpoint checkpoint = testContext.checkpoint();
    loadBuilderAndStartServer(vertx, SECURITY_TESTS, testContext, routerBuilder -> {
        routerBuilder.setOptions(FACTORY_OPTIONS);
        routerBuilder.operation("listPetsOrAndSecurity").handler(routingContext -> routingContext.response().setStatusCode(200).setStatusMessage(concatenateRoutingContextEntries(routingContext, "api_key", "second_api_key", "sibling_second_api_key", "third_api_key")).end());
        routerBuilder.securityHandler("api_key", mockFailingAuthHandler(routingContext -> routingContext.put("api_key", "1")));
        routerBuilder.securityHandler("second_api_key", mockSuccessfulAuthHandler(routingContext -> routingContext.put("second_api_key", "2")));
        routerBuilder.securityHandler("sibling_second_api_key", mockSuccessfulAuthHandler(routingContext -> routingContext.put("sibling_second_api_key", "3")));
        routerBuilder.securityHandler("third_api_key", mockFailingAuthHandler(routingContext -> routingContext.put("third_api_key", "4")));
    }).onComplete(h -> testRequest(client, HttpMethod.GET, "/pets_or_and_security").expect(statusCode(200), statusMessage("1-2-3-null")).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