Search in sources :

Example 6 with Cookie

use of io.vertx.core.http.Cookie in project vertx-web by vert-x3.

the class CookieHandlerTest method testCookiesChangedInHandler.

@Test
public void testCookiesChangedInHandler() throws Exception {
    router.route().handler(rc -> {
        assertEquals(3, rc.request().cookieCount());
        assertEquals("bar", rc.request().getCookie("foo").getValue());
        assertEquals("blibble", rc.request().getCookie("wibble").getValue());
        assertEquals("flop", rc.request().getCookie("plop").getValue());
        rc.response().removeCookie("plop");
        // the expected number of elements should remain the same as we're sending an invalidate cookie back
        assertEquals(3, rc.request().cookieCount());
        rc.next();
    });
    router.route().handler(rc -> {
        assertEquals("bar", rc.request().getCookie("foo").getValue());
        assertEquals("blibble", rc.request().getCookie("wibble").getValue());
        assertNotNull(rc.request().getCookie("plop"));
        rc.response().addCookie(Cookie.cookie("fleeb", "floob"));
        assertEquals(4, rc.request().cookieCount());
        assertNull(rc.response().removeCookie("blarb"));
        assertEquals(4, rc.request().cookieCount());
        Cookie foo = rc.request().getCookie("foo");
        foo.setValue("blah");
        rc.response().end();
    });
    testRequest(HttpMethod.GET, "/", req -> req.headers().set("Cookie", "foo=bar; wibble=blibble; plop=flop"), resp -> {
        List<String> cookies = resp.headers().getAll("set-cookie");
        assertEquals(3, cookies.size());
        assertTrue(cookies.contains("foo=blah"));
        assertTrue(cookies.contains("fleeb=floob"));
        boolean found = false;
        for (String s : cookies) {
            if (s.startsWith("plop")) {
                found = true;
                assertTrue(s.contains("Max-Age=0"));
                assertTrue(s.contains("Expires="));
                break;
            }
        }
        assertTrue(found);
    }, 200, "OK", null);
}
Also used : Cookie(io.vertx.core.http.Cookie) Test(org.junit.Test)

Example 7 with Cookie

use of io.vertx.core.http.Cookie in project java-chassis by ServiceComb.

the class CookieAccessItem method appendServerFormattedItem.

@Override
public void appendServerFormattedItem(ServerAccessLogEvent accessLogEvent, StringBuilder builder) {
    Map<String, Cookie> cookieMap = accessLogEvent.getRoutingContext().cookieMap();
    if (null == cookieMap) {
        builder.append(RESULT_NOT_FOUND);
        return;
    }
    for (Cookie cookie : cookieMap.values()) {
        if (varName.equals(cookie.getName())) {
            builder.append(cookie.getValue());
            return;
        }
    }
    builder.append(RESULT_NOT_FOUND);
}
Also used : Cookie(io.vertx.core.http.Cookie)

Example 8 with Cookie

use of io.vertx.core.http.Cookie in project java-chassis by ServiceComb.

the class CookieItemTest method serverFormattedElementOnNotFound.

@Test
public void serverFormattedElementOnNotFound() {
    HashMap<String, Cookie> cookieSet = new HashMap<>();
    CookieImpl cookie = new CookieImpl("anotherCookieName", COOKIE_VALUE);
    cookieSet.put(cookie.getName(), cookie);
    Mockito.when(mockContext.cookieCount()).thenReturn(1);
    Mockito.when(mockContext.cookieMap()).thenReturn(cookieSet);
    accessLogEvent.setRoutingContext(mockContext);
    ELEMENT.appendServerFormattedItem(accessLogEvent, strBuilder);
    Assert.assertEquals("-", 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 9 with Cookie

use of io.vertx.core.http.Cookie in project java-chassis by ServiceComb.

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 10 with Cookie

use of io.vertx.core.http.Cookie in project incubator-servicecomb-java-chassis by apache.

the class CookieItemTest method serverFormattedElementOnNotFound.

@Test
public void serverFormattedElementOnNotFound() {
    HashMap<String, Cookie> cookieSet = new HashMap<>();
    CookieImpl cookie = new CookieImpl("anotherCookieName", COOKIE_VALUE);
    cookieSet.put(cookie.getName(), cookie);
    Mockito.when(mockContext.cookieCount()).thenReturn(1);
    Mockito.when(mockContext.cookieMap()).thenReturn(cookieSet);
    accessLogEvent.setRoutingContext(mockContext);
    ELEMENT.appendServerFormattedItem(accessLogEvent, strBuilder);
    Assert.assertEquals("-", 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)

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