Java Code Examples for javax.ws.rs.core.NewCookie#valueOf()

The following examples show how to use javax.ws.rs.core.NewCookie#valueOf() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SessionLogoutResourceIntegrationTest.java    From keywhiz with Apache License 2.0 6 votes vote down vote up
@Test public void sendsExpiredCookie() throws Exception {
  Request request = new Request.Builder()
      .post(RequestBody.create(MediaType.parse("text/plain"), ""))
      .url(testUrl("/admin/logout"))
      .build();

  Response response = client.newCall(request).execute();
  assertThat(response.code()).isEqualTo(200);

  List<String> cookies = response.headers(HttpHeaders.SET_COOKIE);
  assertThat(cookies).hasSize(1);

  NewCookie cookie = NewCookie.valueOf(cookies.get(0));
  assertThat(cookie.getName()).isEqualTo("session");
  assertThat(cookie.getValue()).isEqualTo("expired");
  assertThat(cookie.getVersion()).isEqualTo(1);
  assertThat(cookie.getPath()).isEqualTo("/admin");
  assertThat(cookie.isSecure()).isTrue();
  assertThat(cookie.isHttpOnly()).isTrue();
  assertThat(cookie.getExpiry()).isEqualTo(new Date(0));
}
 
Example 2
Source File: NewCookieHeaderProviderTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testFromComplexStringWithExpiresAndHttpOnly() {
    NewCookie c = NewCookie.valueOf(
                  "foo=bar;Comment=comment;Path=path;Max-Age=10;Domain=domain;Secure;"
                  + "Expires=Wed, 09 Jun 2021 10:18:14 GMT;HttpOnly;Version=1");
    assertTrue("bar".equals(c.getValue())
               && "foo".equals(c.getName()));
    assertTrue(1 == c.getVersion()
               && "path".equals(c.getPath())
               && "domain".equals(c.getDomain())
               && "comment".equals(c.getComment())
               && c.isSecure()
               && c.isHttpOnly()
               && 10 == c.getMaxAge());
    Date d = c.getExpiry();
    assertNotNull(d);
    assertEquals("Wed, 09 Jun 2021 10:18:14 GMT", HttpUtils.toHttpDate(d));
}
 
Example 3
Source File: AuthenticatedEncryptedCookieFactory.java    From keywhiz with Apache License 2.0 5 votes vote down vote up
/**
 * Produces a cookie string for a given value and expiration.
 *
 * @param value value of new cookie.
 * @param expiration expiration time of cookie.
 * @return serialized cookie with given value and expiration.
 */
public NewCookie cookieFor(String value, ZonedDateTime expiration) {
  long maxAge = Duration.between(ZonedDateTime.now(clock), expiration).getSeconds();

  HttpCookie cookie = new HttpCookie(config.getName(), value, config.getDomain(),
      config.getPath(), maxAge, config.isHttpOnly(), config.isSecure());

  Response response = newResponse();
  response.addCookie(cookie);
  return NewCookie.valueOf(response.getHttpFields().get(HttpHeader.SET_COOKIE));
}
 
Example 4
Source File: AuthenticatedEncryptedCookieFactory.java    From keywhiz with Apache License 2.0 5 votes vote down vote up
/**
 * Produces an expired cookie string, used to update/overwrite an existing cookie.
 *
 * @return serialized expired cookie with matching parameters to authenticating cookie.
 */
public NewCookie getExpiredSessionCookie() {
  HttpCookie cookie = new HttpCookie(config.getName(), "expired", config.getDomain(), config.getPath(),
      0, config.isHttpOnly(), config.isSecure());

  Response response = newResponse();
  response.addCookie(cookie);
  return NewCookie.valueOf(response.getHttpFields().get(HttpHeader.SET_COOKIE));
}
 
Example 5
Source File: ResponseImpl.java    From cxf with Apache License 2.0 5 votes vote down vote up
public Map<String, NewCookie> getCookies() {
    List<Object> cookieValues = metadata.get(HttpHeaders.SET_COOKIE);
    if (cookieValues == null) {
        return Collections.emptyMap();
    }
    Map<String, NewCookie> cookies = new HashMap<>();
    for (Object o : cookieValues) {
        NewCookie newCookie = NewCookie.valueOf(o.toString());
        cookies.put(newCookie.getName(), newCookie);
    }
    return cookies;
}
 
Example 6
Source File: NewCookieHeaderProviderTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testFromComplexString() {
    NewCookie c = NewCookie.valueOf(
                  "foo=bar;Comment=comment;Path=path;Max-Age=10;Domain=domain;Secure;Version=1");
    assertTrue("bar".equals(c.getValue())
               && "foo".equals(c.getName())
               && 1 == c.getVersion()
               && "path".equals(c.getPath())
               && "domain".equals(c.getDomain())
               && "comment".equals(c.getComment())
               && 10 == c.getMaxAge());
}
 
Example 7
Source File: NewCookieHeaderProviderTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testFromComplexStringLowerCase() {
    NewCookie c = NewCookie.valueOf(
                  "foo=bar;comment=comment;path=path;max-age=10;domain=domain;secure;version=1");
    assertTrue("bar".equals(c.getValue())
               && "foo".equals(c.getName())
               && 1 == c.getVersion()
               && "path".equals(c.getPath())
               && "domain".equals(c.getDomain())
               && "comment".equals(c.getComment())
               && 10 == c.getMaxAge());
}
 
Example 8
Source File: NewCookieHeaderProviderTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testFromStringWithSpaces() {
    NewCookie c = NewCookie.valueOf(
                  "foo=bar; Comment=comment; Path=path; Max-Age=10; Domain=domain; Secure; Version=1");
    assertTrue("bar".equals(c.getValue())
               && "foo".equals(c.getName())
               && 1 == c.getVersion()
               && "path".equals(c.getPath())
               && "domain".equals(c.getDomain())
               && "comment".equals(c.getComment())
               && 10 == c.getMaxAge());
}
 
Example 9
Source File: NewCookieHeaderProviderTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testFromStringWithSpecialChar() {
    NewCookie c = NewCookie.valueOf(
                  "foo=\"bar (space)<>[]\"; Comment=\"comment@comment:,\"; Path=\"/path?path\"; Max-Age=10; "
                  + "Domain=\"domain.com\"; Secure; Version=1");
    assertTrue("bar (space)<>[]".equals(c.getValue())
               && "foo".equals(c.getName())
               && 1 == c.getVersion()
               && "/path?path".equals(c.getPath())
               && "domain.com".equals(c.getDomain())
               && "comment@comment:,".equals(c.getComment())
               && 10 == c.getMaxAge());
}
 
Example 10
Source File: NewCookieHeaderProviderTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testNullValue() throws Exception {
    NewCookie.valueOf(null);
}
 
Example 11
Source File: NewCookieHeaderProviderTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testFromSimpleString() {
    NewCookie c = NewCookie.valueOf("foo=bar");
    assertTrue("bar".equals(c.getValue())
               && "foo".equals(c.getName()));
}
 
Example 12
Source File: NewCookieHeaderProviderTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testFromSimpleStringWithExpires() {
    NewCookie c = NewCookie.valueOf("foo=bar;Expires=Wed, 09 Jun 2021 10:18:14 GMT");
    assertTrue("bar".equals(c.getValue())
               && "foo".equals(c.getName()));
}
 
Example 13
Source File: NewCookieHeaderProviderTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testNoValue() {
    NewCookie c = NewCookie.valueOf("foo=");
    assertTrue("".equals(c.getValue())
               && "foo".equals(c.getName()));
}