io.vertx.ext.web.codec.BodyCodec Java Examples

The following examples show how to use io.vertx.ext.web.codec.BodyCodec. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpKafkaConsumer.java    From client-examples with Apache License 2.0 6 votes vote down vote up
private Future<Void> deleteConsumer() {
    Future<Void> fut = Future.future();

    this.client.delete(this.consumer.getBaseUri())
        .putHeader(HttpHeaderNames.CONTENT_TYPE.toString(), "application/vnd.kafka.v2+json")
        .as(BodyCodec.jsonObject())
        .send(ar -> {
            if (ar.succeeded()) {
                HttpResponse<JsonObject> response = ar.result();
                if (response.statusCode() == HttpResponseStatus.NO_CONTENT.code()) {
                    log.info("Consumer {} deleted", this.consumer.getInstanceId());
                    fut.complete();
                } else {
                    fut.fail(new RuntimeException("Got HTTP status code " + response.statusCode()));
                } 
            } else {
                fut.fail(ar.cause());
            }
        });
    return fut;
}
 
Example #2
Source File: CustomWriterTest.java    From rest.vertx with Apache License 2.0 6 votes vote down vote up
@Test
void testRegisteredClassTypeReaderOutput(VertxTestContext context) {

    Dummy dummy = new Dummy("hello", "world");
    String json = JsonUtils.toJson(dummy);

    client.put(PORT, HOST, "/post/json")
            .as(BodyCodec.string())
            .putHeader("Content-Type", "application/json")
            .sendBuffer(Buffer.buffer(json), context.succeeding(response -> context.verify(() -> {

                assertEquals(200, response.statusCode());
                assertEquals(MediaType.APPLICATION_JSON, response.getHeader("Content-Type"));
                assertEquals("<custom>Received-hello=Received-world</custom>", response.body());
                context.completeNow();
            })));
}
 
Example #3
Source File: RouteWithBeanParamTest.java    From rest.vertx with Apache License 2.0 6 votes vote down vote up
@Test
void postBean(VertxTestContext context) {

    client.post(PORT, HOST, "/bean/read/result;one=1;enum=two?query=1").as(BodyCodec.string())
            .putHeader("MyHeader", "true")
            .putHeader("Cookie", "chocolate=tasty")
            .send(context.succeeding(response -> context.verify(() -> {

                assertEquals(200, response.statusCode());
                assertEquals("Header: true, " +
                        "Path: result;one=1;enum=two, " +
                        "Query: 1, " +
                        "Cookie: tasty, " +
                        "Matrix: two, " +
                        "one: 1, " +
                        "Body: empty", response.body());

                context.completeNow();
            })));
}
 
Example #4
Source File: RecommendationVerticle.java    From istio-tutorial with Apache License 2.0 6 votes vote down vote up
private void getNow(RoutingContext ctx) {
    count++;
    final WebClient client = WebClient.create(vertx);
    client.get(80, HTTP_NOW, "/api/json/cet/now")
    .timeout(5000)
    .as(BodyCodec.jsonObject())
        .send(ar -> {
            if (ar.succeeded()) {
                HttpResponse<JsonObject> response = ar.result();
                JsonObject body = response.body();
                String now = body.getString("currentDateTime");

                ctx.response().end(now + " " + String.format(RESPONSE_STRING_FORMAT, HOSTNAME, count));
            } else {
                ctx.response().setStatusCode(503).end(ar.cause().getMessage());
            }
        });
}
 
Example #5
Source File: DeleteWithBodyTest.java    From rest.vertx with Apache License 2.0 6 votes vote down vote up
@Test
void testDelete(VertxTestContext context) {

    // call and check response
    Dummy dummy = new Dummy("hello", "world");
    String json = JsonUtils.toJson(dummy);

    client.delete(PORT, HOST, "/post/json")
            .as(BodyCodec.string())
            .putHeader("Content-Type", "application/json")
            .sendBuffer(Buffer.buffer(json), context.succeeding(response -> context.verify(() -> {
                assertEquals(200, response.statusCode());
                assertEquals("application/json", response.getHeader("Content-Type"));
                assertEquals("<custom>Received-hello=Received-world</custom>", response.body());
                context.completeNow();
            })));
}
 
Example #6
Source File: AbstractHealthServiceTest.java    From cassandra-sidecar with Apache License 2.0 6 votes vote down vote up
@DisplayName("Should return HTTP 200 OK when check=True")
@Test
public void testHealthCheckReturns200OK(VertxTestContext testContext)
{
    check.setStatus(true);
    service.refreshNow();

    WebClient client = WebClient.create(vertx);

    client.get(config.getPort(), "localhost", "/api/v1/__health")
          .as(BodyCodec.string())
          .ssl(isSslEnabled())
          .send(testContext.succeeding(response -> testContext.verify(() ->
          {
              Assert.assertEquals(200, response.statusCode());
              testContext.completeNow();
          })));
}
 
Example #7
Source File: AbstractHealthServiceTest.java    From cassandra-sidecar with Apache License 2.0 6 votes vote down vote up
@DisplayName("Should return HTTP 503 Failure when check=False")
@Test
public void testHealthCheckReturns503Failure(VertxTestContext testContext)
{
    check.setStatus(false);
    service.refreshNow();

    WebClient client = WebClient.create(vertx);

    client.get(config.getPort(), "localhost", "/api/v1/__health")
          .as(BodyCodec.string())
          .ssl(isSslEnabled())
          .send(testContext.succeeding(response -> testContext.verify(() ->
          {
              Assert.assertEquals(503, response.statusCode());
              testContext.completeNow();
          })));
}
 
Example #8
Source File: RouteWithHeaderTest.java    From rest.vertx with Apache License 2.0 6 votes vote down vote up
@Test
void echoPostHeaderTest(VertxTestContext context) {

    Dummy dummy = new Dummy("one", "dude");

    String json = JsonUtils.toJson(dummy);

    client.post(PORT, HOST,"/header/dummy").as(BodyCodec.string())
            .putHeader("token", "doing")
            .putHeader("other", "things")
            .sendBuffer(Buffer.buffer(JsonUtils.toJson(json)), context.succeeding(response -> context.verify(() -> {
                assertEquals(200, response.statusCode());
                assertEquals("one=dude, doing things", response.body());
                context.completeNow();
            })));
}
 
Example #9
Source File: FormRestTest.java    From rest.vertx with Apache License 2.0 5 votes vote down vote up
@Test
void multipartFormTest(VertxTestContext context) {

    String content = "username=value&password=another";

    client.post(PORT, HOST, "/form/login")
            .as(BodyCodec.string())
            .putHeader("content-type", "multipart/form-data")
            .sendBuffer(Buffer.buffer(content), context.succeeding(response -> context.verify(() -> {
                assertEquals(200, response.statusCode());
                assertEquals("value:another", response.body());
                context.completeNow();
            })));
}
 
Example #10
Source File: RouteWithCustomAnnotationTest.java    From rest.vertx with Apache License 2.0 5 votes vote down vote up
@Test
void testGetEcho(VertxTestContext context) {

    client.get(PORT, HOST, "/system/user/echo2").as(BodyCodec.string())
        .send(context.succeeding(response -> context.verify(() -> {
            assertEquals(200, response.statusCode());
            assertEquals("Hello echo 2", response.body());
            context.completeNow();
        })));
}
 
Example #11
Source File: ValidationTest.java    From rest.vertx with Apache License 2.0 5 votes vote down vote up
@Test
void testDummyViolationSize(VertxTestContext context) {

    String content = "{\"name\": \"test\", \"value\": \"test\", \"size\": 30}";
    client.post(PORT, HOST, "/check/dummy").as(BodyCodec.string())
            .putHeader("content-type", "application/json")
            .sendBuffer(Buffer.buffer(content), context.succeeding(response -> context.verify(() -> {
                assertEquals("body ValidDummy.size: must be less than or equal to 20", response.body());
                assertEquals("Validation failed", response.getHeader("X-Status-Reason"));
                assertEquals(400, response.statusCode());
                context.completeNow();
            })));
}
 
Example #12
Source File: ValidationTest.java    From rest.vertx with Apache License 2.0 5 votes vote down vote up
@Test
void testThisOne(VertxTestContext context) {

    client.get(PORT, HOST, "/check/this").as(BodyCodec.string())
            .send(context.succeeding(response -> context.verify(() -> {
                assertEquals("@QueryParam(\"one\"): must not be null", response.body());
                assertEquals("Validation failed", response.getHeader("X-Status-Reason"));
                assertEquals(400, response.statusCode());
                context.completeNow();
            })));
}
 
Example #13
Source File: ValidationTest.java    From rest.vertx with Apache License 2.0 5 votes vote down vote up
@Test
void testTheOther(VertxTestContext context) {

    client.get(PORT, HOST, "/check/other?one=0&two=0&three=20").as(BodyCodec.string())
            .send(context.succeeding(response -> context.verify(() -> {
                String content = response.body();
                assertTrue(content.contains("@QueryParam(\"one\"): must be greater than or equal to 1"));
                assertTrue(content.contains("@QueryParam(\"two\"): must be greater than or equal to 1"));
                assertTrue(content.contains("@QueryParam(\"three\"): must be less than or equal to 10"));

                assertEquals("Validation failed", response.getHeader("X-Status-Reason"));
                assertEquals(400, response.statusCode());
                context.completeNow();
            })));
}
 
Example #14
Source File: RouteAuthorizationInjectionTest.java    From rest.vertx with Apache License 2.0 5 votes vote down vote up
@Test
void testGetOtherTwoAuthorized(VertxTestContext context) {


    client.get(PORT, HOST, "/private/other")
            .as(BodyCodec.string())
            .putHeader("X-Token", "two")
            .send(context.succeeding(response -> context.verify(() -> {
                assertEquals(200, response.statusCode());
                assertEquals("{\"role\":\"two\"}", response.body());
                context.completeNow();
            })));
}
 
Example #15
Source File: RouteExceptionHandlerTest.java    From rest.vertx with Apache License 2.0 5 votes vote down vote up
@Test
void throwOne(VertxTestContext context) {


    client.get(PORT, HOST, "/throw/exception/one").as(BodyCodec.string())
            .send(context.succeeding(response -> context.verify(() -> {
                assertEquals("BaseExceptionHandler: BASE: first", response.body());
                assertEquals(500, response.statusCode());
                context.completeNow();
            })));
}
 
Example #16
Source File: RouteAuthorizationInjectionTest.java    From rest.vertx with Apache License 2.0 5 votes vote down vote up
@Test
void testGetAdminUnAuthorized(VertxTestContext context) {

    client.get(PORT, HOST, "/private/admin")
            .as(BodyCodec.string())
            .putHeader("X-Token", "user")
            .send(context.succeeding(response -> context.verify(() -> {
                assertEquals(401, response.statusCode());
                assertEquals("HTTP 401 Unauthorized", response.body());
                context.completeNow();
            })));
}
 
Example #17
Source File: GlobalErrorHandlerTest.java    From rest.vertx with Apache License 2.0 5 votes vote down vote up
@Test
void throwBangExceptionTest(VertxTestContext context) {

    client.get(PORT, HOST, "/throw/bang")
            .as(BodyCodec.string())
            .send(context.succeeding(response -> context.verify(() -> {

                assertEquals("Bang!", response.body());
                assertEquals(400, response.statusCode());
                context.completeNow();
            })));
}
 
Example #18
Source File: GlobalErrorHandlerTest.java    From rest.vertx with Apache License 2.0 5 votes vote down vote up
@Test
void throwUnhandledExceptionTest(VertxTestContext context) {

    // call and check response
    RestRouter.getExceptionHandlers().register(IllegalArgumentExceptionHandler.class);

    client.get(PORT, HOST, "/throw/unhandled")
            .as(BodyCodec.string())
            .send(context.succeeding(response -> context.verify(() -> {

                assertEquals("Huh this produced an error: 'KABUM!'", response.body());
                assertEquals(400, response.statusCode());
                context.completeNow();
            })));
}
 
Example #19
Source File: RouteExceptionHandlerTest.java    From rest.vertx with Apache License 2.0 5 votes vote down vote up
@Test
void throwThree(VertxTestContext context) {

    client.get(PORT, HOST, "/throw/exception/three").as(BodyCodec.string())
            .send(context.succeeding(response -> context.verify(() -> {
                assertEquals("InheritedFromInheritedExceptionHandler: BASE: INHERITED: INHERITED FROM: third", response.body());
                assertEquals(500, response.statusCode());
                context.completeNow();
            })));
}
 
Example #20
Source File: CustomReaderTest.java    From rest.vertx with Apache License 2.0 5 votes vote down vote up
@Test
void readTimeFromQuery(VertxTestContext context) {
    RestRouter.getReaders().register(Instant.class, InstantReader.class);

    client.get(PORT, HOST, "/read/time")
        .setQueryParam("is", "2020-08-19")
        .as(BodyCodec.string())
        .send(context.succeeding(response -> context.verify(() -> {

            assertEquals(200, response.statusCode());
            assertEquals("2020-08-19 00:00:00 +0000", response.body());
            context.completeNow();
        })));
}
 
Example #21
Source File: RouteAsyncTest.java    From rest.vertx with Apache License 2.0 5 votes vote down vote up
@Test
void testAsyncCallPromiseInsideRest(VertxTestContext context) {

    client.get(PORT, HOST, "/async/call_promise")
            .as(BodyCodec.string())
            .send(context.succeeding(response -> context.verify(() -> {
                assertEquals(200, response.statusCode());
                assertEquals("{\"name\": \"async\", \"value\": \"called\"}", response.body());
                context.completeNow();
            })));
}
 
Example #22
Source File: CustomWriterTest.java    From rest.vertx with Apache License 2.0 5 votes vote down vote up
@Test
void testCustomOutput(VertxTestContext context) {

    client.get(PORT, HOST, "/test/custom")
            .as(BodyCodec.string())
            .send(context.succeeding(response -> context.verify(() -> {
                assertEquals(200, response.statusCode());
                assertEquals("<custom>CUSTOM</custom>", response.body());
                context.completeNow();
            })));
}
 
Example #23
Source File: RoutePathTest.java    From rest.vertx with Apache License 2.0 5 votes vote down vote up
@Test
void rootWithoutPathTest(VertxTestContext context) {

    client.get(PORT, HOST, "/this").as(BodyCodec.string())
            .send(context.succeeding(response -> context.verify(() -> {
                assertEquals(200, response.statusCode());
                assertEquals("this", response.body());
                context.completeNow();
            })));
}
 
Example #24
Source File: RouteWithCustomAnnotationTest.java    From rest.vertx with Apache License 2.0 5 votes vote down vote up
@Test
void testDeleteWithBody(VertxTestContext context) {

    Dummy json = new Dummy("test", "me");
    client.delete(PORT, HOST, "/system/user").as(BodyCodec.string())
        .sendBuffer(Buffer.buffer(JsonUtils.toJson(json)), context.succeeding(response -> context.verify(() -> {
            assertEquals(200, response.statusCode());
            assertEquals("[delete]", response.body());
            context.completeNow();
        })));
}
 
Example #25
Source File: NoMatchTest.java    From rest.vertx with Apache License 2.0 5 votes vote down vote up
@Test
void testEcho(VertxTestContext context) {

    client.get(PORT, HOST, "/rest/echo")
            .as(BodyCodec.string())
            .send(context.succeeding(response -> context.verify(() -> {
                assertEquals("\"echo\"", response.body());
                assertEquals(200, response.statusCode());

                String header = response.getHeader("Content-Type");
                assertEquals("application/json;charset=UTF-8", header);
                context.completeNow();
            })));
}
 
Example #26
Source File: RestRouterTest.java    From rest.vertx with Apache License 2.0 5 votes vote down vote up
@Test
void nullTest(VertxTestContext context) {

    client.get(PORT, HOST, "/test/context/null")
        .as(BodyCodec.string())
        .send(context.succeeding(response -> context.verify(() -> {

            assertEquals(200, response.statusCode());
            assertNull(response.body());
            context.completeNow();
        })));
}
 
Example #27
Source File: RestRouterTest.java    From rest.vertx with Apache License 2.0 5 votes vote down vote up
@Test
void contextTest(VertxTestContext context) {

    client.get(PORT, HOST, "/test/context/path")
        .as(BodyCodec.string())
        .send(context.succeeding(response -> context.verify(() -> {

            assertEquals("text/plain", response.getHeader("Content-Type"));
            assertEquals(200, response.statusCode());
            assertEquals("http://localhost:4444/test/context/path", response.body());
            context.completeNow();
        })));
}
 
Example #28
Source File: RouteWithTraceTest.java    From rest.vertx with Apache License 2.0 5 votes vote down vote up
@Test
void testTrace(VertxTestContext context) {

    client.request(HttpMethod.TRACE, PORT, HOST, "/rest/echo").as(BodyCodec.string())
            .send(context.succeeding(response -> context.verify(() -> {
                assertEquals("trace", response.body()); // returns sorted list of unique words
                assertEquals(200, response.statusCode());
                context.completeNow();
            })));
}
 
Example #29
Source File: RouteWithContextTest.java    From rest.vertx with Apache License 2.0 5 votes vote down vote up
@Test
void pushContextTest(VertxTestContext context) {

    client.get(PORT, HOST, "/context/custom").as(BodyCodec.string())
            .send(context.succeeding(response -> context.verify(() -> {
                assertEquals(200, response.statusCode());
                assertEquals("{\"name\":\"test\",\"value\":\"user\"}", response.body());
                context.completeNow();
            })));
}
 
Example #30
Source File: CustomReaderTest.java    From rest.vertx with Apache License 2.0 5 votes vote down vote up
@Test
void testCustomInput(VertxTestContext context) {

    client.post(PORT, HOST, "/read/custom")
        .as(BodyCodec.string())
        .sendBuffer(Buffer.buffer("The quick brown fox jumps over the red dog!"),
                    context.succeeding(response -> context.verify(() -> {
                        assertEquals(200, response.statusCode());
                        assertEquals("brown,dog,fox,jumps,over,quick,red,the", response.body()); // returns sorted list of unique words
                        context.completeNow();
                    })));
}