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