Vertx http post客户端永远运行

时间:2019-03-18 21:39:23

标签: http-post vert.x

我具有以下Vertx路由设置:

          router.post("/api/apple/")
            .handler(e -> {
                e.response()
                        .putHeader("content-type", "application/json")
                        .setStatusCode(200)
                        .end("hello");
            })
           .failureHandler(ctx -> {
                LOG.error("Error: "+ ctx.response().getStatusMessage());
                ctx.response().end();
            });

 vertx.createHttpServer().requestHandler(router::accept)
            .listen(config().getInteger("http.port", 8081), result -> {

                if (result.succeeded()) {
                    LOG.info("result succeeded in my start method");
                    future.complete();
                } else {
                    LOG.error("result failed");
                    future.fail(result.cause());
                }
            });

当我从Java测试客户端调用此代码时:

Async async = context.async();
    io.vertx.core.http.HttpClient client = vertx.createHttpClient();
    HttpClientRequest request = client.post(8081, "localhost", "/api/apple/", response -> {

        async.complete();
        LOG.info("Some callback {}",response.statusCode());
    });

    String body = "{'username':'www','password':'www'}";
    request.putHeader("content-length", "1000");
    request.putHeader("content-type", "application/x-www-form-urlencoded");
    request.write(body);

    request.end();

客户端继续运行,然后客户端超时。似乎无法在localhost:8081 / api / apple

上找到端点

1 个答案:

答案 0 :(得分:1)

未在测试范围内部署定义路线的顶点。这是一个有效的代码段:

public class HttpServerVerticleTest extends VertxTestRunner {

   private WebClient webClient;
   private HttpServerVerticle httpServer;
   private int port;

   @Before
   public void setUp(TestContext context) throws IOException {
      port = 8081;
      httpServer = new HttpServerVerticle(); // the verticle where your routes are registered
      // NOTICE HERE
      vertx.deployVerticle(httpServer, yourdeploymentOptions, context.asyncAssertSuccess());
      webClient = WebClient.wrap(vertx.createHttpClient());
   }

   @After
   public void tearDown(TestContext testContext) {
      webClient.close();
      vertx.close(testContext.asyncAssertSuccess());
   }

   @Test
   public void test_my_post_method(TestContext testContext) {
      Async http = testContext.async();

      String body = "{'username':'www','password':'www'}";

      webClient.post(port, "localhost", "/api/apple/")
             //.putHeader("Authorization", JWT_TOKEN)
               .putHeader("content-length", "1000");
               .putHeader("content-type", "application/x-www-form-urlencoded");
               .sendJson(Buffer.buffer(body.getBytes()), requestResponse -> {
                   if (requestResponse.succeeded()) {
                      testContext.assertTrue(requestResponse.result().statusCode() == HttpResponseStatus.OK.code());
                      testContext.assertTrue(requestResponse.result().body().getString().equals("hello"));
                   } else {
                        testContext.fail(requestResponse.cause());
                   }
                   http.complete();
               });
   }
}