Search in sources :

Example 1 with CookieImpl

use of io.vertx.ext.web.impl.CookieImpl in project incubator-servicecomb-java-chassis by apache.

the class CookieItemTest method getFormattedElementOnNotFound.

@Test
public void getFormattedElementOnNotFound() {
    AccessLogParam<RoutingContext> param = new AccessLogParam<>();
    RoutingContext mockContext = Mockito.mock(RoutingContext.class);
    HashSet<Cookie> cookieSet = new HashSet<>();
    String cookieValue = "cookieValue";
    CookieImpl cookie = new CookieImpl("anotherCookieName", cookieValue);
    cookieSet.add(cookie);
    Mockito.when(mockContext.cookieCount()).thenReturn(1);
    Mockito.when(mockContext.cookies()).thenReturn(cookieSet);
    param.setContextData(mockContext);
    String result = ELEMENT.getFormattedItem(param);
    Assert.assertEquals("-", result);
}
Also used : Cookie(io.vertx.ext.web.Cookie) RoutingContext(io.vertx.ext.web.RoutingContext) CookieImpl(io.vertx.ext.web.impl.CookieImpl) AccessLogParam(org.apache.servicecomb.transport.rest.vertx.accesslog.AccessLogParam) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with CookieImpl

use of io.vertx.ext.web.impl.CookieImpl in project incubator-servicecomb-java-chassis by apache.

the class CookieItemTest method getFormattedElement.

@Test
public void getFormattedElement() {
    AccessLogParam<RoutingContext> param = new AccessLogParam<>();
    RoutingContext mockContext = Mockito.mock(RoutingContext.class);
    HashSet<Cookie> cookieSet = new HashSet<>();
    String cookieValue = "cookieValue";
    CookieImpl cookie = new CookieImpl(COOKIE_NAME, cookieValue);
    cookieSet.add(cookie);
    Mockito.when(mockContext.cookieCount()).thenReturn(1);
    Mockito.when(mockContext.cookies()).thenReturn(cookieSet);
    param.setContextData(mockContext);
    String result = ELEMENT.getFormattedItem(param);
    Assert.assertEquals(cookieValue, result);
}
Also used : Cookie(io.vertx.ext.web.Cookie) RoutingContext(io.vertx.ext.web.RoutingContext) CookieImpl(io.vertx.ext.web.impl.CookieImpl) AccessLogParam(org.apache.servicecomb.transport.rest.vertx.accesslog.AccessLogParam) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with CookieImpl

use of io.vertx.ext.web.impl.CookieImpl in project vertx-web by vert-x3.

the class CookieHandlerImpl method handle.

@Override
public void handle(RoutingContext context) {
    String cookieHeader = context.request().headers().get(COOKIE);
    if (cookieHeader != null) {
        Set<io.netty.handler.codec.http.cookie.Cookie> nettyCookies = ServerCookieDecoder.STRICT.decode(cookieHeader);
        for (io.netty.handler.codec.http.cookie.Cookie cookie : nettyCookies) {
            Cookie ourCookie = new CookieImpl(cookie);
            context.addCookie(ourCookie);
        }
    }
    context.addHeadersEndHandler(v -> {
        // save the cookies
        Set<Cookie> cookies = context.cookies();
        for (Cookie cookie : cookies) {
            if (cookie.isChanged()) {
                context.response().headers().add(SET_COOKIE, cookie.encode());
            }
        }
    });
    context.next();
}
Also used : Cookie(io.vertx.ext.web.Cookie) CookieImpl(io.vertx.ext.web.impl.CookieImpl)

Aggregations

Cookie (io.vertx.ext.web.Cookie)3 CookieImpl (io.vertx.ext.web.impl.CookieImpl)3 RoutingContext (io.vertx.ext.web.RoutingContext)2 HashSet (java.util.HashSet)2 AccessLogParam (org.apache.servicecomb.transport.rest.vertx.accesslog.AccessLogParam)2 Test (org.junit.Test)2