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();
}));
}
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);
}
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);
}
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));
}
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();
}));
}
Aggregations