Search in sources :

Example 6 with ResponsePredicate

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);
    });
}
Also used : AsyncFile(io.vertx.core.file.AsyncFile) CoreMatchers(org.hamcrest.CoreMatchers) ResponsePredicate(io.vertx.ext.web.client.predicate.ResponsePredicate) java.util(java.util) ErrorConverter(io.vertx.ext.web.client.predicate.ErrorConverter) URLDecoder(java.net.URLDecoder) io.vertx.core(io.vertx.core) DecodeException(io.vertx.core.json.DecodeException) HttpPostRequestEncoder(io.netty.handler.codec.http.multipart.HttpPostRequestEncoder) BodyCodec(io.vertx.ext.web.codec.BodyCodec) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ResponsePredicateResult(io.vertx.ext.web.client.predicate.ResponsePredicateResult) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) MultipartForm(io.vertx.ext.web.multipart.MultipartForm) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WriteStream(io.vertx.core.streams.WriteStream) ReadStream(io.vertx.core.streams.ReadStream) BiConsumer(java.util.function.BiConsumer) JsonObject(io.vertx.core.json.JsonObject) ConnectException(java.net.ConnectException) SocketAddress(io.vertx.core.net.SocketAddress) ProxyOptions(io.vertx.core.net.ProxyOptions) UsernamePasswordCredentials(io.vertx.ext.auth.authentication.UsernamePasswordCredentials) OpenOptions(io.vertx.core.file.OpenOptions) MalformedURLException(java.net.MalformedURLException) Files(java.nio.file.Files) Test(org.junit.Test) io.vertx.core.http(io.vertx.core.http) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) StandardCharsets(java.nio.charset.StandardCharsets) Consumer(java.util.function.Consumer) JsonArray(io.vertx.core.json.JsonArray) HtdigestCredentials(io.vertx.ext.auth.htdigest.HtdigestCredentials) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) TokenCredentials(io.vertx.ext.auth.authentication.TokenCredentials) Repeat(io.vertx.test.core.Repeat) URLEncoder(java.net.URLEncoder) Buffer(io.vertx.core.buffer.Buffer) ProxyType(io.vertx.core.net.ProxyType) NetServer(io.vertx.core.net.NetServer) WineAndCheese(io.vertx.ext.web.client.jackson.WineAndCheese) Cert(io.vertx.test.tls.Cert) ResponsePredicate(io.vertx.ext.web.client.predicate.ResponsePredicate) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 7 with ResponsePredicate

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());
    });
}
Also used : ResponsePredicate(io.vertx.ext.web.client.predicate.ResponsePredicate) Test(org.junit.Test)

Example 8 with ResponsePredicate

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);
    });
}
Also used : AsyncFile(io.vertx.core.file.AsyncFile) CoreMatchers(org.hamcrest.CoreMatchers) ResponsePredicate(io.vertx.ext.web.client.predicate.ResponsePredicate) java.util(java.util) ErrorConverter(io.vertx.ext.web.client.predicate.ErrorConverter) URLDecoder(java.net.URLDecoder) io.vertx.core(io.vertx.core) DecodeException(io.vertx.core.json.DecodeException) HttpPostRequestEncoder(io.netty.handler.codec.http.multipart.HttpPostRequestEncoder) BodyCodec(io.vertx.ext.web.codec.BodyCodec) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ResponsePredicateResult(io.vertx.ext.web.client.predicate.ResponsePredicateResult) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) MultipartForm(io.vertx.ext.web.multipart.MultipartForm) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WriteStream(io.vertx.core.streams.WriteStream) ReadStream(io.vertx.core.streams.ReadStream) BiConsumer(java.util.function.BiConsumer) JsonObject(io.vertx.core.json.JsonObject) ConnectException(java.net.ConnectException) SocketAddress(io.vertx.core.net.SocketAddress) ProxyOptions(io.vertx.core.net.ProxyOptions) UsernamePasswordCredentials(io.vertx.ext.auth.authentication.UsernamePasswordCredentials) OpenOptions(io.vertx.core.file.OpenOptions) MalformedURLException(java.net.MalformedURLException) Files(java.nio.file.Files) Test(org.junit.Test) io.vertx.core.http(io.vertx.core.http) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) StandardCharsets(java.nio.charset.StandardCharsets) Consumer(java.util.function.Consumer) JsonArray(io.vertx.core.json.JsonArray) HtdigestCredentials(io.vertx.ext.auth.htdigest.HtdigestCredentials) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) TokenCredentials(io.vertx.ext.auth.authentication.TokenCredentials) Repeat(io.vertx.test.core.Repeat) URLEncoder(java.net.URLEncoder) Buffer(io.vertx.core.buffer.Buffer) ProxyType(io.vertx.core.net.ProxyType) NetServer(io.vertx.core.net.NetServer) WineAndCheese(io.vertx.ext.web.client.jackson.WineAndCheese) Cert(io.vertx.test.tls.Cert) ResponsePredicate(io.vertx.ext.web.client.predicate.ResponsePredicate) Test(org.junit.Test)

Example 9 with ResponsePredicate

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));
    });
}
Also used : ResponsePredicate(io.vertx.ext.web.client.predicate.ResponsePredicate) Test(org.junit.Test)

Aggregations

ResponsePredicate (io.vertx.ext.web.client.predicate.ResponsePredicate)9 Test (org.junit.Test)6 ErrorConverter (io.vertx.ext.web.client.predicate.ErrorConverter)4 Buffer (io.vertx.core.buffer.Buffer)3 JsonObject (io.vertx.core.json.JsonObject)3 HttpPostRequestEncoder (io.netty.handler.codec.http.multipart.HttpPostRequestEncoder)2 io.vertx.core (io.vertx.core)2 AsyncFile (io.vertx.core.file.AsyncFile)2 OpenOptions (io.vertx.core.file.OpenOptions)2 io.vertx.core.http (io.vertx.core.http)2 DecodeException (io.vertx.core.json.DecodeException)2 JsonArray (io.vertx.core.json.JsonArray)2 NetServer (io.vertx.core.net.NetServer)2 ProxyOptions (io.vertx.core.net.ProxyOptions)2 ProxyType (io.vertx.core.net.ProxyType)2 SocketAddress (io.vertx.core.net.SocketAddress)2 ReadStream (io.vertx.core.streams.ReadStream)2 WriteStream (io.vertx.core.streams.WriteStream)2 TokenCredentials (io.vertx.ext.auth.authentication.TokenCredentials)2 UsernamePasswordCredentials (io.vertx.ext.auth.authentication.UsernamePasswordCredentials)2