use of io.vertx.ext.web.client.predicate.ResponsePredicate in project vertx-web by vert-x3.
the class WebClientTest method testExpectCustomExceptionWithResponseBody.
@Test
public void testExpectCustomExceptionWithResponseBody() throws Exception {
UUID uuid = UUID.randomUUID();
ResponsePredicate predicate = ResponsePredicate.create(ResponsePredicate.SC_SUCCESS, ErrorConverter.createFullBody(result -> {
JsonObject body = result.response().bodyAsJsonObject();
return new CustomException(UUID.fromString(body.getString("tag")), body.getString("message"));
}));
testExpectation(true, req -> req.expect(predicate), httpServerResponse -> {
httpServerResponse.setStatusCode(400).end(new JsonObject().put("tag", uuid.toString()).put("message", "tilt").toBuffer());
}, ar -> {
Throwable cause = ar.cause();
assertThat(cause, instanceOf(CustomException.class));
CustomException customException = (CustomException) cause;
assertEquals("tilt", customException.getMessage());
assertEquals(uuid, customException.tag);
});
}
use of io.vertx.ext.web.client.predicate.ResponsePredicate in project vertx-web by vert-x3.
the class WebClientTest method testExpectCustomException.
@Test
public void testExpectCustomException() throws Exception {
ResponsePredicate predicate = ResponsePredicate.create(r -> ResponsePredicateResult.failure("boom"), result -> new CustomException(result.message()));
testExpectation(true, req -> req.expect(predicate), HttpServerResponse::end, ar -> {
Throwable cause = ar.cause();
assertThat(cause, instanceOf(CustomException.class));
CustomException customException = (CustomException) cause;
assertEquals("boom", customException.getMessage());
});
}
use of io.vertx.ext.web.client.predicate.ResponsePredicate in project vertx-web by vert-x3.
the class WebClientTest method testExpectCustomExceptionWithStatusCode.
@Test
public void testExpectCustomExceptionWithStatusCode() throws Exception {
UUID uuid = UUID.randomUUID();
int statusCode = 400;
ResponsePredicate predicate = ResponsePredicate.create(ResponsePredicate.SC_SUCCESS, ErrorConverter.create(result -> {
int code = result.response().statusCode();
return new CustomException(uuid, String.valueOf(code));
}));
testExpectation(true, req -> req.expect(predicate), httpServerResponse -> {
httpServerResponse.setStatusCode(statusCode).end(TestUtils.randomBuffer(2048));
}, ar -> {
Throwable cause = ar.cause();
assertThat(cause, instanceOf(CustomException.class));
CustomException customException = (CustomException) cause;
assertEquals(String.valueOf(statusCode), customException.getMessage());
assertEquals(uuid, customException.tag);
});
}
use of io.vertx.ext.web.client.predicate.ResponsePredicate in project vertx-web by vert-x3.
the class WebClientTest method testExpectFunctionThrowsException.
@Test
public void testExpectFunctionThrowsException() throws Exception {
ResponsePredicate predicate = ResponsePredicate.create(r -> {
throw new IndexOutOfBoundsException("boom");
});
testExpectation(true, req -> req.expect(predicate), HttpServerResponse::end, ar -> {
assertThat(ar.cause(), instanceOf(IndexOutOfBoundsException.class));
});
}
Aggregations