Search in sources :

Example 26 with RequestParameter

use of io.vertx.ext.web.api.RequestParameter in project vertx-web by vert-x3.

the class OpenAPI3ParametersUnitTest method testQueryPipeDelimitedNoexplodeObject.

/**
 * Test: query_pipeDelimited_noexplode_object
 * Expected parameters sent:
 * color: R|100|G|200|B|150
 * Expected response: {"color":{"R":"100","G":"200","B":"150"}}
 * @throws Exception
 */
@Test
public void testQueryPipeDelimitedNoexplodeObject() throws Exception {
    routerFactory.addHandlerByOperationId("query_pipeDelimited_noexplode_object", routingContext -> {
        RequestParameters params = routingContext.get("parsedParameters");
        JsonObject res = new JsonObject();
        RequestParameter color_query = params.queryParameter("color");
        assertNotNull(color_query);
        assertTrue(color_query.isObject());
        Map<String, String> map = new HashMap<>();
        for (String key : color_query.getObjectKeys()) map.put(key, color_query.getObjectValue(key).getString());
        res.put("color", map);
        routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("content-type", "application/json; charset=utf-8").end(res.encode());
    });
    CountDownLatch latch = new CountDownLatch(1);
    Map<String, Object> color_query;
    color_query = new HashMap<>();
    color_query.put("R", "100");
    color_query.put("G", "200");
    color_query.put("B", "150");
    startServer();
    apiClient.queryPipeDelimitedNoexplodeObject(color_query, (AsyncResult<HttpResponse> ar) -> {
        if (ar.succeeded()) {
            assertEquals(200, ar.result().statusCode());
            assertTrue("Expected: " + new JsonObject("{\"color\":{\"R\":\"100\",\"G\":\"200\",\"B\":\"150\"}}").encode() + " Actual: " + ar.result().bodyAsJsonObject().encode(), new JsonObject("{\"color\":{\"R\":\"100\",\"G\":\"200\",\"B\":\"150\"}}").equals(ar.result().bodyAsJsonObject()));
        } else {
            assertTrue(ar.cause().getMessage(), false);
        }
        latch.countDown();
    });
    awaitLatch(latch);
}
Also used : HashMap(java.util.HashMap) RequestParameter(io.vertx.ext.web.api.RequestParameter) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncResult(io.vertx.core.AsyncResult) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Example 27 with RequestParameter

use of io.vertx.ext.web.api.RequestParameter in project vertx-web by vert-x3.

the class OpenAPI3ParametersUnitTest method testPathSimpleExplodeString.

/**
 * Test: path_simple_explode_string
 * Expected parameters sent:
 * color: blue
 * Expected response: {"color":"blue"}
 * @throws Exception
 */
@Test
public void testPathSimpleExplodeString() throws Exception {
    routerFactory.addHandlerByOperationId("path_simple_explode_string", routingContext -> {
        RequestParameters params = routingContext.get("parsedParameters");
        JsonObject res = new JsonObject();
        RequestParameter color_path = params.pathParameter("color");
        assertNotNull(color_path);
        assertTrue(color_path.isString());
        assertEquals(color_path.getString(), "blue");
        res.put("color", color_path.getString());
        routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("content-type", "application/json; charset=utf-8").end(res.encode());
    });
    CountDownLatch latch = new CountDownLatch(1);
    String color_path;
    color_path = "blue";
    startServer();
    apiClient.pathSimpleExplodeString(color_path, (AsyncResult<HttpResponse> ar) -> {
        if (ar.succeeded()) {
            assertEquals(200, ar.result().statusCode());
            assertTrue("Expected: " + new JsonObject("{\"color\":\"blue\"}").encode() + " Actual: " + ar.result().bodyAsJsonObject().encode(), new JsonObject("{\"color\":\"blue\"}").equals(ar.result().bodyAsJsonObject()));
        } else {
            assertTrue(ar.cause().getMessage(), false);
        }
        latch.countDown();
    });
    awaitLatch(latch);
}
Also used : RequestParameter(io.vertx.ext.web.api.RequestParameter) JsonObject(io.vertx.core.json.JsonObject) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncResult(io.vertx.core.AsyncResult) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Example 28 with RequestParameter

use of io.vertx.ext.web.api.RequestParameter in project vertx-web by vert-x3.

the class OpenAPI3ValidationTest method testQueryParameterArrayExploded.

@Test
public void testQueryParameterArrayExploded() throws Exception {
    Operation op = testSpec.getPaths().get("/queryTests/arrayTests/formExploded").getGet();
    OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(op, op.getParameters(), testSpec);
    loadHandlers("/queryTests/arrayTests/formExploded", HttpMethod.GET, false, validationHandler, (routingContext) -> {
        RequestParameters params = routingContext.get("parsedParameters");
        List<String> result = new ArrayList<>();
        for (RequestParameter r : params.queryParameter("parameter").getArray()) result.add(r.getInteger().toString());
        routingContext.response().setStatusMessage(serializeInCSVStringArray(result)).end();
    });
    List<String> values = new ArrayList<>();
    values.add("4");
    values.add("2");
    values.add("26");
    StringBuilder stringBuilder = new StringBuilder();
    for (String s : values) {
        stringBuilder.append("parameter=" + s + "&");
    }
    stringBuilder.deleteCharAt(stringBuilder.length() - 1);
    testRequest(HttpMethod.GET, "/queryTests/arrayTests/formExploded?" + stringBuilder, 200, serializeInCSVStringArray(values));
}
Also used : OpenAPI3RequestValidationHandlerImpl(io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RequestValidationHandlerImpl) RequestParameter(io.vertx.ext.web.api.RequestParameter) ArrayList(java.util.ArrayList) Operation(io.swagger.v3.oas.models.Operation) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Example 29 with RequestParameter

use of io.vertx.ext.web.api.RequestParameter in project vertx-web by vert-x3.

the class OpenAPI3ValidationTest method testDefaultFloatQueryParameter.

@Test
public void testDefaultFloatQueryParameter() throws Exception {
    Operation op = testSpec.getPaths().get("/queryTests/defaultFloat").getGet();
    OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(op, op.getParameters(), testSpec);
    loadHandlers("/queryTests/defaultFloat", HttpMethod.GET, false, validationHandler, (routingContext) -> {
        RequestParameters params = routingContext.get("parsedParameters");
        RequestParameter requestParameter = params.queryParameter("parameter");
        assertTrue(requestParameter.isFloat());
        routingContext.response().setStatusMessage(requestParameter.toString()).end();
    });
    testRequest(HttpMethod.GET, "/queryTests/defaultFloat", 200, "1.0");
}
Also used : OpenAPI3RequestValidationHandlerImpl(io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RequestValidationHandlerImpl) RequestParameter(io.vertx.ext.web.api.RequestParameter) Operation(io.swagger.v3.oas.models.Operation) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Example 30 with RequestParameter

use of io.vertx.ext.web.api.RequestParameter in project vertx-web by vert-x3.

the class OpenAPI3ValidationTest method testDefaultDoubleQueryParameter.

@Test
public void testDefaultDoubleQueryParameter() throws Exception {
    Operation op = testSpec.getPaths().get("/queryTests/defaultDouble").getGet();
    OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(op, op.getParameters(), testSpec);
    loadHandlers("/queryTests/defaultDouble", HttpMethod.GET, false, validationHandler, (routingContext) -> {
        RequestParameters params = routingContext.get("parsedParameters");
        RequestParameter requestParameter = params.queryParameter("parameter");
        assertTrue(requestParameter.isDouble());
        routingContext.response().setStatusMessage(requestParameter.toString()).end();
    });
    testRequest(HttpMethod.GET, "/queryTests/defaultDouble", 200, "1.0");
}
Also used : OpenAPI3RequestValidationHandlerImpl(io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RequestValidationHandlerImpl) RequestParameter(io.vertx.ext.web.api.RequestParameter) Operation(io.swagger.v3.oas.models.Operation) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Aggregations

RequestParameter (io.vertx.ext.web.api.RequestParameter)65 RequestParameters (io.vertx.ext.web.api.RequestParameters)57 Test (org.junit.Test)54 JsonObject (io.vertx.core.json.JsonObject)49 AsyncResult (io.vertx.core.AsyncResult)48 CountDownLatch (java.util.concurrent.CountDownLatch)48 HashMap (java.util.HashMap)34 ArrayList (java.util.ArrayList)21 HttpServerOptions (io.vertx.core.http.HttpServerOptions)18 RouterFactoryOptions (io.vertx.ext.web.api.contract.RouterFactoryOptions)18 Map (java.util.Map)18 OpenAPI (io.swagger.v3.oas.models.OpenAPI)17 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)17 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)17 Handler (io.vertx.core.Handler)17 JsonArray (io.vertx.core.json.JsonArray)17 RoutingContext (io.vertx.ext.web.RoutingContext)17 OpenAPI3RouterFactoryImpl (io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RouterFactoryImpl)17 WebTestValidationBase (io.vertx.ext.web.api.validation.WebTestValidationBase)17 HttpResponse (io.vertx.ext.web.client.HttpResponse)17