Vertx http post客户端永远运行

问题描述 投票:1回答:1

我有以下Vertx Route设置:

          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

http-post vert.x
1个回答
1
投票

您没有在测试范围中部署定义路径的Verticle。这是一个工作片段:

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();
               });
   }
}
© www.soinside.com 2019 - 2024. All rights reserved.