Search in sources :

Example 1 with ClientPhase

use of io.vertx.ext.web.client.impl.ClientPhase in project vertx-web by vert-x3.

the class InterceptorTest method testFollowRedirects.

@Test
public void testFollowRedirects() throws Exception {
    server.requestHandler(req -> {
        switch(req.path()) {
            case "/1":
                req.response().setStatusCode(302).putHeader("location", "http://localhost:8080/2").end();
                break;
            default:
                req.response().end();
        }
    });
    startServer();
    List<ClientPhase> phases = new ArrayList<>();
    List<String> requestUris = new ArrayList<>();
    AtomicInteger redirects = new AtomicInteger();
    client.addInterceptor(ctx -> {
        phases.add(ctx.phase());
        switch(ctx.phase()) {
            case PREPARE_REQUEST:
                assertEquals(0, ctx.redirects());
                break;
            case SEND_REQUEST:
                assertEquals(redirects.getAndIncrement(), ctx.redirects());
                requestUris.add(ctx.requestOptions().getURI());
                break;
        }
        ctx.next();
    });
    HttpRequest<Buffer> builder = client.get("/1").host("localhost").port(8080);
    builder.send(onSuccess(resp -> {
        assertEquals(200, resp.statusCode());
        assertEquals(Arrays.asList(ClientPhase.PREPARE_REQUEST, ClientPhase.CREATE_REQUEST, ClientPhase.SEND_REQUEST, ClientPhase.FOLLOW_REDIRECT, ClientPhase.CREATE_REQUEST, ClientPhase.SEND_REQUEST, ClientPhase.RECEIVE_RESPONSE, ClientPhase.DISPATCH_RESPONSE), phases);
        assertEquals(Arrays.asList("/1", "/2"), requestUris);
        complete();
    }));
    await();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) AsyncFile(io.vertx.core.file.AsyncFile) Arrays(java.util.Arrays) BiFunction(java.util.function.BiFunction) MultiMap(io.vertx.core.MultiMap) BodyCodec(io.vertx.ext.web.codec.BodyCodec) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AddressResolverOptions(io.vertx.core.dns.AddressResolverOptions) ClientPhase(io.vertx.ext.web.client.impl.ClientPhase) ArrayList(java.util.ArrayList) HttpTestBase(io.vertx.core.http.HttpTestBase) HttpVersion(io.vertx.core.http.HttpVersion) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpClientOptions(io.vertx.core.http.HttpClientOptions) RepeatRule(io.vertx.ext.unit.junit.RepeatRule) WebClientInternal(io.vertx.ext.web.client.impl.WebClientInternal) OpenOptions(io.vertx.core.file.OpenOptions) Files(java.nio.file.Files) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test) File(java.io.File) JsonArray(io.vertx.core.json.JsonArray) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Rule(org.junit.Rule) Buffer(io.vertx.core.buffer.Buffer) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Handler(io.vertx.core.Handler) Collections(java.util.Collections) HttpContext(io.vertx.ext.web.client.impl.HttpContext) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientPhase(io.vertx.ext.web.client.impl.ClientPhase) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Aggregations

Handler (io.vertx.core.Handler)1 MultiMap (io.vertx.core.MultiMap)1 VertxOptions (io.vertx.core.VertxOptions)1 Buffer (io.vertx.core.buffer.Buffer)1 AddressResolverOptions (io.vertx.core.dns.AddressResolverOptions)1 AsyncFile (io.vertx.core.file.AsyncFile)1 OpenOptions (io.vertx.core.file.OpenOptions)1 HttpClientOptions (io.vertx.core.http.HttpClientOptions)1 HttpServerOptions (io.vertx.core.http.HttpServerOptions)1 HttpTestBase (io.vertx.core.http.HttpTestBase)1 HttpVersion (io.vertx.core.http.HttpVersion)1 JsonArray (io.vertx.core.json.JsonArray)1 RepeatRule (io.vertx.ext.unit.junit.RepeatRule)1 ClientPhase (io.vertx.ext.web.client.impl.ClientPhase)1 HttpContext (io.vertx.ext.web.client.impl.HttpContext)1 WebClientInternal (io.vertx.ext.web.client.impl.WebClientInternal)1 BodyCodec (io.vertx.ext.web.codec.BodyCodec)1 File (java.io.File)1 Files (java.nio.file.Files)1 ArrayList (java.util.ArrayList)1