use of io.vertx.ext.web.validation.impl.body.BodyProcessor in project vertx-web by vert-x3.
the class JsonBodyProcessorImplTest method testMalformedJson.
@Test
public void testMalformedJson() {
when(mockerServerRequest.getHeader(HttpHeaders.CONTENT_TYPE)).thenReturn("application/json");
when(mockedContext.request()).thenReturn(mockerServerRequest);
when(mockedContext.getBody()).thenReturn(Buffer.buffer("{\"a"));
BodyProcessor processor = Bodies.json(TestSchemas.SAMPLE_ARRAY_SCHEMA_BUILDER).create(parser);
assertThatCode(() -> processor.process(mockedContext)).isInstanceOf(BodyProcessorException.class).hasFieldOrPropertyWithValue("actualContentType", "application/json").hasCauseInstanceOf(DecodeException.class);
}
use of io.vertx.ext.web.validation.impl.body.BodyProcessor in project vertx-web by vert-x3.
the class JsonBodyProcessorImplTest method testNullBody.
@Test
public void testNullBody() {
when(mockerServerRequest.getHeader(HttpHeaders.CONTENT_TYPE)).thenReturn("application/json");
when(mockedContext.request()).thenReturn(mockerServerRequest);
when(mockedContext.getBody()).thenReturn(null);
BodyProcessor processor = Bodies.json(schema().withKeyword("type", "null")).create(parser);
assertThatCode(() -> processor.process(mockedContext)).isInstanceOf(BodyProcessorException.class).hasFieldOrPropertyWithValue("actualContentType", "application/json").hasCauseInstanceOf(MalformedValueException.class);
}
use of io.vertx.ext.web.validation.impl.body.BodyProcessor in project vertx-web by vert-x3.
the class OpenAPI3ValidationHandlerGenerator method create.
public ValidationHandlerImpl create(OperationImpl operation) {
// TODO error handling of this function?
Map<ParameterLocation, List<ParameterProcessor>> parameterProcessors = new HashMap<>();
List<BodyProcessor> bodyProcessors = new ArrayList<>();
GeneratorContext context = new GeneratorContext(this.schemaParser, holder, operation);
// Parse parameter processors
for (Map.Entry<JsonPointer, JsonObject> pe : operation.getParameters().entrySet()) {
ParameterLocation parsedLocation = ParameterLocation.valueOf(pe.getValue().getString("in").toUpperCase());
String parsedStyle = OpenAPI3Utils.resolveStyle(pe.getValue());
if (pe.getValue().getBoolean("allowReserved", false))
throw RouterBuilderException.createUnsupportedSpecFeature("You are using allowReserved keyword in parameter " + pe.getKey() + " which " + "is not supported");
JsonObject fakeSchema = context.fakeSchema(pe.getValue().getJsonObject("schema", new JsonObject()));
ParameterProcessorGenerator generator = parameterProcessorGenerators.stream().filter(g -> g.canGenerate(pe.getValue(), fakeSchema, parsedLocation, parsedStyle)).findFirst().orElseThrow(() -> RouterBuilderException.cannotFindParameterProcessorGenerator(pe.getKey(), pe.getValue()));
try {
ParameterProcessor generated = generator.generate(pe.getValue(), fakeSchema, pe.getKey(), parsedLocation, parsedStyle, context);
if (!parameterProcessors.containsKey(generated.getLocation()))
parameterProcessors.put(generated.getLocation(), new ArrayList<>());
parameterProcessors.get(generated.getLocation()).add(generated);
} catch (Exception e) {
throw RouterBuilderException.createErrorWhileGeneratingValidationHandler(String.format("Cannot generate parameter validator for parameter %s in %s", pe.getKey().toURI(), parsedLocation), e);
}
}
// Parse body required predicate
if (parseIsBodyRequired(operation))
context.addPredicate(RequestPredicate.BODY_REQUIRED);
// Parse body processors
for (Map.Entry<String, Object> mediaType : (JsonObject) REQUEST_BODY_CONTENT_POINTER.queryOrDefault(operation.getOperationModel(), iteratorWithLoader, new JsonObject())) {
JsonObject mediaTypeModel = (JsonObject) mediaType.getValue();
JsonPointer mediaTypePointer = operation.getPointer().copy().append("requestBody").append("content").append(mediaType.getKey());
BodyProcessor generated = bodyProcessorGenerators.stream().filter(g -> g.canGenerate(mediaType.getKey(), mediaTypeModel)).findFirst().orElse(NoopBodyProcessorGenerator.INSTANCE).generate(mediaType.getKey(), mediaTypeModel, mediaTypePointer, context);
bodyProcessors.add(generated);
}
return new ValidationHandlerImpl(parameterProcessors, bodyProcessors, context.getPredicates());
}
use of io.vertx.ext.web.validation.impl.body.BodyProcessor 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();
}));
}
use of io.vertx.ext.web.validation.impl.body.BodyProcessor in project vertx-web by vert-x3.
the class JsonBodyProcessorImplTest method testInvalidJsonObject.
@Test
public void testInvalidJsonObject(VertxTestContext testContext) {
when(mockerServerRequest.getHeader(HttpHeaders.CONTENT_TYPE)).thenReturn("application/json");
when(mockedContext.request()).thenReturn(mockerServerRequest);
when(mockedContext.getBody()).thenReturn(TestSchemas.INVALID_OBJECT.toBuffer());
BodyProcessor processor = Bodies.json(TestSchemas.SAMPLE_OBJECT_SCHEMA_BUILDER).create(parser);
processor.process(mockedContext).onComplete(testContext.failing(err -> {
testContext.verify(() -> {
assertThat(err).isInstanceOf(BodyProcessorException.class).hasFieldOrPropertyWithValue("actualContentType", "application/json").hasCauseInstanceOf(ValidationException.class);
});
testContext.completeNow();
}));
}
Aggregations