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();
}));
}
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();
}));
}
Aggregations