Search in sources :

Example 31 with TestRequest

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

the class RouterBuilderSecurityTest 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) AuthenticationHandlerImpl(io.vertx.ext.web.handler.impl.AuthenticationHandlerImpl) AuthenticationHandler(io.vertx.ext.web.handler.AuthenticationHandler) 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) AsyncResult(io.vertx.core.AsyncResult) HttpException(io.vertx.ext.web.handler.HttpException) Credentials(io.vertx.ext.auth.authentication.Credentials) Route(io.vertx.ext.web.Route) SimpleAuthenticationHandler(io.vertx.ext.web.handler.SimpleAuthenticationHandler) Vertx(io.vertx.core.Vertx) TestRequest(io.vertx.ext.web.validation.testutils.TestRequest) OAuth2AuthHandler(io.vertx.ext.web.handler.OAuth2AuthHandler) AuthenticationProvider(io.vertx.ext.auth.authentication.AuthenticationProvider) 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) 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 32 with TestRequest

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

the class RouterBuilderSecurityTest method mountOrWithFirstSuccessful.

@Test
public void mountOrWithFirstSuccessful(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", mockSuccessfulAuthHandler(routingContext -> routingContext.put("api_key", "1")));
        routerBuilder.securityHandler("second_api_key", mockSuccessfulAuthHandler(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-null-null")).send(testContext, checkpoint));
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) Arrays(java.util.Arrays) AuthenticationHandlerImpl(io.vertx.ext.web.handler.impl.AuthenticationHandlerImpl) AuthenticationHandler(io.vertx.ext.web.handler.AuthenticationHandler) 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) AsyncResult(io.vertx.core.AsyncResult) HttpException(io.vertx.ext.web.handler.HttpException) Credentials(io.vertx.ext.auth.authentication.Credentials) Route(io.vertx.ext.web.Route) SimpleAuthenticationHandler(io.vertx.ext.web.handler.SimpleAuthenticationHandler) Vertx(io.vertx.core.Vertx) TestRequest(io.vertx.ext.web.validation.testutils.TestRequest) OAuth2AuthHandler(io.vertx.ext.web.handler.OAuth2AuthHandler) AuthenticationProvider(io.vertx.ext.auth.authentication.AuthenticationProvider) 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) 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 33 with TestRequest

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

the class RouteToEBServiceHandlerTest method authorizedUserTest.

@Test
public void authorizedUserTest(Vertx vertx, VertxTestContext testContext) {
    Checkpoint checkpoint = testContext.checkpoint();
    TestService service = new TestServiceImpl(vertx);
    final ServiceBinder serviceBinder = new ServiceBinder(vertx).setAddress("someAddress");
    consumer = serviceBinder.register(TestService.class, service);
    router.get("/test").handler(ValidationHandler.builder(parser).build()).handler(rc -> {
        // Put user mock into context
        rc.setUser(User.fromName("slinkydeveloper"));
        rc.next();
    }).handler(RouteToEBServiceHandler.build(vertx.eventBus(), "someAddress", "testUser"));
    testRequest(client, HttpMethod.GET, "/test").expect(statusCode(200), statusMessage("OK")).expect(jsonBodyResponse(new JsonObject().put("result", "Hello slinkydeveloper!"))).send(testContext, checkpoint);
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) ValidationHandler(io.vertx.ext.web.validation.ValidationHandler) BaseValidationHandlerTest(io.vertx.ext.web.validation.BaseValidationHandlerTest) Vertx(io.vertx.core.Vertx) ServiceBinder(io.vertx.serviceproxy.ServiceBinder) TestRequest(io.vertx.ext.web.validation.testutils.TestRequest) Schemas(io.vertx.json.schema.draft7.dsl.Schemas) HttpHeaders(io.vertx.core.http.HttpHeaders) BodyHandler(io.vertx.ext.web.handler.BodyHandler) VertxExtension(io.vertx.junit5.VertxExtension) Test(org.junit.jupiter.api.Test) JsonArray(io.vertx.core.json.JsonArray) AfterEach(org.junit.jupiter.api.AfterEach) Parameters.param(io.vertx.ext.web.validation.builder.Parameters.param) User(io.vertx.ext.auth.User) Buffer(io.vertx.core.buffer.Buffer) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) HttpMethod(io.vertx.core.http.HttpMethod) JsonObject(io.vertx.core.json.JsonObject) Checkpoint(io.vertx.junit5.Checkpoint) URI(java.net.URI) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) JsonPointer(io.vertx.core.json.pointer.JsonPointer) Bodies.json(io.vertx.ext.web.validation.builder.Bodies.json) Checkpoint(io.vertx.junit5.Checkpoint) ServiceBinder(io.vertx.serviceproxy.ServiceBinder) JsonObject(io.vertx.core.json.JsonObject) BaseValidationHandlerTest(io.vertx.ext.web.validation.BaseValidationHandlerTest) Test(org.junit.jupiter.api.Test)

Example 34 with TestRequest

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

the class RouteToEBServiceHandlerTest method authorizationPropagationTest.

@Test
public void authorizationPropagationTest(Vertx vertx, VertxTestContext testContext) {
    Checkpoint checkpoint = testContext.checkpoint();
    TestService service = new TestServiceImpl(vertx);
    final ServiceBinder serviceBinder = new ServiceBinder(vertx).setAddress("someAddress");
    consumer = serviceBinder.register(TestService.class, service);
    router.get("/test").handler(ValidationHandler.builder(parser).build()).handler(rc -> {
        // patch the request to include authorization header
        rc.request().headers().add(HttpHeaders.AUTHORIZATION, "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==");
        rc.next();
    }).handler(RouteToEBServiceHandler.build(vertx.eventBus(), "someAddress", "testAuthorization"));
    testRequest(client, HttpMethod.GET, "/test").expect(statusCode(200), statusMessage("OK")).expect(jsonBodyResponse(new JsonObject().put("result", "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="))).send(testContext, checkpoint);
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) ValidationHandler(io.vertx.ext.web.validation.ValidationHandler) BaseValidationHandlerTest(io.vertx.ext.web.validation.BaseValidationHandlerTest) Vertx(io.vertx.core.Vertx) ServiceBinder(io.vertx.serviceproxy.ServiceBinder) TestRequest(io.vertx.ext.web.validation.testutils.TestRequest) Schemas(io.vertx.json.schema.draft7.dsl.Schemas) HttpHeaders(io.vertx.core.http.HttpHeaders) BodyHandler(io.vertx.ext.web.handler.BodyHandler) VertxExtension(io.vertx.junit5.VertxExtension) Test(org.junit.jupiter.api.Test) JsonArray(io.vertx.core.json.JsonArray) AfterEach(org.junit.jupiter.api.AfterEach) Parameters.param(io.vertx.ext.web.validation.builder.Parameters.param) User(io.vertx.ext.auth.User) Buffer(io.vertx.core.buffer.Buffer) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) HttpMethod(io.vertx.core.http.HttpMethod) JsonObject(io.vertx.core.json.JsonObject) Checkpoint(io.vertx.junit5.Checkpoint) URI(java.net.URI) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) JsonPointer(io.vertx.core.json.pointer.JsonPointer) Bodies.json(io.vertx.ext.web.validation.builder.Bodies.json) Checkpoint(io.vertx.junit5.Checkpoint) ServiceBinder(io.vertx.serviceproxy.ServiceBinder) JsonObject(io.vertx.core.json.JsonObject) BaseValidationHandlerTest(io.vertx.ext.web.validation.BaseValidationHandlerTest) Test(org.junit.jupiter.api.Test)

Example 35 with TestRequest

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

the class RouteToEBServiceHandlerTest method extraPayloadTest.

@Test
public void extraPayloadTest(Vertx vertx, VertxTestContext testContext) {
    Checkpoint checkpoint = testContext.checkpoint();
    TestService service = new TestServiceImpl(vertx);
    final ServiceBinder serviceBinder = new ServiceBinder(vertx).setAddress("someAddress");
    consumer = serviceBinder.register(TestService.class, service);
    router.get("/test").handler(ValidationHandler.builder(parser).build()).handler(RouteToEBServiceHandler.build(vertx.eventBus(), "someAddress", "extraPayload").extraPayloadMapper(rc -> new JsonObject().put("username", "slinkydeveloper")));
    testRequest(client, HttpMethod.GET, "/test").expect(statusCode(200), statusMessage("OK")).expect(jsonBodyResponse(new JsonObject().put("result", "Hello slinkydeveloper!"))).send(testContext, checkpoint);
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) ValidationHandler(io.vertx.ext.web.validation.ValidationHandler) BaseValidationHandlerTest(io.vertx.ext.web.validation.BaseValidationHandlerTest) Vertx(io.vertx.core.Vertx) ServiceBinder(io.vertx.serviceproxy.ServiceBinder) TestRequest(io.vertx.ext.web.validation.testutils.TestRequest) Schemas(io.vertx.json.schema.draft7.dsl.Schemas) HttpHeaders(io.vertx.core.http.HttpHeaders) BodyHandler(io.vertx.ext.web.handler.BodyHandler) VertxExtension(io.vertx.junit5.VertxExtension) Test(org.junit.jupiter.api.Test) JsonArray(io.vertx.core.json.JsonArray) AfterEach(org.junit.jupiter.api.AfterEach) Parameters.param(io.vertx.ext.web.validation.builder.Parameters.param) User(io.vertx.ext.auth.User) Buffer(io.vertx.core.buffer.Buffer) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) HttpMethod(io.vertx.core.http.HttpMethod) JsonObject(io.vertx.core.json.JsonObject) Checkpoint(io.vertx.junit5.Checkpoint) URI(java.net.URI) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) JsonPointer(io.vertx.core.json.pointer.JsonPointer) Bodies.json(io.vertx.ext.web.validation.builder.Bodies.json) Checkpoint(io.vertx.junit5.Checkpoint) ServiceBinder(io.vertx.serviceproxy.ServiceBinder) JsonObject(io.vertx.core.json.JsonObject) BaseValidationHandlerTest(io.vertx.ext.web.validation.BaseValidationHandlerTest) 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