Java Code Examples for io.vertx.core.http.HttpClientRequest#end()

The following examples show how to use io.vertx.core.http.HttpClientRequest#end() . 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: RESTServiceExceptionTest.java    From vxms with Apache License 2.0 6 votes vote down vote up
@Test
public void exceptionInMethodBody() throws InterruptedException {
  HttpClientOptions options = new HttpClientOptions();
  options.setDefaultPort(PORT);
  options.setDefaultHost(HOST);
  HttpClient client = vertx.createHttpClient(options);

  HttpClientRequest request =
      client.get(
          "/wsService/exceptionInMethodBody?val=123&tmp=456",
          new Handler<HttpClientResponse>() {
            public void handle(HttpClientResponse resp) {
              resp.bodyHandler(
                  body -> {
                    String val = body.getString(0, body.length());
                    System.out.println("--------exceptionInMethodBody: " + val);
                    // assertEquals(key, "val");
                    testComplete();
                  });
            }
          });
  request.end();

  await();
}
 
Example 2
Source File: TranslatorExampleTest.java    From weld-vertx with Apache License 2.0 6 votes vote down vote up
@Test(timeout = DEFAULT_TIMEOUT)
public void testTranslator(TestContext context) throws InterruptedException {
    Async async = context.async();
    HttpClient client = vertx.createHttpClient();
    HttpClientRequest request = client.request(HttpMethod.POST, 8080, "localhost", "/translate");
    request.putHeader("Content-type", "application/x-www-form-urlencoded");
    request.handler((response) -> {
        if (response.statusCode() == 200) {
            response.bodyHandler((buffer) -> {
                context.assertEquals("[{\"word\":\"Hello\",\"translations\":[\"ahoj\",\"dobry den\"]},{\"word\":\"world\",\"translations\":[\"svet\"]}]",
                        buffer.toString());
                client.close();
                async.complete();
            });
        }
    });
    request.end("sentence=Hello world");
}
 
Example 3
Source File: RESTJerseyClientEventStringResponseAsyncTest.java    From vxms with Apache License 2.0 6 votes vote down vote up
@Test
public void complexResponseTest() throws InterruptedException {
  System.setProperty("sun.net.http.allowRestrictedHeaders", "true");

  HttpClientOptions options = new HttpClientOptions();
  options.setDefaultPort(PORT2);
  options.setDefaultHost(HOST);
  HttpClient client = vertx.createHttpClient(options);

  HttpClientRequest request =
      client.get(
          "/wsService/complexResponse",
          resp -> {
            resp.bodyHandler(
                body -> {
                  System.out.println("Got a createResponse" + body.toString());

                  assertEquals(body.toString(), "hello");
                });

            testComplete();
          });
  request.end();
  await();
}
 
Example 4
Source File: FormParameterExtractorTest.java    From vertx-swagger with Apache License 2.0 6 votes vote down vote up
@Test()
public void testOkFormDataArrayCsv(TestContext context) {
    Async async = context.async();
    HttpClientRequest req = httpClient.post(TEST_PORT, TEST_HOST, "/formdata/array/csv");
    req.handler(response -> {
        response.bodyHandler(body -> {
            context.assertEquals(response.statusCode(), 200);
            context.assertEquals("[\"1\",\"2\",\"3\"]", body.toString());
            async.complete();
        });
    });

    // Construct form
    StringBuffer payload = new StringBuffer().append("array_formdata=").append(esc.escape("1,2,3"));
    req.putHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
    req.end(payload.toString());
}
 
Example 5
Source File: StaticPostConstructTest.java    From vxms with Apache License 2.0 6 votes vote down vote up
@Test
public void stringGETResponse() throws InterruptedException {

  HttpClientOptions options = new HttpClientOptions();
  options.setDefaultPort(PORT);
  options.setDefaultHost(HOST);
  HttpClient client = vertx.createHttpClient(options);

  HttpClientRequest request =
      client.get(
          "/wsService/stringGETResponse",
          resp -> {
            resp.bodyHandler(
                body -> {
                  assertEquals(body.toString(), POST_VAL);
                  testComplete();
                });
          });
  request.end();
  await();
}
 
Example 6
Source File: RESTJerseyClientEventStringCircuitBreakerTest.java    From vxms with Apache License 2.0 6 votes vote down vote up
@Test
public void simpleSyncNoConnectionErrorResponseTest() throws InterruptedException {
  System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
  HttpClientOptions options = new HttpClientOptions();
  options.setDefaultPort(PORT2);
  options.setDefaultHost(HOST);
  HttpClient client = vertx.createHttpClient(options);

  HttpClientRequest request =
      client.get(
          "/wsService/simpleSyncNoConnectionErrorResponse",
          resp -> {
            resp.bodyHandler(
                body -> {
                  assertEquals(body.toString(), "No handlers for address hello1");
                  testComplete();
                });
          });
  request.end();
  await();
}
 
Example 7
Source File: RESTVerticleRouteBuilderSelfhostedTest.java    From vxms with Apache License 2.0 6 votes vote down vote up
@Test
public void endpointThree() throws InterruptedException {
  HttpClientOptions options = new HttpClientOptions();
  options.setDefaultPort(PORT);
  options.setDefaultHost(HOST);
  HttpClient client = vertx.createHttpClient(options);

  HttpClientRequest request =
      client.get(
          "/wsService/endpointThree?val=123&tmp=456",
          resp -> {
            resp.bodyHandler(
                body -> {
                  System.out.println("Got a createResponse: " + body.toString());
                  assertEquals(body.toString(), "123456");
                    testComplete();
                });
          });
  request.end();
  await();
}
 
Example 8
Source File: RESTServiceChainByteTest.java    From vxms with Apache License 2.0 6 votes vote down vote up
@Test
// @Ignore
public void basicTestAndThenWithErrorUnhandled() throws InterruptedException {
  HttpClientOptions options = new HttpClientOptions();
  options.setDefaultPort(PORT);
  options.setDefaultHost(HOST);
  HttpClient client = vertx.createHttpClient(options);

  HttpClientRequest request =
      client.get(
          "/wsService/basicTestAndThenWithErrorUnhandled",
          new Handler<HttpClientResponse>() {
            public void handle(HttpClientResponse resp) {
              assertEquals(resp.statusCode(), 500);
              assertEquals(resp.statusMessage(), "test error");
              testComplete();
            }
          });
  request.end();
  await();
}
 
Example 9
Source File: RESTVerticleRouteBuilderSelfhostedTestStaticInitializer.java    From vxms with Apache License 2.0 6 votes vote down vote up
@Test
public void endpointEight_header() throws InterruptedException {
  HttpClientOptions options = new HttpClientOptions();
  options.setDefaultPort(PORT);
  options.setDefaultHost(HOST);
  HttpClient client = vertx.createHttpClient(options);

  HttpClientRequest request =
      client.get(
          "/wsService/endpointEight_header?val=123&tmp=456",
          resp -> {
            resp.bodyHandler(
                body -> {
                  System.out.println(
                      "Got a createResponse endpointFourErrorReturnRetryTest: "
                          + body.toString());

                  assertEquals(body.toString(), "123456");
                });
            String contentType = resp.getHeader("Content-Type");
            assertEquals(contentType, "application/json");
            testComplete();
          });
  request.end();
  await();
}
 
Example 10
Source File: RESTServiceSelfhostedTestStaticInitializer.java    From vxms with Apache License 2.0 6 votes vote down vote up
@Test
public void endpointFourErrorRetryTest() throws InterruptedException {
  HttpClientOptions options = new HttpClientOptions();
  options.setDefaultPort(PORT);
  options.setDefaultHost(HOST);
  HttpClient client = vertx.createHttpClient(options);

  HttpClientRequest request =
      client.get(
          "/wsService/endpointFourErrorRetryTest?val=123&tmp=456",
          resp -> resp.bodyHandler(
              body -> {
                System.out.println("Got a createResponse: " + body.toString());
                assertEquals(body.toString(), "123456");
                testComplete();
              }));
  request.end();
  await();
}
 
Example 11
Source File: RESTServiceExceptionTest.java    From vxms with Apache License 2.0 6 votes vote down vote up
@Test
public void exceptionInByteResponse() throws InterruptedException {
  HttpClientOptions options = new HttpClientOptions();
  options.setDefaultPort(PORT);
  options.setDefaultHost(HOST);
  HttpClient client = vertx.createHttpClient(options);

  HttpClientRequest request =
      client.get(
          "/wsService/exceptionInByteResponse?val=123&tmp=456",
          new Handler<HttpClientResponse>() {
            public void handle(HttpClientResponse resp) {
              resp.bodyHandler(
                  body -> {
                    String val = body.getString(0, body.length());
                    System.out.println("--------exceptionInByteResponse: " + val);
                    // assertEquals(key, "val");
                    testComplete();
                  });
            }
          });
  request.end();

  await(5000, TimeUnit.MILLISECONDS);
}
 
Example 12
Source File: RESTServiceBlockingChainStringTest.java    From vxms with Apache License 2.0 6 votes vote down vote up
@Test
// @Ignore
public void basicTestAndThenWithError() throws InterruptedException {
  HttpClientOptions options = new HttpClientOptions();
  options.setDefaultPort(PORT);
  options.setDefaultHost(HOST);
  HttpClient client = vertx.createHttpClient(options);

  HttpClientRequest request =
      client.get(
          "/wsService/basicTestAndThenWithError",
          new Handler<HttpClientResponse>() {
            public void handle(HttpClientResponse resp) {
              resp.bodyHandler(
                  body -> {
                    System.out.println("Got a createResponse: " + body.toString());
                    assertEquals(body.toString(), "error test error");
                    testComplete();
                  });
            }
          });
  request.end();
  await();
}
 
Example 13
Source File: RESTServiceSelfhostedTestStaticInitializer.java    From vxms with Apache License 2.0 6 votes vote down vote up
@Test
public void endpointNine_exception() throws InterruptedException {
  HttpClientOptions options = new HttpClientOptions();
  options.setDefaultPort(PORT);
  options.setDefaultHost(HOST);
  HttpClient client = vertx.createHttpClient(options);

  HttpClientRequest request =
      client.get(
          "/wsService/endpointNine_exception?val=123&tmp=456",
          resp -> {
            assertEquals(500, resp.statusCode());
            assertEquals("test", resp.statusMessage());
            testComplete();
          });
  request.end();
  await();
}
 
Example 14
Source File: ResolveServicesByLAbelsWithPortNameOfflineTest.java    From vxms with Apache License 2.0 5 votes vote down vote up
@Test
public void testServiceByName() throws InterruptedException {
  CountDownLatch latch = new CountDownLatch(1);
  AtomicBoolean failed = new AtomicBoolean(false);
  HttpClientOptions options = new HttpClientOptions();
  options.setDefaultPort(PORT);
  options.setDefaultHost(HOST);
  HttpClient client = vertx.createHttpClient(options);
  HttpClientRequest request =
      client.get(
          "/wsService/myTestService",
          resp -> {
            resp.bodyHandler(
                body -> {
                  String response = body.toString();
                  System.out.println("Response entity '" + response + "' received.");
                  vertx.runOnContext(
                      context -> {
                        failed.set(
                            !response.equalsIgnoreCase(
                                "tcp://192.168.1.1:9090/http://192.168.1.2:9080"));

                        latch.countDown();
                      });
                });
          });
  request.end();

  latch.await();
  assertTrue(!failed.get());
  testComplete();
}
 
Example 15
Source File: ProxyTest.java    From okapi with Apache License 2.0 5 votes vote down vote up
private void myEdgeCallTest(RoutingContext ctx, String token) {
  HttpClientRequest get = HttpClientLegacy.get(httpClient, port, "localhost", "/testb/1", res1 -> {
    Buffer resBuf = Buffer.buffer();
    res1.handler(resBuf::appendBuffer);
    res1.endHandler(res2 -> {
      ctx.response().setStatusCode(res1.statusCode());
      ctx.response().end(resBuf);
    });
  });
  get.putHeader("X-Okapi-Token", token);
  get.end();
}
 
Example 16
Source File: VertxRequestTransmitter.java    From ethsigner with Apache License 2.0 5 votes vote down vote up
public void sendRequest(
    final HttpClientRequest request, final Buffer bodyContent, final RoutingContext context) {
  request.setTimeout(httpRequestTimeout.toMillis());
  request.exceptionHandler(thrown -> handleException(context, thrown));
  final MultiMap requestHeaders = createHeaders(context.request().headers());
  request.headers().setAll(requestHeaders);
  request.setChunked(false);
  request.end(bodyContent);
}
 
Example 17
Source File: WebSocketServiceTest.java    From besu with Apache License 2.0 5 votes vote down vote up
@Test
public void handleLoginRequestWithAuthDisabled() {
  final HttpClientRequest request =
      httpClient.post(
          websocketConfiguration.getPort(),
          websocketConfiguration.getHost(),
          "/login",
          response -> {
            assertThat(response.statusCode()).isEqualTo(400);
            assertThat(response.statusMessage()).isEqualTo("Authentication not enabled");
          });
  request.putHeader("Content-Type", "application/json; charset=utf-8");
  request.end("{\"username\":\"user\",\"password\":\"pass\"}");
}
 
Example 18
Source File: RESTJerseyClientTests.java    From vxms with Apache License 2.0 5 votes vote down vote up
@Test
public void stringOPTIONSResponse() throws InterruptedException, ExecutionException {

  HttpClientOptions options = new HttpClientOptions();
  options.setDefaultPort(PORT);
  options.setDefaultHost(HOST);
  HttpClient client = vertx.createHttpClient(options);

  HttpClientRequest request =
      client
          .options(
              "/wsService/stringOPTIONSResponse",
              resp -> {
                resp.exceptionHandler(error -> {});

                resp.bodyHandler(
                    body -> {
                      System.out.println("Got a createResponse: " + body.toString());
                      assertEquals(body.toString(), "hello");
                      testComplete();
                    });
              })
          .putHeader("Content-Type", "application/json;charset=UTF-8");

  request.end();
  await();
}
 
Example 19
Source File: WebSocketServiceLoginTest.java    From besu with Apache License 2.0 4 votes vote down vote up
@Test
public void loginWithGoodCredentials(final TestContext context) {
  final Async async = context.async();
  final HttpClientRequest request =
      httpClient.post(
          websocketConfiguration.getPort(),
          websocketConfiguration.getHost(),
          "/login",
          response -> {
            assertThat(response.statusCode()).isEqualTo(200);
            assertThat(response.statusMessage()).isEqualTo("OK");
            assertThat(response.getHeader("Content-Type")).isNotNull();
            assertThat(response.getHeader("Content-Type")).isEqualTo("application/json");
            response.bodyHandler(
                buffer -> {
                  final String body = buffer.toString();
                  assertThat(body).isNotBlank();

                  final JsonObject respBody = new JsonObject(body);
                  final String token = respBody.getString("token");
                  assertThat(token).isNotNull();

                  websocketService
                      .authenticationService
                      .get()
                      .getJwtAuthProvider()
                      .authenticate(
                          new JsonObject().put("jwt", token),
                          (r) -> {
                            Assertions.assertThat(r.succeeded()).isTrue();
                            final User user = r.result();
                            user.isAuthorized(
                                "noauths",
                                (authed) -> {
                                  assertThat(authed.succeeded()).isTrue();
                                  assertThat(authed.result()).isFalse();
                                });
                            user.isAuthorized(
                                "fakePermission",
                                (authed) -> {
                                  assertThat(authed.succeeded()).isTrue();
                                  assertThat(authed.result()).isTrue();
                                });
                            user.isAuthorized(
                                "eth:subscribe",
                                (authed) -> {
                                  assertThat(authed.succeeded()).isTrue();
                                  assertThat(authed.result()).isTrue();
                                  async.complete();
                                });
                          });
                });
          });
  request.putHeader("Content-Type", "application/json; charset=utf-8");
  request.end("{\"username\":\"user\",\"password\":\"pegasys\"}");

  async.awaitSuccess(VERTX_AWAIT_TIMEOUT_MILLIS);
}
 
Example 20
Source File: RESTAsyncThreadCheckStaticInitializer.java    From vxms with Apache License 2.0 4 votes vote down vote up
@Test
public void statefulTimeoutWithRetryTest() throws InterruptedException {

  HttpClientOptions options = new HttpClientOptions();
  options.setDefaultPort(PORT);
  options.setDefaultHost(HOST);
  HttpClient client = vertx.createHttpClient(options);

  HttpClientRequest request =
      client.get(
          "/wsService/statefulTimeoutWithRetryTest/crash",
          resp ->
              resp.bodyHandler(
                  body -> {
                    System.out.println("Got a createResponse: " + body.toString());
                    assertEquals(body.toString(), "operation _timeout");
                    HttpClientRequest request2 =
                        client.get(
                            "/wsService/statefulTimeoutWithRetryTest/value",
                            resp2 ->
                                resp2.bodyHandler(
                                    body2 -> {
                                      System.out.println(
                                          "Got a createResponse: " + body2.toString());
                                      assertEquals(body2.toString(), "circuit open");
                                      // wait 1s, but circuit is still open
                                      vertx.setTimer(
                                          1205,
                                          handler -> {
                                            HttpClientRequest request3 =
                                                client.get(
                                                    "/wsService/statefulTimeoutWithRetryTest/value",
                                                    resp3 ->
                                                        resp3.bodyHandler(
                                                            body3 -> {
                                                              System.out.println(
                                                                  "Got a createResponse: "
                                                                      + body3.toString());
                                                              assertEquals(
                                                                  body3.toString(),
                                                                  "circuit open");
                                                              // wait another 1s, now circuit
                                                              // should be closed
                                                              vertx.setTimer(
                                                                  2005,
                                                                  handler2 -> {
                                                                    HttpClientRequest request4 =
                                                                        client.get(
                                                                            "/wsService/statefulTimeoutWithRetryTest/value",
                                                                            resp4 ->
                                                                                resp4.bodyHandler(
                                                                                    body4 -> {
                                                                                      System.out
                                                                                          .println(
                                                                                              "Got a createResponse: "
                                                                                                  + body4
                                                                                                      .toString());
                                                                                      assertEquals(
                                                                                          body4
                                                                                              .toString(),
                                                                                          "value");
                                                                                      // wait
                                                                                      // another
                                                                                      // 1s, now
                                                                                      // circuit
                                                                                      // should be
                                                                                      // closed
                                                                                      testComplete();
                                                                                    }));
                                                                    request4.end();
                                                                  });
                                                            }));
                                            request3.end();
                                          });
                                    }));
                    request2.end();
                  }));
  request.end();

  await(80000, TimeUnit.MILLISECONDS);
}