Search in sources :

Example 6 with WebClient

use of io.vertx.reactivex.ext.web.client.WebClient in project vertx-examples by vert-x3.

the class VertxWebClientVerticle method start.

@Override
public void start() throws Exception {
    WebClient client = WebClient.create(vertx);
    client.get(80, "perdu.com", "/").rxSend().map(HttpResponse::bodyAsString).subscribe(s -> LOGGER.info("From web client: " + s), Throwable::printStackTrace);
}
Also used : WebClient(io.vertx.reactivex.ext.web.client.WebClient)

Example 7 with WebClient

use of io.vertx.reactivex.ext.web.client.WebClient in project vertx-examples by vert-x3.

the class Client method start.

@Override
public void start() throws Exception {
    WebClient client = WebClient.create(vertx);
    Single<HttpResponse<String>> request = client.get(8080, "localhost", "/").as(BodyCodec.string()).rxSend();
    // Fire the request
    request.subscribe(resp -> System.out.println("Server content " + resp.body()));
    // Again
    request.subscribe(resp -> System.out.println("Server content " + resp.body()));
    // And again
    request.subscribe(resp -> System.out.println("Server content " + resp.body()));
}
Also used : HttpResponse(io.vertx.reactivex.ext.web.client.HttpResponse) WebClient(io.vertx.reactivex.ext.web.client.WebClient)

Example 8 with WebClient

use of io.vertx.reactivex.ext.web.client.WebClient in project vertx-examples by vert-x3.

the class Client method start.

@Override
public void start() throws Exception {
    // Create two requests
    WebClient client = WebClient.create(vertx);
    Single<JsonObject> request = client.get(8080, "localhost", "/").as(BodyCodec.jsonObject()).rxSend().map(resp -> resp.body());
    // Combine the responses with the zip into a single response
    request.zipWith(request, (b1, b2) -> new JsonObject().put("req1", b1).put("req2", b2)).subscribe(json -> {
        System.out.println("Got combined result " + json);
    }, err -> {
        err.printStackTrace();
    });
}
Also used : BodyCodec(io.vertx.reactivex.ext.web.codec.BodyCodec) JsonObject(io.vertx.core.json.JsonObject) AbstractVerticle(io.vertx.reactivex.core.AbstractVerticle) Runner(io.vertx.example.util.Runner) Single(io.reactivex.Single) WebClient(io.vertx.reactivex.ext.web.client.WebClient) JsonObject(io.vertx.core.json.JsonObject) WebClient(io.vertx.reactivex.ext.web.client.WebClient)

Example 9 with WebClient

use of io.vertx.reactivex.ext.web.client.WebClient in project knotx by Cognifide.

the class KnotxServerCsrfTest method whenRequestingGetLocalPath_expectLocalAC.

@Test
@KnotxConfiguration("test-server-csrf.json")
public void whenRequestingGetLocalPath_expectLocalAC(TestContext context) {
    Async async = context.async();
    createPassThroughKnot("test-splitter");
    createPassThroughKnot("test-assembler");
    createSimpleKnot("some-knot", "test", null);
    WebClient client = WebClient.create(Vertx.newInstance(vertx.vertx()));
    client.get(KNOTX_SERVER_PORT, KNOTX_SERVER_ADDRESS, "/content/local/simple.html").send(ar -> {
        if (ar.succeeded()) {
            context.assertEquals(HttpResponseStatus.OK.code(), ar.result().statusCode());
            context.assertTrue(ar.result().getHeader(EXPECTED_RESPONSE_HEADER) != null);
            context.assertEquals(EXPECTED_XSERVER_HEADER_VALUE, ar.result().getHeader(EXPECTED_RESPONSE_HEADER));
            context.assertTrue(ar.result().cookies().stream().anyMatch(cookie -> cookie.contains(CSRFHandler.DEFAULT_COOKIE_NAME)));
            client.close();
            async.complete();
        } else {
            context.fail(ar.cause());
            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) WebClient(io.vertx.reactivex.ext.web.client.WebClient) RuleChain(org.junit.rules.RuleChain) MultiMap(io.vertx.reactivex.core.MultiMap) Vertx(io.vertx.reactivex.core.Vertx) List(java.util.List) Rule(org.junit.Rule) Buffer(io.vertx.core.buffer.Buffer) KnotxConfiguration(io.knotx.junit.rule.KnotxConfiguration) RunTestOnContext(io.vertx.ext.unit.junit.RunTestOnContext) CSRFHandler(io.vertx.ext.web.handler.CSRFHandler) TestVertxDeployer(io.knotx.junit.rule.TestVertxDeployer) Async(io.vertx.ext.unit.Async) WebClient(io.vertx.reactivex.ext.web.client.WebClient) Test(org.junit.Test) KnotxConfiguration(io.knotx.junit.rule.KnotxConfiguration)

Example 10 with WebClient

use of io.vertx.reactivex.ext.web.client.WebClient in project knotx by Cognifide.

the class HttpClientFacadeTest method whenSupportedStaticPathServiceRequested_expectRequestExecutedAndResponseOKWithBody.

@Test
@KnotxConfiguration("knotx-action-adapter-http-test.json")
public void whenSupportedStaticPathServiceRequested_expectRequestExecutedAndResponseOKWithBody(TestContext context) throws Exception {
    Async async = context.async();
    // given
    final WebClient mockedWebClient = PowerMockito.spy(webClient());
    HttpClientFacade clientFacade = new HttpClientFacade(mockedWebClient, getConfiguration());
    final JsonObject expectedResponse = new JsonObject(FileReader.readText("first-response.json"));
    // when
    Single<ClientResponse> result = clientFacade.process(payloadMessage(new JsonObject().put("path", REQUEST_PATH), clientRequest), HttpMethod.POST);
    // then
    result.doOnSuccess(response -> {
        context.assertEquals(HttpResponseStatus.OK.code(), response.getStatusCode());
        context.assertEquals(expectedResponse, response.getBody().toJsonObject());
        Mockito.verify(mockedWebClient, Mockito.times(1)).request(HttpMethod.POST, PORT, DOMAIN, REQUEST_PATH);
    }).subscribe(response -> async.complete(), error -> context.fail(error.getMessage()));
}
Also used : ClientResponse(io.knotx.dataobjects.ClientResponse) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) Matchers(org.mockito.Matchers) UnsupportedServiceException(io.knotx.adapter.common.exception.UnsupportedServiceException) RunWith(org.junit.runner.RunWith) Logback(io.knotx.junit.rule.Logback) Single(io.reactivex.Single) ClientResponse(io.knotx.dataobjects.ClientResponse) Vertx(io.vertx.reactivex.core.Vertx) Lists(com.google.common.collect.Lists) AdapterRequest(io.knotx.dataobjects.AdapterRequest) FileReader(io.knotx.junit.util.FileReader) ClientRequest(io.knotx.dataobjects.ClientRequest) JsonObject(io.vertx.core.json.JsonObject) HttpClientFacade(io.knotx.adapter.common.http.HttpClientFacade) KnotxConfiguration(io.knotx.junit.rule.KnotxConfiguration) PowerMockito(org.powermock.api.mockito.PowerMockito) TestVertxDeployer(io.knotx.junit.rule.TestVertxDeployer) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) HttpAdapterConfiguration(io.knotx.adapter.common.http.HttpAdapterConfiguration) WebClient(io.vertx.reactivex.ext.web.client.WebClient) RuleChain(org.junit.rules.RuleChain) Mockito(org.mockito.Mockito) MultiMap(io.vertx.reactivex.core.MultiMap) List(java.util.List) Rule(org.junit.Rule) ServiceMetadata(io.knotx.adapter.common.http.ServiceMetadata) HttpMethod(io.vertx.core.http.HttpMethod) Pattern(java.util.regex.Pattern) RunTestOnContext(io.vertx.ext.unit.junit.RunTestOnContext) Collections(java.util.Collections) Async(io.vertx.ext.unit.Async) HttpClientFacade(io.knotx.adapter.common.http.HttpClientFacade) JsonObject(io.vertx.core.json.JsonObject) WebClient(io.vertx.reactivex.ext.web.client.WebClient) Test(org.junit.Test) KnotxConfiguration(io.knotx.junit.rule.KnotxConfiguration)

Aggregations

WebClient (io.vertx.reactivex.ext.web.client.WebClient)13 KnotxConfiguration (io.knotx.junit.rule.KnotxConfiguration)9 Async (io.vertx.ext.unit.Async)9 MultiMap (io.vertx.reactivex.core.MultiMap)9 Test (org.junit.Test)9 Logback (io.knotx.junit.rule.Logback)6 TestVertxDeployer (io.knotx.junit.rule.TestVertxDeployer)6 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)6 Single (io.reactivex.Single)6 JsonObject (io.vertx.core.json.JsonObject)6 TestContext (io.vertx.ext.unit.TestContext)6 RunTestOnContext (io.vertx.ext.unit.junit.RunTestOnContext)6 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)6 Vertx (io.vertx.reactivex.core.Vertx)6 List (java.util.List)6 Rule (org.junit.Rule)6 RuleChain (org.junit.rules.RuleChain)6 RunWith (org.junit.runner.RunWith)6 Lists (com.google.common.collect.Lists)5 UnsupportedServiceException (io.knotx.adapter.common.exception.UnsupportedServiceException)5