use of io.vertx.core.http.Cookie in project incubator-servicecomb-java-chassis by apache.
the class CookieItemTest method serverFormattedElement.
@Test
public void serverFormattedElement() {
HashMap<String, Cookie> cookieSet = new HashMap<>();
CookieImpl cookie = new CookieImpl(COOKIE_NAME, COOKIE_VALUE);
cookieSet.put(cookie.getName(), cookie);
when(mockContext.cookieCount()).thenReturn(1);
when(mockContext.cookieMap()).thenReturn(cookieSet);
accessLogEvent.setRoutingContext(mockContext);
ELEMENT.appendServerFormattedItem(accessLogEvent, strBuilder);
Assert.assertEquals(COOKIE_VALUE, strBuilder.toString());
}
use of io.vertx.core.http.Cookie in project vertx-web by vert-x3.
the class CookieHandlerTest method testSimpleCookie.
@Test
public void testSimpleCookie() throws Exception {
router.route().handler(rc -> {
assertEquals(1, rc.request().cookieCount());
Cookie cookie = rc.request().getCookie("foo");
assertNotNull(cookie);
assertEquals("bar", cookie.getValue());
rc.response().end();
});
testRequestWithCookies(HttpMethod.GET, "/", "foo=bar", 200, "OK");
}
use of io.vertx.core.http.Cookie in project vertx-web by vert-x3.
the class CookieHandlerTest method testCookieFields.
@Test
public void testCookieFields() throws Exception {
Cookie cookie = Cookie.cookie("foo", "bar");
assertEquals("foo", cookie.getName());
assertEquals("bar", cookie.getValue());
assertEquals("foo=bar", cookie.encode());
assertNull(cookie.getPath());
cookie.setPath("/somepath");
assertEquals("/somepath", cookie.getPath());
assertEquals("foo=bar; Path=/somepath", cookie.encode());
assertNull(cookie.getDomain());
cookie.setDomain("foo.com");
assertEquals("foo.com", cookie.getDomain());
assertEquals("foo=bar; Path=/somepath; Domain=foo.com", cookie.encode());
long maxAge = 30 * 60;
cookie.setMaxAge(maxAge);
long now = System.currentTimeMillis();
String encoded = cookie.encode();
int startPos = encoded.indexOf("Expires=");
int endPos = encoded.indexOf(';', startPos);
String expiresDate = encoded.substring(startPos + 8, endPos);
Date d = new Date(Utils.parseRFC1123DateTime(expiresDate));
assertTrue(d.getTime() - now >= maxAge);
cookie.setMaxAge(Long.MIN_VALUE);
cookie.setSecure(true);
assertEquals("foo=bar; Path=/somepath; Domain=foo.com; Secure", cookie.encode());
cookie.setHttpOnly(true);
assertEquals("foo=bar; Path=/somepath; Domain=foo.com; Secure; HTTPOnly", cookie.encode());
}
use of io.vertx.core.http.Cookie in project vertx-web by vert-x3.
the class SessionHandlerImpl method sessionCookie.
private Cookie sessionCookie(final RoutingContext context, final Session session) {
// only pick the first cookie, when multiple sessions are used:
// https://www.rfc-editor.org/rfc/rfc6265#section-5.4
// The user agent SHOULD sort the cookie-list in the following order:
// Cookies with longer paths are listed before cookies with shorter paths.
Cookie cookie = context.request().getCookie(sessionCookieName);
if (cookie != null) {
return cookie;
}
cookie = Cookie.cookie(sessionCookieName, session.value());
setCookieProperties(cookie, false);
context.response().addCookie(cookie);
return cookie;
}
use of io.vertx.core.http.Cookie in project vertx-web by vert-x3.
the class SessionHandlerImpl method getSessionId.
private String getSessionId(RoutingContext context) {
if (cookieless) {
// cookieless sessions store the session on the path or the request
// a session is identified by a sequence of characters between braces
String path = context.normalizedPath();
int s = -1;
int e = -1;
for (int i = 0; i < path.length(); i++) {
if (path.charAt(i) == '(') {
s = i + 1;
continue;
}
if (path.charAt(i) == ')') {
// this is a false end, continue looking
if (s != -1) {
e = i;
break;
}
}
}
if (s != -1 && e != -1 && s < e) {
return path.substring(s, e);
}
} else {
// only pick the first cookie, when multiple sessions are used:
// https://www.rfc-editor.org/rfc/rfc6265#section-5.4
// The user agent SHOULD sort the cookie-list in the following order:
// Cookies with longer paths are listed before cookies with shorter paths.
Cookie cookie = context.request().getCookie(sessionCookieName);
if (cookie != null) {
// Look up sessionId
return cookie.getValue();
}
}
return null;
}
Aggregations