Java Code Examples for io.vertx.core.http.HttpClientOptions#setDefaultHost()

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

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

  HttpClientRequest request =
      client.get(
          "/wsService/basicTestSupplyWithError",
          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 3
Source File: RESTAsyncThreadCheckStaticInitializer.java    From vxms with Apache License 2.0 6 votes vote down vote up
@Test
public void simpleTimeoutTest() throws InterruptedException {

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

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

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

  await(10000, TimeUnit.MILLISECONDS);
}
 
Example 5
Source File: RESTServiceChainStringTest.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 6
Source File: RESTServiceChainByteTest.java    From vxms with Apache License 2.0 6 votes vote down vote up
@Test
// @Ignore
public void endpointOne() throws InterruptedException {
  HttpClientOptions options = new HttpClientOptions();
  options.setDefaultPort(PORT);
  options.setDefaultHost(HOST);
  HttpClient client = vertx.createHttpClient(options);

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

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

                resp.bodyHandler(
                    body -> {
                      System.out.println("Got a createResponse: " + body.toString());
                      testComplete();
                    });
              })
          .putHeader("content-length", String.valueOf("hello".getBytes().length))
          .putHeader("Content-Type", "application/json;charset=UTF-8");
  request.write("hello");
  request.end();
  await();
}
 
Example 8
Source File: RESTJerseyClientEventStringResponseAsyncTest.java    From vxms with Apache License 2.0 6 votes vote down vote up
@Test
public void onErrorResponseTest() 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/onFailurePass",
          resp -> {
            resp.bodyHandler(
                body -> {
                  System.out.println("Got a createResponse" + body.toString());

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

            testComplete();
          });
  request.end();
  await();
}
 
Example 9
Source File: RESTJerseyClientTests.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.exceptionHandler(error -> {});

                resp.bodyHandler(
                    body -> {
                      System.out.println("Got a createResponse: " + body.toString());
                      assertEquals(body.toString(), "test");
                      testComplete();
                    });
              })
          .putHeader("Content-Type", "application/json;charset=UTF-8");
  request.end();
  await();
}
 
Example 10
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 11
Source File: RESTJerseyClientEventStringResponseTest.java    From vxms with Apache License 2.0 6 votes vote down vote up
@Test
@Ignore
public void simpleSyncNoConnectionErrorTest() 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/simpleSyncNoConnectionError",
          resp -> {
            resp.bodyHandler(
                body -> {
                  System.out.println("Got a createResponse" + body.toString());

                  assertEquals(body.toString(), "no connection");
                });

            testComplete();
          });
  request.end();
  await();
}
 
Example 12
Source File: RESTJerseyMimeTypeClientTests.java    From vxms with Apache License 2.0 5 votes vote down vote up
@Test
public void stringPOSTResponse_1() throws InterruptedException {
  HttpClientOptions options = new HttpClientOptions();
  options.setDefaultPort(PORT);
  options.setDefaultHost(HOST);
  HttpClient client = vertx.createHttpClient(options);

  HttpClientRequest request =
      client
          .post(
              "/wsService/stringPOSTConsumesResponse/123",
              resp -> {
                resp.exceptionHandler(error -> {});

                resp.bodyHandler(
                    body -> {
                      System.out.println("Got a createResponse: " + body.toString());
                      assertEquals(body.toString(), "hello");
                      testComplete();
                    });
              })
          .putHeader("content-length", String.valueOf("hello".getBytes().length))
          .putHeader("Content-Type", "application/json;charset=UTF-8");
  request.write("hello");
  request.end();
  await();
}
 
Example 13
Source File: RESTJerseyClientCORSTest.java    From vxms with Apache License 2.0 5 votes vote down vote up
@Test
public void corsOK() 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/stringGETResponseSyncAsync",
              resp -> {
                resp.exceptionHandler(error -> {});

                resp.bodyHandler(
                    body -> {
                      System.out.println(
                          "Status: " + resp.statusCode() + " message:" + resp.statusMessage());
                      assertEquals("test-123", body.toString());
                      testComplete();
                    });
              })
          .putHeader("Origin", "http://example.com");
  request.end();
  await();
}
 
Example 14
Source File: HonoHttpDevice.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
private HonoHttpDevice() {
    final HttpClientOptions options = new HttpClientOptions();
    options.setDefaultHost(HONO_HTTP_ADAPTER_HOST);
    options.setDefaultPort(HONO_HTTP_ADAPTER_PORT);
    httpClient = VERTX.createHttpClient(options);
    standardRequestHeaders = MultiMap.caseInsensitiveMultiMap()
            .add(HttpHeaders.AUTHORIZATION, getBasicAuth(TENANT_ID, DEVICE_AUTH_ID, DEVICE_PASSWORD))
            .add(HttpHeaders.ORIGIN, ORIGIN_URI);
}
 
Example 15
Source File: RESTJerseyMimeTypeClientTests.java    From vxms with Apache License 2.0 5 votes vote down vote up
@Test
public void stringGETResponse_3() throws InterruptedException {
  HttpClientOptions options = new HttpClientOptions();
  options.setDefaultPort(PORT);
  options.setDefaultHost(HOST);
  HttpClient client = vertx.createHttpClient(options);

  HttpClientRequest request =
      client
          .get(
              "/wsService/stringGETConsumesResponse/123",
              resp -> {
                resp.exceptionHandler(
                    error -> {
                      System.out.println("Got a createResponse ERROR: " + error.toString());
                    });
                resp.bodyHandler(
                    body -> {
                      System.out.println("Got a createResponse: " + body.toString());
                      assertEquals(body.toString(), "123");
                      testComplete();
                    });
              })
          .putHeader("Content-Type", "application/xml;charset=UTF-8");
  request.end();
  await();
}
 
Example 16
Source File: ResolveServicesByLabelsConfigOKTest.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("192.168.1.1:8080/192.168.1.2:9080"));

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

  latch.await();
  assertTrue(!failed.get());
  testComplete();
}
 
Example 17
Source File: RESTServiceOnFailureStringResponseTest.java    From vxms with Apache License 2.0 5 votes vote down vote up
@Test
public void simpleOnFailureResponse400RetryStateless() throws InterruptedException {
  HttpClientOptions options = new HttpClientOptions();
  options.setDefaultPort(PORT);
  options.setDefaultHost(HOST);
  HttpClient client = vertx.createHttpClient(options);

  HttpClientRequest request =
      client.get(
          "/wsService/simpleOnFailureResponse400RetryStateless",
          new Handler<HttpClientResponse>() {
            public void handle(HttpClientResponse resp) {
              resp.bodyHandler(
                  body -> {
                    String val = body.getString(0, body.length());
                    System.out.println(
                        "--------catchedAsyncByteErrorDelay: "
                            + val
                            + "  status: "
                            + resp.statusMessage()
                            + "  status nr.:"
                            + resp.statusCode());
                    assertEquals("on failure", val);
                    assertEquals(400, resp.statusCode());
                    testComplete();
                  });
            }
          });
  request.end();

  await(10000, TimeUnit.MILLISECONDS);
}
 
Example 18
Source File: RESTJerseyClientEventStringCircuitBreakerTest.java    From vxms with Apache License 2.0 4 votes vote down vote up
@Test
public void simpleSyncNoConnectionErrorResponseStateful() 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/simpleSyncNoConnectionErrorResponseStateful",
          resp ->
              resp.bodyHandler(
                  body -> {
                    System.out.println("Got a createResponse: " + body.toString());

                    assertEquals(body.toString(), "No handlers for address hello1");

                    HttpClientRequest request2 =
                        client.get(
                            "/wsService/simpleSyncNoConnectionErrorResponseStateful",
                            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/simpleSyncNoConnectionErrorResponseStateful",
                                                    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/simpleSyncNoConnectionErrorResponseStateful",
                                                                            resp4 ->
                                                                                resp4.bodyHandler(
                                                                                    body4 -> {
                                                                                      System.out
                                                                                          .println(
                                                                                              "Got a createResponse: "
                                                                                                  + body4
                                                                                                      .toString());

                                                                                      assertEquals(
                                                                                          body4
                                                                                              .toString(),
                                                                                          "No handlers for address hello1");
                                                                                      testComplete();
                                                                                    }));
                                                                    request4.end();
                                                                  });
                                                            }));
                                            request3.end();
                                          });
                                    }));
                    request2.end();
                  }));

  request.end();

  await(80000, TimeUnit.MILLISECONDS);
}
 
Example 19
Source File: RESTJerseyClientStatefulCircuitBrakerAsyncTests.java    From vxms with Apache License 2.0 4 votes vote down vote up
@Test
public void stringGETResponseCircuitBaseTest() throws InterruptedException {

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

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

                    assertEquals(body.toString(), "failure");
                    HttpClientRequest request2 =
                        client.get(
                            "/wsService/stringGETResponseCircuitBaseTest/value",
                            resp2 ->
                                resp2.bodyHandler(
                                    body2 -> {
                                      System.out.println(
                                          "Got a createResponse: " + body2.toString());
                                      assertEquals(body2.toString(), "failure");
                                      // wait 1s, but circuit is still open
                                      vertx.setTimer(
                                          1205,
                                          handler -> {
                                            HttpClientRequest request3 =
                                                client.get(
                                                    "/wsService/stringGETResponseCircuitBaseTest/value",
                                                    resp3 ->
                                                        resp3.bodyHandler(
                                                            body3 -> {
                                                              System.out.println(
                                                                  "Got a createResponse: "
                                                                      + body3.toString());

                                                              assertEquals(
                                                                  body3.toString(), "failure");
                                                              // wait another 1s, now circuit
                                                              // should be closed
                                                              vertx.setTimer(
                                                                  2005,
                                                                  handler2 -> {
                                                                    HttpClientRequest request4 =
                                                                        client.get(
                                                                            "/wsService/stringGETResponseCircuitBaseTest/value",
                                                                            resp4 ->
                                                                                resp4.bodyHandler(
                                                                                    body4 -> {
                                                                                      System.out
                                                                                          .println(
                                                                                              "Got a createResponse: "
                                                                                                  + body4
                                                                                                      .toString());

                                                                                      assertEquals(
                                                                                          body4
                                                                                              .toString(),
                                                                                          "value");

                                                                                      // should be
                                                                                      // closed
                                                                                      testComplete();
                                                                                    }));
                                                                    request4.end();
                                                                  });
                                                            }));
                                            request3.end();
                                          });
                                    }));
                    request2.end();
                  }));
  request.end();

  await(80000, TimeUnit.MILLISECONDS);
}
 
Example 20
Source File: RESTServiceBlockingChainStringTest.java    From vxms with Apache License 2.0 4 votes vote down vote up
@Test
public void basicTestAndThenWithErrorAndCircuitBreaker() throws InterruptedException {

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

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

                    assertEquals(body.toString(), "failure");
                    HttpClientRequest request2 =
                        client.get(
                            "/wsService/basicTestAndThenWithErrorAndCircuitBreaker/value",
                            resp2 ->
                                resp2.bodyHandler(
                                    body2 -> {
                                      System.out.println(
                                          "Got a createResponse: " + body2.toString());
                                      assertEquals(body2.toString(), "failure");
                                      // wait 1s, but circuit is still open
                                      vertx.setTimer(
                                          1205,
                                          handler -> {
                                            HttpClientRequest request3 =
                                                client.get(
                                                    "/wsService/basicTestAndThenWithErrorAndCircuitBreaker/value",
                                                    resp3 ->
                                                        resp3.bodyHandler(
                                                            body3 -> {
                                                              System.out.println(
                                                                  "Got a createResponse: "
                                                                      + body3.toString());

                                                              assertEquals(
                                                                  body3.toString(), "failure");
                                                              // wait another 1s, now circuit
                                                              // should be closed
                                                              vertx.setTimer(
                                                                  2005,
                                                                  handler2 -> {
                                                                    HttpClientRequest request4 =
                                                                        client.get(
                                                                            "/wsService/basicTestAndThenWithErrorAndCircuitBreaker/value",
                                                                            resp4 ->
                                                                                resp4.bodyHandler(
                                                                                    body4 -> {
                                                                                      System.out
                                                                                          .println(
                                                                                              "Got a createResponse: "
                                                                                                  + body4
                                                                                                      .toString());

                                                                                      assertEquals(
                                                                                          body4
                                                                                              .toString(),
                                                                                          "value");

                                                                                      // should be
                                                                                      // closed
                                                                                      testComplete();
                                                                                    }));
                                                                    request4.end();
                                                                  });
                                                            }));
                                            request3.end();
                                          });
                                    }));
                    request2.end();
                  }));
  request.end();

  await(80000, TimeUnit.MILLISECONDS);
}