Search in sources :

Example 1 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<Data>> request = client.get(8080, "localhost", "/").as(BodyCodec.json(Data.class)).rxSend();
    // Fire the request
    request.subscribe(resp -> System.out.println("Server content " + resp.body().message));
    // Again
    request.subscribe(resp -> System.out.println("Server content " + resp.body().message));
    // And again
    request.subscribe(resp -> System.out.println("Server content " + resp.body().message));
}
Also used : HttpResponse(io.vertx.reactivex.ext.web.client.HttpResponse) WebClient(io.vertx.reactivex.ext.web.client.WebClient)

Example 2 with WebClient

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

the class HttpClientFacadeTest method whenServiceRequestedWithoutPathParam_expectNoServiceRequestAndBadRequest.

@Test
@KnotxConfiguration("knotx-action-adapter-http-test.json")
public void whenServiceRequestedWithoutPathParam_expectNoServiceRequestAndBadRequest(TestContext context) throws Exception {
    Async async = context.async();
    // given
    final WebClient mockedWebClient = PowerMockito.spy(webClient());
    HttpClientFacade clientFacade = new HttpClientFacade(mockedWebClient, getConfiguration());
    // when
    Single<ClientResponse> result = clientFacade.process(new AdapterRequest().setParams(new JsonObject()).setRequest(clientRequest), HttpMethod.POST);
    // then
    result.doOnError(error -> {
        context.assertEquals("Parameter `path` was not defined in `params`!", error.getMessage());
        Mockito.verify(mockedWebClient, Mockito.times(0)).request(Matchers.any(), Matchers.anyInt(), Matchers.anyString(), Matchers.anyString());
    }).subscribe(response -> context.fail("Error should occur!"), error -> async.complete());
}
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) AdapterRequest(io.knotx.dataobjects.AdapterRequest) 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)

Example 3 with WebClient

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

the class KnotxServerCsrfTest method whenDoPostSecureWithCSRF_expectOK.

@Test
@KnotxConfiguration("test-server-csrf.json")
public void whenDoPostSecureWithCSRF_expectOK(TestContext context) {
    Async async = context.async();
    createPassThroughKnot("test-splitter");
    createPassThroughKnot("test-assembler");
    createSimpleKnot("some-knot", "test", null);
    MultiMap body = MultiMap.caseInsensitiveMultiMap().add("field", "value");
    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()) {
            String token = getToken(ar.result().cookies());
            client.post(KNOTX_SERVER_PORT, KNOTX_SERVER_ADDRESS, "/content/local/simple.html").putHeader(CSRFHandler.DEFAULT_HEADER_NAME, token).sendForm(body, res -> {
                if (res.succeeded()) {
                    context.assertEquals(HttpResponseStatus.OK.code(), res.result().statusCode());
                    async.complete();
                } else {
                    context.fail(ar.cause());
                    async.complete();
                }
            });
        } else {
            context.fail(ar.cause());
            async.complete();
        }
    });
}
Also used : MultiMap(io.vertx.reactivex.core.MultiMap) 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 4 with WebClient

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

the class KnotxServerCsrfTest method whenDoPostSecureWithoutCSRF_expectForbidden.

@Test
@KnotxConfiguration("test-server-csrf.json")
public void whenDoPostSecureWithoutCSRF_expectForbidden(TestContext context) {
    Async async = context.async();
    createPassThroughKnot("test-splitter");
    createPassThroughKnot("test-assembler");
    createSimpleKnot("some-knot", "test", null);
    MultiMap body = MultiMap.caseInsensitiveMultiMap().add("field", "value");
    WebClient client = WebClient.create(Vertx.newInstance(vertx.vertx()));
    client.post(KNOTX_SERVER_PORT, KNOTX_SERVER_ADDRESS, "/content/local/simple.html").sendForm(body, ar -> {
        if (ar.succeeded()) {
            context.assertEquals(HttpResponseStatus.FORBIDDEN.code(), ar.result().statusCode());
            async.complete();
        } else {
            context.fail(ar.cause());
            async.complete();
        }
    });
}
Also used : MultiMap(io.vertx.reactivex.core.MultiMap) 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 5 with WebClient

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

the class KnotxServerCsrfTest method whenDoPostPublicWithoutCSRF_expectOk.

@Test
@KnotxConfiguration("test-server-csrf.json")
public void whenDoPostPublicWithoutCSRF_expectOk(TestContext context) {
    Async async = context.async();
    createPassThroughKnot("test-splitter");
    createPassThroughKnot("test-assembler");
    createSimpleKnot("some-knot", "test", null);
    MultiMap body = MultiMap.caseInsensitiveMultiMap().add("field", "value");
    WebClient client = WebClient.create(Vertx.newInstance(vertx.vertx()));
    client.post(KNOTX_SERVER_PORT, KNOTX_SERVER_ADDRESS, "/content/local/public.html").sendForm(body, ar -> {
        if (ar.succeeded()) {
            context.assertEquals(HttpResponseStatus.OK.code(), ar.result().statusCode());
            async.complete();
        } else {
            context.fail(ar.cause());
            async.complete();
        }
    });
}
Also used : MultiMap(io.vertx.reactivex.core.MultiMap) Async(io.vertx.ext.unit.Async) 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