下面列出了javax.ws.rs.core.NewCookie#valueOf() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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));
}
@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));
}
/**
* 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));
}
/**
* 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));
}
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;
}
@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());
}
@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());
}
@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());
}
@Test
public void testFromStringWithSpecialChar() {
NewCookie c = NewCookie.valueOf(
"foo=\"bar (space)<>[]\"; Comment=\"[email protected]:,\"; 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())
&& "[email protected]:,".equals(c.getComment())
&& 10 == c.getMaxAge());
}
@Test(expected = IllegalArgumentException.class)
public void testNullValue() throws Exception {
NewCookie.valueOf(null);
}
@Test
public void testFromSimpleString() {
NewCookie c = NewCookie.valueOf("foo=bar");
assertTrue("bar".equals(c.getValue())
&& "foo".equals(c.getName()));
}
@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()));
}
@Test
public void testNoValue() {
NewCookie c = NewCookie.valueOf("foo=");
assertTrue("".equals(c.getValue())
&& "foo".equals(c.getName()));
}