Search in sources :

Example 11 with Cookie

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());
}
Also used : Cookie(io.vertx.core.http.Cookie) CookieImpl(io.vertx.core.http.impl.CookieImpl) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 12 with Cookie

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");
}
Also used : Cookie(io.vertx.core.http.Cookie) Test(org.junit.Test)

Example 13 with Cookie

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());
}
Also used : Cookie(io.vertx.core.http.Cookie) Test(org.junit.Test)

Example 14 with Cookie

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;
}
Also used : Cookie(io.vertx.core.http.Cookie)

Example 15 with 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;
}
Also used : Cookie(io.vertx.core.http.Cookie)

Aggregations

Cookie (io.vertx.core.http.Cookie)15 Test (org.junit.Test)9 CookieImpl (io.vertx.core.http.impl.CookieImpl)4 HashMap (java.util.HashMap)4 Session (io.vertx.ext.web.Session)2 AsyncResult (io.vertx.core.AsyncResult)1 Future (io.vertx.core.Future)1 Handler (io.vertx.core.Handler)1 Promise (io.vertx.core.Promise)1 Vertx (io.vertx.core.Vertx)1 CookieSameSite (io.vertx.core.http.CookieSameSite)1 HttpHeaders (io.vertx.core.http.HttpHeaders)1 HttpServerRequest (io.vertx.core.http.HttpServerRequest)1 Logger (io.vertx.core.impl.logging.Logger)1 LoggerFactory (io.vertx.core.impl.logging.LoggerFactory)1 User (io.vertx.ext.auth.User)1 RoutingContext (io.vertx.ext.web.RoutingContext)1 SessionHandler (io.vertx.ext.web.handler.SessionHandler)1 RoutingContextInternal (io.vertx.ext.web.impl.RoutingContextInternal)1 SessionStore (io.vertx.ext.web.sstore.SessionStore)1