Search in sources :

Example 6 with HttpClient

use of io.vertx.reactivex.core.http.HttpClient in project knotx by Cognifide.

the class KnotxServerRoutingTest method testGetRequest.

private void testGetRequest(TestContext context, String url, String expectedResult) {
    HttpClient client = Vertx.newInstance(vertx.vertx()).createHttpClient();
    Async async = context.async();
    client.getNow(KNOTX_SERVER_PORT, KNOTX_SERVER_ADDRESS, url, resp -> resp.bodyHandler(body -> {
        context.assertEquals(HttpResponseStatus.OK.code(), resp.statusCode());
        context.assertTrue(resp.getHeader(EXPECTED_RESPONSE_HEADER) != null);
        context.assertEquals(EXPECTED_XSERVER_HEADER_VALUE, resp.getHeader(EXPECTED_RESPONSE_HEADER));
        try {
            context.assertEquals(body.toString(), expectedResult, "Wrong engines processed request, expected " + expectedResult);
        } catch (Exception e) {
            context.fail(e);
        }
        client.close();
        async.complete();
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) KnotContext(io.knotx.dataobjects.KnotContext) RunWith(org.junit.runner.RunWith) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Test(org.junit.Test) Logback(io.knotx.junit.rule.Logback) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Action1(rx.functions.Action1) ClientResponse(io.knotx.dataobjects.ClientResponse) RuleChain(org.junit.rules.RuleChain) HttpClientRequest(io.vertx.reactivex.core.http.HttpClientRequest) MultiMap(io.vertx.reactivex.core.MultiMap) Vertx(io.vertx.reactivex.core.Vertx) Rule(org.junit.Rule) Buffer(io.vertx.core.buffer.Buffer) HttpMethod(io.vertx.core.http.HttpMethod) Observable(io.reactivex.Observable) HttpClient(io.vertx.reactivex.core.http.HttpClient) KnotxConfiguration(io.knotx.junit.rule.KnotxConfiguration) RunTestOnContext(io.vertx.ext.unit.junit.RunTestOnContext) HttpClientResponse(io.vertx.reactivex.core.http.HttpClientResponse) TestVertxDeployer(io.knotx.junit.rule.TestVertxDeployer) Async(io.vertx.ext.unit.Async) HttpClient(io.vertx.reactivex.core.http.HttpClient)

Example 7 with HttpClient

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

the class Client method start.

@Override
public void start() throws Exception {
    HttpClient client = vertx.createHttpClient();
    HttpClientRequest req = client.request(HttpMethod.GET, 8080, "localhost", "/");
    req.toFlowable().flatMap(HttpClientResponse::toFlowable).map(buffer -> buffer.toJsonObject().mapTo(Data.class)).subscribe(data -> System.out.println("Got response " + data.message));
    // End request
    req.end();
}
Also used : HttpClientRequest(io.vertx.reactivex.core.http.HttpClientRequest) HttpMethod(io.vertx.core.http.HttpMethod) HttpClient(io.vertx.reactivex.core.http.HttpClient) AbstractVerticle(io.vertx.reactivex.core.AbstractVerticle) Runner(io.vertx.example.util.Runner) HttpClientResponse(io.vertx.reactivex.core.http.HttpClientResponse) HttpClientRequest(io.vertx.reactivex.core.http.HttpClientRequest) HttpClient(io.vertx.reactivex.core.http.HttpClient) HttpClientResponse(io.vertx.reactivex.core.http.HttpClientResponse)

Example 8 with HttpClient

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

the class Client method start.

@Override
public void start() throws Exception {
    HttpClient client = vertx.createHttpClient();
    // Create two requests
    HttpClientRequest req1 = client.request(HttpMethod.GET, 8080, "localhost", "/");
    HttpClientRequest req2 = client.request(HttpMethod.GET, 8080, "localhost", "/");
    // Turn the requests responses into Flowable<JsonObject>
    Flowable<JsonObject> obs1 = req1.toFlowable().flatMap(HttpClientResponse::toFlowable).map(buf -> new JsonObject(buf.toString("UTF-8")));
    Flowable<JsonObject> obs2 = req2.toFlowable().flatMap(HttpClientResponse::toFlowable).map(buf -> new JsonObject(buf.toString("UTF-8")));
    // Combine the responses with the zip into a single response
    obs1.zipWith(obs2, (b1, b2) -> new JsonObject().put("req1", b1).put("req2", b2)).subscribe(json -> {
        System.out.println("Got combined result " + json);
    }, Throwable::printStackTrace);
    req1.end();
    req2.end();
}
Also used : HttpClientRequest(io.vertx.reactivex.core.http.HttpClientRequest) Flowable(io.reactivex.Flowable) HttpMethod(io.vertx.core.http.HttpMethod) JsonObject(io.vertx.core.json.JsonObject) HttpClient(io.vertx.reactivex.core.http.HttpClient) AbstractVerticle(io.vertx.reactivex.core.AbstractVerticle) Runner(io.vertx.example.util.Runner) HttpClientResponse(io.vertx.reactivex.core.http.HttpClientResponse) HttpClientRequest(io.vertx.reactivex.core.http.HttpClientRequest) HttpClient(io.vertx.reactivex.core.http.HttpClient) JsonObject(io.vertx.core.json.JsonObject)

Example 9 with HttpClient

use of io.vertx.reactivex.core.http.HttpClient in project knotx by Cognifide.

the class KnotxServerRoutingTest method whenRequestingWithInvalidQuery_expectBadRequest.

@Test
@KnotxConfiguration("test-server.json")
public void whenRequestingWithInvalidQuery_expectBadRequest(TestContext context) {
    HttpClient client = Vertx.newInstance(vertx.vertx()).createHttpClient();
    Async async = context.async();
    client.getNow(KNOTX_SERVER_PORT, KNOTX_SERVER_ADDRESS, "/content/local/simple.html?q=~!@\\||$%^&*()_=-%22;;%27%22:%3C%3E/?]}{", resp -> {
        context.assertEquals(HttpResponseStatus.BAD_REQUEST.code(), resp.statusCode());
        client.close();
        async.complete();
    });
}
Also used : Async(io.vertx.ext.unit.Async) HttpClient(io.vertx.reactivex.core.http.HttpClient) Test(org.junit.Test) KnotxConfiguration(io.knotx.junit.rule.KnotxConfiguration)

Example 10 with HttpClient

use of io.vertx.reactivex.core.http.HttpClient in project knotx by Cognifide.

the class SampleApplicationHeadersTest method testGetRequest.

private void testGetRequest(TestContext context, String url) {
    HttpClient client = Vertx.newInstance(vertx.vertx()).createHttpClient();
    Async async = context.async();
    client.getNow(KNOTX_SERVER_PORT, KNOTX_SERVER_ADDRESS, url, resp -> {
        MultiMap headers = resp.headers();
        headers.names().forEach(name -> {
            context.assertEquals(resp.statusCode(), 200, "Wrong status code received.");
            context.assertTrue(expectedHeaders.contains(name), "Header " + name + " is not expected.");
            context.assertEquals(expectedHeaders.get(name), headers.get(name), "Wrong value of " + name + " header.");
        });
        async.complete();
    });
}
Also used : MultiMap(io.vertx.reactivex.core.MultiMap) Async(io.vertx.ext.unit.Async) HttpClient(io.vertx.reactivex.core.http.HttpClient)

Aggregations

HttpClient (io.vertx.reactivex.core.http.HttpClient)12 HttpMethod (io.vertx.core.http.HttpMethod)8 HttpClientRequest (io.vertx.reactivex.core.http.HttpClientRequest)8 HttpClientResponse (io.vertx.reactivex.core.http.HttpClientResponse)7 Async (io.vertx.ext.unit.Async)6 KnotxConfiguration (io.knotx.junit.rule.KnotxConfiguration)5 Test (org.junit.Test)5 Logback (io.knotx.junit.rule.Logback)4 TestVertxDeployer (io.knotx.junit.rule.TestVertxDeployer)4 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)4 Observable (io.reactivex.Observable)4 Runner (io.vertx.example.util.Runner)4 TestContext (io.vertx.ext.unit.TestContext)4 RunTestOnContext (io.vertx.ext.unit.junit.RunTestOnContext)4 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)4 AbstractVerticle (io.vertx.reactivex.core.AbstractVerticle)4 Vertx (io.vertx.reactivex.core.Vertx)4 Rule (org.junit.Rule)4 RuleChain (org.junit.rules.RuleChain)4 RunWith (org.junit.runner.RunWith)4