use of io.vertx.core.http.HttpClient in project vertx-examples by vert-x3.
the class MyJUnitTest method test1.
@Test
public void test1(TestContext context) {
// Send a request and get a response
HttpClient client = vertx.createHttpClient();
Async async = context.async();
client.getNow(8080, "localhost", "/", resp -> {
resp.bodyHandler(body -> {
context.assertEquals("foo", body.toString());
client.close();
async.complete();
});
});
}
use of io.vertx.core.http.HttpClient in project vertx-examples by vert-x3.
the class VertxUnitTest method run.
// Not yet detected
@CodeTranslate
public void run() {
TestOptions options = new TestOptions().addReporter(new ReportOptions().setTo("console"));
TestSuite suite = TestSuite.create("io.vertx.example.unit.test.VertxUnitTest");
suite.before(context -> {
vertx = Vertx.vertx();
vertx.createHttpServer().requestHandler(req -> req.response().end("foo")).listen(8080, context.asyncAssertSuccess());
});
suite.after(context -> {
vertx.close(context.asyncAssertSuccess());
});
// Specifying the test names seems ugly...
suite.test("some_test1", context -> {
// Send a request and get a response
HttpClient client = vertx.createHttpClient();
Async async = context.async();
client.getNow(8080, "localhost", "/", resp -> {
resp.bodyHandler(body -> context.assertEquals("foo", body.toString("UTF-8")));
client.close();
async.complete();
});
});
suite.test("some_test2", context -> {
// Deploy and undeploy a verticle
vertx.deployVerticle("io.vertx.example.unit.SomeVerticle", context.asyncAssertSuccess(deploymentID -> {
vertx.undeploy(deploymentID, context.asyncAssertSuccess());
}));
});
suite.run(options);
}
use of io.vertx.core.http.HttpClient in project vertx-examples by vert-x3.
the class WgetCommand method start.
@Override
public void start() throws Exception {
// Create the wget CLI
CLI cli = CLI.create("wget").setSummary("Wget implemented with Vert.x HTTP client").addArgument(new Argument().setIndex(0).setArgName("http-url").setDescription("the HTTP uri to get"));
// Create the command
Command helloWorld = CommandBuilder.command(cli).processHandler(process -> {
URL url;
try {
url = new URL(process.commandLine().getArgumentValue(0));
} catch (MalformedURLException e) {
process.write("Bad url\n").end();
return;
}
HttpClient client = process.vertx().createHttpClient();
process.write("Connecting to " + url + "\n");
int port = url.getPort();
if (port == -1) {
port = 80;
}
HttpClientRequest req = client.get(port, url.getHost(), url.getPath());
req.exceptionHandler(err -> {
process.write("wget: error " + err.getMessage() + "\n");
process.end();
});
req.handler(resp -> {
process.write(resp.statusCode() + " " + resp.statusMessage() + "\n");
String contentType = resp.getHeader("Content-Type");
String contentLength = resp.getHeader("Content-Length");
process.write("Length: " + (contentLength != null ? contentLength : "unspecified"));
if (contentType != null) {
process.write("[" + contentType + "]");
}
process.write("\n");
process.end();
});
req.end();
}).build(vertx);
ShellService service = ShellService.create(vertx, new ShellServiceOptions().setTelnetOptions(new TelnetTermOptions().setHost("localhost").setPort(3000)));
CommandRegistry.getShared(vertx).registerCommand(helloWorld);
service.start(ar -> {
if (!ar.succeeded()) {
ar.cause().printStackTrace();
}
});
}
use of io.vertx.core.http.HttpClient in project gravitee-management-rest-api by gravitee-io.
the class HttpProvider method get.
@Override
public CompletableFuture<Collection<DynamicProperty>> get() {
CompletableFuture<Buffer> future = new VertxCompletableFuture<>(vertx);
URI requestUri = URI.create(dpConfiguration.getUrl());
boolean ssl = HTTPS_SCHEME.equalsIgnoreCase(requestUri.getScheme());
final HttpClientOptions options = new HttpClientOptions().setSsl(ssl).setTrustAll(true).setMaxPoolSize(1).setKeepAlive(false).setTcpKeepAlive(false).setConnectTimeout(2000);
final HttpClient httpClient = vertx.createHttpClient(options);
final int port = requestUri.getPort() != -1 ? requestUri.getPort() : (HTTPS_SCHEME.equals(requestUri.getScheme()) ? 443 : 80);
try {
HttpClientRequest request = httpClient.request(HttpMethod.GET, port, requestUri.getHost(), requestUri.toString());
request.handler(response -> {
if (response.statusCode() == HttpStatusCode.OK_200) {
response.bodyHandler(buffer -> {
future.complete(buffer);
// Close client
httpClient.close();
});
} else {
future.complete(null);
}
});
request.exceptionHandler(event -> {
try {
future.completeExceptionally(event);
// Close client
httpClient.close();
} catch (IllegalStateException ise) {
// Do not take care about exception when closing client
}
});
request.end();
} catch (Exception ex) {
logger.error("Unable to look for dynamic properties", ex);
future.completeExceptionally(ex);
}
return future.thenApply(buffer -> {
if (buffer == null) {
return null;
}
return mapper.map(buffer.toString());
});
}
use of io.vertx.core.http.HttpClient in project vertx-web by vert-x3.
the class StaticHandlerTest method testNoHttp2Push.
@Test
public void testNoHttp2Push() throws Exception {
stat.setWebRoot("webroot/somedir3");
router.route().handler(stat);
HttpServer http2Server = vertx.createHttpServer(new HttpServerOptions().setUseAlpn(true).setSsl(true).setPemKeyCertOptions(new PemKeyCertOptions().setKeyPath("tls/server-key.pem").setCertPath("tls/server-cert.pem")));
http2Server.requestHandler(router).listen(8443);
HttpClientOptions options = new HttpClientOptions().setSsl(true).setUseAlpn(true).setProtocolVersion(HttpVersion.HTTP_2).setPemTrustOptions(new PemTrustOptions().addCertPath("tls/server-cert.pem"));
HttpClient client = vertx.createHttpClient(options);
HttpClientRequest request = client.get(8443, "localhost", "/testLinkPreload.html", resp -> {
assertEquals(200, resp.statusCode());
assertEquals(HttpVersion.HTTP_2, resp.version());
resp.bodyHandler(this::assertNotNull);
testComplete();
});
request.pushHandler(pushedReq -> pushedReq.handler(pushedResp -> {
fail();
}));
request.end();
await();
}
Aggregations