Search in sources :

Example 11 with HttpClient

use of io.vertx.core.http.HttpClient in project vertx-examples by vert-x3.

the class Proxy method start.

@Override
public void start() throws Exception {
    HttpClient client = vertx.createHttpClient(new HttpClientOptions());
    vertx.createHttpServer().requestHandler(req -> {
        System.out.println("Proxying request: " + req.uri());
        HttpClientRequest c_req = client.request(req.method(), 8282, "localhost", req.uri(), c_res -> {
            System.out.println("Proxying response: " + c_res.statusCode());
            req.response().setChunked(true);
            req.response().setStatusCode(c_res.statusCode());
            req.response().headers().setAll(c_res.headers());
            c_res.handler(data -> {
                System.out.println("Proxying response body: " + data.toString("ISO-8859-1"));
                req.response().write(data);
            });
            c_res.endHandler((v) -> req.response().end());
        });
        c_req.setChunked(true);
        c_req.headers().setAll(req.headers());
        req.handler(data -> {
            System.out.println("Proxying request body " + data.toString("ISO-8859-1"));
            c_req.write(data);
        });
        req.endHandler((v) -> c_req.end());
    }).listen(8080);
}
Also used : HttpClientRequest(io.vertx.core.http.HttpClientRequest) AbstractVerticle(io.vertx.core.AbstractVerticle) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Runner(io.vertx.example.util.Runner) HttpClient(io.vertx.core.http.HttpClient) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClient(io.vertx.core.http.HttpClient) HttpClientOptions(io.vertx.core.http.HttpClientOptions)

Example 12 with HttpClient

use of io.vertx.core.http.HttpClient in project vertx-examples by vert-x3.

the class ParameterizedTest method test.

@Test
public void test(TestContext context) {
    HttpServer server = vertx.createHttpServer().requestHandler(req -> {
        context.assertEquals(port, req.localAddress().port());
        req.response().end();
    });
    server.listen(port, "localhost", context.asyncAssertSuccess(s -> {
        HttpClient client = vertx.createHttpClient();
        Async async = context.async();
        HttpClientRequest req = client.get(port, "localhost", "/", resp -> {
            context.assertEquals(200, resp.statusCode());
            async.complete();
        });
        req.exceptionHandler(context::fail);
        req.end();
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) Arrays(java.util.Arrays) HttpServer(io.vertx.core.http.HttpServer) Vertx(io.vertx.core.Vertx) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) VertxUnitRunnerWithParametersFactory(io.vertx.ext.unit.junit.VertxUnitRunnerWithParametersFactory) HttpClientRequest(io.vertx.core.http.HttpClientRequest) After(org.junit.After) Parameterized(org.junit.runners.Parameterized) HttpClient(io.vertx.core.http.HttpClient) Before(org.junit.Before) HttpClientRequest(io.vertx.core.http.HttpClientRequest) Async(io.vertx.ext.unit.Async) HttpClient(io.vertx.core.http.HttpClient) HttpServer(io.vertx.core.http.HttpServer) Test(org.junit.Test)

Example 13 with HttpClient

use of io.vertx.core.http.HttpClient in project vertx-examples by vert-x3.

the class JUnitAndAssertJTest method testHttpCall.

@Test
public void testHttpCall(TestContext context) {
    // Send a request and get a response
    HttpClient client = vertx.createHttpClient();
    Async async = context.async();
    client.getNow(8080, "localhost", "/", resp -> {
        resp.exceptionHandler(context.exceptionHandler());
        resp.bodyHandler(body -> {
            assertThat(body.toString()).isEqualTo("foo");
            client.close();
            async.complete();
        });
    });
}
Also used : Async(io.vertx.ext.unit.Async) HttpClient(io.vertx.core.http.HttpClient) Test(org.junit.Test)

Example 14 with HttpClient

use of io.vertx.core.http.HttpClient in project vertx-examples by vert-x3.

the class JUnitAndHamcrestTest method testHTTPCall.

@Test
public void testHTTPCall(TestContext context) {
    // Send a request and get a response
    HttpClient client = vertx.createHttpClient();
    Async async = context.async();
    client.getNow(8080, "localhost", "/", resp -> {
        resp.exceptionHandler(context.exceptionHandler());
        resp.bodyHandler(body -> {
            assertThat(body.toString(), is("foo"));
            client.close();
            async.complete();
        });
    });
}
Also used : Async(io.vertx.ext.unit.Async) HttpClient(io.vertx.core.http.HttpClient) Test(org.junit.Test)

Example 15 with HttpClient

use of io.vertx.core.http.HttpClient in project incubator-servicecomb-java-chassis by apache.

the class HttpClientPoolFactory method createClientPool.

@Override
public HttpClientWithContext createClientPool() {
    Context context = Vertx.currentContext();
    HttpClient httpClient = context.owner().createHttpClient(httpClientOptions);
    return new HttpClientWithContext(httpClient, context);
}
Also used : Context(io.vertx.core.Context) HttpClient(io.vertx.core.http.HttpClient)

Aggregations

HttpClient (io.vertx.core.http.HttpClient)77 Test (org.junit.Test)47 HttpClientRequest (io.vertx.core.http.HttpClientRequest)36 HttpClientOptions (io.vertx.core.http.HttpClientOptions)25 Vertx (io.vertx.core.Vertx)22 HttpMethod (io.vertx.core.http.HttpMethod)22 JsonObject (io.vertx.core.json.JsonObject)22 Handler (io.vertx.core.Handler)18 Buffer (io.vertx.core.buffer.Buffer)18 HttpClientResponse (io.vertx.core.http.HttpClientResponse)16 TimeUnit (java.util.concurrent.TimeUnit)16 HttpServer (io.vertx.core.http.HttpServer)15 Async (io.vertx.ext.unit.Async)15 Before (org.junit.Before)15 File (java.io.File)14 CountDownLatch (java.util.concurrent.CountDownLatch)14 URL (java.net.URL)12 HttpServerOptions (io.vertx.core.http.HttpServerOptions)11 IOException (java.io.IOException)10 List (java.util.List)10