Search in sources :

Example 11 with HttpClient

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

the class SampleApplicationTest method testPostRequest.

private void testPostRequest(TestContext context, String url, Map<String, String> formData, String expectedResponseFile, boolean ajaxCall) {
    HttpClient client = Vertx.newInstance(vertx.vertx()).createHttpClient();
    Async async = context.async();
    Observable<HttpClientResponse> request = request(client, HttpMethod.POST, KNOTX_SERVER_PORT, KNOTX_SERVER_ADDRESS, url, req -> {
        String bodyForm = formData.entrySet().stream().map(entry -> entry.getKey() + "=" + entry.getValue()).reduce((p1, p2) -> p1 + "&" + p2).get();
        req.headers().set("content-length", String.valueOf(bodyForm.length()));
        req.headers().set("content-type", "application/x-www-form-urlencoded");
        if (ajaxCall) {
            req.headers().set("X-Requested-With", "XMLHttpRequest");
        }
        req.write(bodyForm);
    });
    request.subscribe(resp -> resp.bodyHandler(body -> {
        context.assertEquals(resp.statusCode(), HttpResponseStatus.OK.code());
        try {
            context.assertEquals(Jsoup.parse(body.toString()).body().html(), Jsoup.parse(FileReader.readText(expectedResponseFile)).body().html());
        } catch (Exception e) {
            context.fail(e);
        }
        async.complete();
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) 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) Maps(com.google.common.collect.Maps) RuleChain(org.junit.rules.RuleChain) HttpClientRequest(io.vertx.reactivex.core.http.HttpClientRequest) Vertx(io.vertx.reactivex.core.Vertx) Rule(org.junit.Rule) FileReader(io.knotx.junit.util.FileReader) HttpMethod(io.vertx.core.http.HttpMethod) Map(java.util.Map) Observable(io.reactivex.Observable) HttpClient(io.vertx.reactivex.core.http.HttpClient) Jsoup(org.jsoup.Jsoup) 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) HttpClientResponse(io.vertx.reactivex.core.http.HttpClientResponse)

Example 12 with HttpClient

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

the class SampleApplicationTest method testGetRequest.

private void testGetRequest(TestContext context, String url, String expectedResponseFile) {
    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(resp.statusCode(), HttpResponseStatus.OK.code());
        try {
            context.assertEquals(Jsoup.parse(body.toString()).body().html().trim(), Jsoup.parse(FileReader.readText(expectedResponseFile)).body().html().trim());
        } catch (Exception e) {
            context.fail(e);
        }
        client.close();
        async.complete();
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) 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) Maps(com.google.common.collect.Maps) RuleChain(org.junit.rules.RuleChain) HttpClientRequest(io.vertx.reactivex.core.http.HttpClientRequest) Vertx(io.vertx.reactivex.core.Vertx) Rule(org.junit.Rule) FileReader(io.knotx.junit.util.FileReader) HttpMethod(io.vertx.core.http.HttpMethod) Map(java.util.Map) Observable(io.reactivex.Observable) HttpClient(io.vertx.reactivex.core.http.HttpClient) Jsoup(org.jsoup.Jsoup) 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)

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