Search in sources :

Example 1 with ObjectSchemaBuilder

use of io.vertx.json.schema.common.dsl.ObjectSchemaBuilder in project vertx-web by vert-x3.

the class FormBodyProcessorImplTest method testFormBodyProcessorParsingFailure.

@Test
public void testFormBodyProcessorParsingFailure(VertxTestContext testContext) {
    ObjectSchemaBuilder schemaBuilder = TestSchemas.SAMPLE_OBJECT_SCHEMA_BUILDER;
    MultiMap map = MultiMap.caseInsensitiveMultiMap();
    map.add("someNumbers", "1.1");
    map.add("someNumbers", "2.2");
    map.add("oneNumber", "3.3");
    map.add("someIntegers", "1");
    map.add("someIntegers", "hello");
    map.add("oneInteger", "3");
    map.add("aBoolean", "true");
    when(mockedServerRequest.getHeader(HttpHeaders.CONTENT_TYPE)).thenReturn("application/x-www-form-urlencoded");
    when(mockedServerRequest.formAttributes()).thenReturn(map);
    when(mockedContext.request()).thenReturn(mockedServerRequest);
    BodyProcessor processor = Bodies.formUrlEncoded(schemaBuilder).create(parser);
    assertThat(processor.canProcess("application/x-www-form-urlencoded")).isTrue();
    processor.process(mockedContext).onComplete(testContext.failing(err -> {
        testContext.verify(() -> {
            assertThat(err).isInstanceOf(BodyProcessorException.class).hasFieldOrPropertyWithValue("actualContentType", "application/x-www-form-urlencoded").hasCauseInstanceOf(MalformedValueException.class);
        });
        testContext.completeNow();
    }));
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) HttpServerRequest(io.vertx.core.http.HttpServerRequest) Mock(org.mockito.Mock) BodyProcessor(io.vertx.ext.web.validation.impl.body.BodyProcessor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MultiMap(io.vertx.core.MultiMap) RoutingContext(io.vertx.ext.web.RoutingContext) BodyProcessorException(io.vertx.ext.web.validation.BodyProcessorException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) JsonObject(io.vertx.core.json.JsonObject) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Vertx(io.vertx.core.Vertx) HttpHeaders(io.vertx.core.http.HttpHeaders) SchemaParser(io.vertx.json.schema.SchemaParser) Mockito.when(org.mockito.Mockito.when) Bodies(io.vertx.ext.web.validation.builder.Bodies) VertxExtension(io.vertx.junit5.VertxExtension) SchemaRouter(io.vertx.json.schema.SchemaRouter) TestSchemas(io.vertx.ext.web.validation.testutils.TestSchemas) Test(org.junit.jupiter.api.Test) JsonArray(io.vertx.core.json.JsonArray) ObjectSchemaBuilder(io.vertx.json.schema.common.dsl.ObjectSchemaBuilder) MalformedValueException(io.vertx.ext.web.validation.MalformedValueException) SchemaRouterOptions(io.vertx.json.schema.SchemaRouterOptions) Draft7SchemaParser(io.vertx.json.schema.draft7.Draft7SchemaParser) ObjectSchemaBuilder(io.vertx.json.schema.common.dsl.ObjectSchemaBuilder) MultiMap(io.vertx.core.MultiMap) BodyProcessorException(io.vertx.ext.web.validation.BodyProcessorException) MalformedValueException(io.vertx.ext.web.validation.MalformedValueException) BodyProcessor(io.vertx.ext.web.validation.impl.body.BodyProcessor) Test(org.junit.jupiter.api.Test)

Example 2 with ObjectSchemaBuilder

use of io.vertx.json.schema.common.dsl.ObjectSchemaBuilder in project vertx-web by vert-x3.

the class ValidationHandlerProcessorsIntegrationTest method testSameResultWithDifferentBodyTypes.

@Test
public void testSameResultWithDifferentBodyTypes(VertxTestContext testContext, @TempDir Path tempDir) throws Exception {
    Checkpoint checkpoint = testContext.checkpoint(3);
    JsonObject expectedResult = new JsonObject().put("int", 10).put("string", "hello").put("array", new JsonArray().add(1).add(1.1));
    ObjectSchemaBuilder bodySchema = objectSchema().requiredProperty("int", intSchema()).requiredProperty("string", stringSchema()).property("array", arraySchema().items(numberSchema()));
    ValidationHandler validationHandler = ValidationHandler.builder(parser).body(Bodies.json(bodySchema)).body(Bodies.multipartFormData(bodySchema)).body(Bodies.formUrlEncoded(bodySchema)).build();
    router.route().handler(BodyHandler.create(tempDir.toAbsolutePath().toString()));
    router.post("/testFormParam").handler(validationHandler).handler(routingContext -> {
        RequestParameters params = routingContext.get("parsedParameters");
        if (params.body().getJsonObject().equals(expectedResult)) {
            routingContext.response().setStatusCode(200).end();
        } else {
            routingContext.response().setStatusCode(500).end();
        }
    });
    testRequest(client, HttpMethod.POST, "/testFormParam").expect(statusCode(200)).sendURLEncodedForm(MultiMap.caseInsensitiveMultiMap().add("int", "10").add("string", "hello").add("array", "1").add("array", "1.1"), testContext, checkpoint);
    testRequest(client, HttpMethod.POST, "/testFormParam").expect(statusCode(200)).sendMultipartForm(MultipartForm.create().attribute("int", "10").attribute("string", "hello").attribute("array", "1").attribute("array", "1.1"), testContext, checkpoint);
    testRequest(client, HttpMethod.POST, "/testFormParam").expect(statusCode(200)).sendJson(expectedResult, testContext, checkpoint);
}
Also used : JsonArray(io.vertx.core.json.JsonArray) ObjectSchemaBuilder(io.vertx.json.schema.common.dsl.ObjectSchemaBuilder) Checkpoint(io.vertx.junit5.Checkpoint) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.jupiter.api.Test)

Example 3 with ObjectSchemaBuilder

use of io.vertx.json.schema.common.dsl.ObjectSchemaBuilder in project vertx-web by vert-x3.

the class ValidationHandlerProcessorsIntegrationTest method testBothFormTypes.

@Test
public void testBothFormTypes(VertxTestContext testContext, @TempDir Path tempDir) throws Exception {
    Checkpoint checkpoint = testContext.checkpoint(6);
    ObjectSchemaBuilder bodySchema = objectSchema().requiredProperty("parameter", intSchema());
    ValidationHandler validationHandler = ValidationHandler.builder(parser).body(Bodies.multipartFormData(bodySchema)).body(Bodies.formUrlEncoded(bodySchema)).build();
    router.route().handler(BodyHandler.create(tempDir.toAbsolutePath().toString()));
    router.post("/testFormParam").handler(validationHandler).handler(routingContext -> {
        RequestParameters params = routingContext.get("parsedParameters");
        if (params.body() != null) {
            routingContext.response().setStatusMessage(params.body().getJsonObject().getInteger("parameter").toString()).end();
        } else {
            routingContext.response().setStatusMessage("No body").end();
        }
    });
    testRequest(client, HttpMethod.POST, "/testFormParam").expect(statusCode(200), statusMessage("5")).sendURLEncodedForm(MultiMap.caseInsensitiveMultiMap().add("parameter", "5"), testContext, checkpoint);
    testRequest(client, HttpMethod.POST, "/testFormParam").expect(statusCode(400)).expect(badBodyResponse(BodyProcessorException.BodyProcessorErrorType.PARSING_ERROR)).sendURLEncodedForm(MultiMap.caseInsensitiveMultiMap().add("parameter", "bla"), testContext, checkpoint);
    testRequest(client, HttpMethod.POST, "/testFormParam").expect(statusCode(200), statusMessage("5")).sendMultipartForm(MultipartForm.create().attribute("parameter", "5"), testContext, checkpoint);
    testRequest(client, HttpMethod.POST, "/testFormParam").expect(statusCode(400)).expect(badBodyResponse(BodyProcessorException.BodyProcessorErrorType.PARSING_ERROR)).sendMultipartForm(MultipartForm.create().attribute("parameter", "bla"), testContext, checkpoint);
    testRequest(client, HttpMethod.POST, "/testFormParam").expect(statusCode(200), statusMessage("No body")).send(testContext, checkpoint);
    testRequest(client, HttpMethod.POST, "/testFormParam").expect(statusCode(400)).expect(badBodyResponse(BodyProcessorException.BodyProcessorErrorType.MISSING_MATCHING_BODY_PROCESSOR)).sendJson(new JsonObject(), testContext, checkpoint);
}
Also used : ObjectSchemaBuilder(io.vertx.json.schema.common.dsl.ObjectSchemaBuilder) Checkpoint(io.vertx.junit5.Checkpoint) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.jupiter.api.Test)

Example 4 with ObjectSchemaBuilder

use of io.vertx.json.schema.common.dsl.ObjectSchemaBuilder in project vertx-web by vert-x3.

the class WebValidationExamples method bodies.

public void bodies(SchemaParser schemaParser) {
    ObjectSchemaBuilder bodySchemaBuilder = objectSchema().property("username", stringSchema()).property("password", stringSchema());
    ValidationHandler.builder(schemaParser).body(Bodies.json(bodySchemaBuilder)).body(Bodies.formUrlEncoded(bodySchemaBuilder));
}
Also used : ObjectSchemaBuilder(io.vertx.json.schema.common.dsl.ObjectSchemaBuilder)

Example 5 with ObjectSchemaBuilder

use of io.vertx.json.schema.common.dsl.ObjectSchemaBuilder in project vertx-web by vert-x3.

the class FormBodyProcessorImplTest method testFormBodyProcessor.

@Test
public void testFormBodyProcessor(VertxTestContext testContext) {
    ObjectSchemaBuilder schemaBuilder = TestSchemas.SAMPLE_OBJECT_SCHEMA_BUILDER;
    MultiMap map = MultiMap.caseInsensitiveMultiMap();
    map.add("someNumbers", "1.1");
    map.add("someNumbers", "2.2");
    map.add("oneNumber", "3.3");
    map.add("someIntegers", "1");
    map.add("someIntegers", "2");
    map.add("oneInteger", "3");
    map.add("aBoolean", "true");
    when(mockedServerRequest.formAttributes()).thenReturn(map);
    when(mockedContext.request()).thenReturn(mockedServerRequest);
    BodyProcessor processor = Bodies.formUrlEncoded(schemaBuilder).create(parser);
    assertThat(processor.canProcess("application/x-www-form-urlencoded")).isTrue();
    processor.process(mockedContext).onComplete(testContext.succeeding(rp -> {
        testContext.verify(() -> {
            assertThat(rp.isJsonObject()).isTrue();
            assertThat(rp.getJsonObject()).isEqualTo(new JsonObject().put("someNumbers", new JsonArray().add(1.1d).add(2.2d)).put("oneNumber", 3.3d).put("someIntegers", new JsonArray().add(1L).add(2L)).put("oneInteger", 3L).put("aBoolean", true));
        });
        testContext.completeNow();
    }));
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) HttpServerRequest(io.vertx.core.http.HttpServerRequest) Mock(org.mockito.Mock) BodyProcessor(io.vertx.ext.web.validation.impl.body.BodyProcessor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MultiMap(io.vertx.core.MultiMap) RoutingContext(io.vertx.ext.web.RoutingContext) BodyProcessorException(io.vertx.ext.web.validation.BodyProcessorException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) JsonObject(io.vertx.core.json.JsonObject) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Vertx(io.vertx.core.Vertx) HttpHeaders(io.vertx.core.http.HttpHeaders) SchemaParser(io.vertx.json.schema.SchemaParser) Mockito.when(org.mockito.Mockito.when) Bodies(io.vertx.ext.web.validation.builder.Bodies) VertxExtension(io.vertx.junit5.VertxExtension) SchemaRouter(io.vertx.json.schema.SchemaRouter) TestSchemas(io.vertx.ext.web.validation.testutils.TestSchemas) Test(org.junit.jupiter.api.Test) JsonArray(io.vertx.core.json.JsonArray) ObjectSchemaBuilder(io.vertx.json.schema.common.dsl.ObjectSchemaBuilder) MalformedValueException(io.vertx.ext.web.validation.MalformedValueException) SchemaRouterOptions(io.vertx.json.schema.SchemaRouterOptions) Draft7SchemaParser(io.vertx.json.schema.draft7.Draft7SchemaParser) JsonArray(io.vertx.core.json.JsonArray) ObjectSchemaBuilder(io.vertx.json.schema.common.dsl.ObjectSchemaBuilder) MultiMap(io.vertx.core.MultiMap) JsonObject(io.vertx.core.json.JsonObject) BodyProcessor(io.vertx.ext.web.validation.impl.body.BodyProcessor) Test(org.junit.jupiter.api.Test)

Aggregations

ObjectSchemaBuilder (io.vertx.json.schema.common.dsl.ObjectSchemaBuilder)5 JsonObject (io.vertx.core.json.JsonObject)4 Test (org.junit.jupiter.api.Test)4 JsonArray (io.vertx.core.json.JsonArray)3 MultiMap (io.vertx.core.MultiMap)2 Vertx (io.vertx.core.Vertx)2 HttpHeaders (io.vertx.core.http.HttpHeaders)2 HttpServerRequest (io.vertx.core.http.HttpServerRequest)2 RoutingContext (io.vertx.ext.web.RoutingContext)2 BodyProcessorException (io.vertx.ext.web.validation.BodyProcessorException)2 MalformedValueException (io.vertx.ext.web.validation.MalformedValueException)2 Bodies (io.vertx.ext.web.validation.builder.Bodies)2 BodyProcessor (io.vertx.ext.web.validation.impl.body.BodyProcessor)2 TestSchemas (io.vertx.ext.web.validation.testutils.TestSchemas)2 SchemaParser (io.vertx.json.schema.SchemaParser)2 SchemaRouter (io.vertx.json.schema.SchemaRouter)2 SchemaRouterOptions (io.vertx.json.schema.SchemaRouterOptions)2 Draft7SchemaParser (io.vertx.json.schema.draft7.Draft7SchemaParser)2 Checkpoint (io.vertx.junit5.Checkpoint)2 VertxExtension (io.vertx.junit5.VertxExtension)2