Search in sources :

Example 1 with CookieStore

use of io.vertx.ext.web.client.spi.CookieStore in project vertx-web by vert-x3.

the class SessionAwareWebClientTest method testCookieStoreIsFluent.

@Test
public void testCookieStoreIsFluent(TestContext context) {
    CookieStore store = CookieStore.build();
    Cookie cookie = new DefaultCookie("a", "a");
    context.assertTrue(store == store.put(cookie));
    context.assertTrue(store == store.remove(cookie));
}
Also used : Cookie(io.netty.handler.codec.http.cookie.Cookie) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) CookieStore(io.vertx.ext.web.client.spi.CookieStore) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) Test(org.junit.Test)

Example 2 with CookieStore

use of io.vertx.ext.web.client.spi.CookieStore in project vertx-web by vert-x3.

the class SessionAwareWebClientTest method testCookieStore.

@Test
public void testCookieStore(TestContext context) {
    CookieStore store = CookieStore.build();
    Cookie c;
    c = new DefaultCookie("a", "1");
    store.put(c);
    c = new DefaultCookie("b", "2");
    c.setDomain("vertx.io");
    store.put(c);
    c = new DefaultCookie("c", "3");
    c.setDomain("www.vertx.io");
    c.setPath("/web-client");
    store.put(c);
    c = new DefaultCookie("d", "4");
    c.setPath("/web-client");
    store.put(c);
    c = new DefaultCookie("e", "5");
    c.setDomain("vertx.io");
    c.setSecure(true);
    store.put(c);
    c = new DefaultCookie("b", "20");
    c.setDomain("www.vertx.io");
    store.put(c);
    c = new DefaultCookie("b", "200");
    c.setDomain("www.vertx.io");
    c.setPath("/web-client");
    store.put(c);
    validate(context, store.get(false, "www.vertx.io", "/"), new String[] { "a", "b" }, new String[] { "1", "20" });
    validate(context, store.get(false, "a.www.vertx.io", "/"), new String[] { "a", "b" }, new String[] { "1", "20" });
    validate(context, store.get(false, "test.vertx.io", "/"), new String[] { "a", "b" }, new String[] { "1", "2" });
    validate(context, store.get(false, "www.vertx.io", "/web-client"), new String[] { "a", "b", "c", "d" }, new String[] { "1", "200", "3", "4" });
    validate(context, store.get(true, "test.vertx.io", "/"), new String[] { "a", "b", "e" }, new String[] { "1", "2", "5" });
}
Also used : Cookie(io.netty.handler.codec.http.cookie.Cookie) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) CookieStore(io.vertx.ext.web.client.spi.CookieStore) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) Test(org.junit.Test)

Example 3 with CookieStore

use of io.vertx.ext.web.client.spi.CookieStore in project vertx-web by vert-x3.

the class SessionAwareWebClientTest method testSendRequest.

@Test
public void testSendRequest(TestContext context) throws IOException {
    AtomicInteger count = new AtomicInteger(0);
    client = buildClient(plainWebClient, new CookieStoreImpl() {

        @Override
        public CookieStore put(Cookie cookie) {
            count.incrementAndGet();
            return super.put(cookie);
        }
    });
    String encodedCookie = ServerCookieEncoder.STRICT.encode(new DefaultCookie("a", "1"));
    prepareServer(context, req -> {
        req.response().headers().add("set-cookie", encodedCookie);
    });
    int expected = 7;
    Async async = context.async(expected);
    Handler<AsyncResult<HttpResponse<Buffer>>> handler = ar -> {
        async.countDown();
    };
    HttpRequest<Buffer> req = client.post("/");
    req.send(handler);
    req.sendBuffer(Buffer.buffer(), handler);
    req.sendForm(HttpHeaders.set("a", "b"), handler);
    req.sendJson("", handler);
    req.sendJsonObject(new JsonObject(), handler);
    req.sendMultipartForm(MultipartForm.create().attribute("a", "b"), handler);
    File f = File.createTempFile("vertx", ".tmp");
    f.deleteOnExit();
    AsyncFile asyncFile = vertx.fileSystem().openBlocking(f.getAbsolutePath(), new OpenOptions());
    req.sendStream(asyncFile, handler);
    async.await();
    asyncFile.close();
    context.assertEquals(expected, count.get());
}
Also used : Cookie(io.netty.handler.codec.http.cookie.Cookie) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) Buffer(io.vertx.core.buffer.Buffer) IntStream(java.util.stream.IntStream) TestContext(io.vertx.ext.unit.TestContext) AsyncFile(io.vertx.core.file.AsyncFile) Async(io.vertx.ext.unit.Async) RunWith(org.junit.runner.RunWith) ArrayList(java.util.ArrayList) MultipartForm(io.vertx.ext.web.multipart.MultipartForm) ServerCookieDecoder(io.netty.handler.codec.http.cookie.ServerCookieDecoder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) CookieStore(io.vertx.ext.web.client.spi.CookieStore) Before(org.junit.Before) OpenOptions(io.vertx.core.file.OpenOptions) Vertx(io.vertx.core.Vertx) Set(java.util.Set) IOException(java.io.IOException) Test(org.junit.Test) Verticle(io.vertx.core.Verticle) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Cookie(io.netty.handler.codec.http.cookie.Cookie) File(java.io.File) io.vertx.core.http(io.vertx.core.http) Consumer(java.util.function.Consumer) List(java.util.List) Ignore(org.junit.Ignore) Buffer(io.vertx.core.buffer.Buffer) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) CookieStoreImpl(io.vertx.ext.web.client.impl.CookieStoreImpl) AbstractVerticle(io.vertx.core.AbstractVerticle) Handler(io.vertx.core.Handler) Assert.assertEquals(org.junit.Assert.assertEquals) ServerCookieEncoder(io.netty.handler.codec.http.cookie.ServerCookieEncoder) OpenOptions(io.vertx.core.file.OpenOptions) CookieStoreImpl(io.vertx.ext.web.client.impl.CookieStoreImpl) JsonObject(io.vertx.core.json.JsonObject) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Async(io.vertx.ext.unit.Async) AsyncFile(io.vertx.core.file.AsyncFile) AsyncResult(io.vertx.core.AsyncResult) AsyncFile(io.vertx.core.file.AsyncFile) File(java.io.File) Test(org.junit.Test)

Example 4 with CookieStore

use of io.vertx.ext.web.client.spi.CookieStore in project vertx-web by vert-x3.

the class SessionAwareInterceptor method processResponse.

private void processResponse(HttpContext<?> context) {
    List<String> cookieHeaders = context.response().cookies();
    if (cookieHeaders == null) {
        return;
    }
    RequestOptions request = context.requestOptions();
    CookieStore cookieStore = parentClient.cookieStore();
    cookieHeaders.forEach(header -> {
        Cookie cookie = ClientCookieDecoder.STRICT.decode(header);
        if (cookie != null) {
            if (cookie.domain() == null) {
                // Set the domain if missing, because we need to send cookies
                // only to the domains we received them from.
                cookie.setDomain(((HttpRequestImpl<?>) context.request()).virtualHost != null ? ((HttpRequestImpl<?>) context.request()).virtualHost : request.getHost());
            }
            cookieStore.put(cookie);
        }
    });
}
Also used : Cookie(io.netty.handler.codec.http.cookie.Cookie) CookieStore(io.vertx.ext.web.client.spi.CookieStore) RequestOptions(io.vertx.core.http.RequestOptions)

Example 5 with CookieStore

use of io.vertx.ext.web.client.spi.CookieStore in project vertx-web by vert-x3.

the class SessionAwareInterceptor method processRedirectResponse.

private void processRedirectResponse(HttpContext<?> context) {
    // Now the context contains the redirect request in clientRequest() and the original request in request()
    List<String> cookieHeaders = context.clientResponse().cookies();
    if (cookieHeaders == null) {
        return;
    }
    RequestOptions originalRequest = context.requestOptions();
    CookieStore cookieStore = parentClient.cookieStore();
    String domain = URI.create(context.clientResponse().request().absoluteURI()).getHost();
    if (domain.equals(originalRequest.getHost()) && ((HttpRequestImpl<?>) context.request()).virtualHost != null) {
        domain = ((HttpRequestImpl<?>) context.request()).virtualHost;
    }
    final String finalDomain = domain;
    cookieHeaders.forEach(header -> {
        Cookie cookie = ClientCookieDecoder.STRICT.decode(header);
        if (cookie != null) {
            if (cookie.domain() == null) {
                // Set the domain if missing, because we need to send cookies
                // only to the domains we received them from.
                cookie.setDomain(finalDomain);
            }
            cookieStore.put(cookie);
        }
    });
}
Also used : Cookie(io.netty.handler.codec.http.cookie.Cookie) CookieStore(io.vertx.ext.web.client.spi.CookieStore) RequestOptions(io.vertx.core.http.RequestOptions)

Aggregations

Cookie (io.netty.handler.codec.http.cookie.Cookie)5 CookieStore (io.vertx.ext.web.client.spi.CookieStore)5 DefaultCookie (io.netty.handler.codec.http.cookie.DefaultCookie)3 Test (org.junit.Test)3 RequestOptions (io.vertx.core.http.RequestOptions)2 ServerCookieDecoder (io.netty.handler.codec.http.cookie.ServerCookieDecoder)1 ServerCookieEncoder (io.netty.handler.codec.http.cookie.ServerCookieEncoder)1 AbstractVerticle (io.vertx.core.AbstractVerticle)1 AsyncResult (io.vertx.core.AsyncResult)1 Handler (io.vertx.core.Handler)1 Verticle (io.vertx.core.Verticle)1 Vertx (io.vertx.core.Vertx)1 Buffer (io.vertx.core.buffer.Buffer)1 AsyncFile (io.vertx.core.file.AsyncFile)1 OpenOptions (io.vertx.core.file.OpenOptions)1 io.vertx.core.http (io.vertx.core.http)1 JsonObject (io.vertx.core.json.JsonObject)1 Async (io.vertx.ext.unit.Async)1 TestContext (io.vertx.ext.unit.TestContext)1 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)1