use of com.dtflys.forest.http.ForestCookie in project forest by dromara.
the class HttpclientExecutor method prepareCookies.
public void prepareCookies(LifeCycleHandler lifeCycleHandler) {
cookieStore = new BasicCookieStore();
ForestCookies cookies = new ForestCookies();
lifeCycleHandler.handleLoadCookie(request, cookies);
for (ForestCookie cookie : cookies) {
BasicClientCookie httpCookie = new BasicClientCookie(cookie.getName(), cookie.getValue());
httpCookie.setDomain(cookie.getDomain());
httpCookie.setPath(cookie.getPath());
httpCookie.setSecure(cookie.isSecure());
httpCookie.setExpiryDate(new Date(cookie.getExpiresTime()));
cookieStore.addCookie(httpCookie);
}
}
use of com.dtflys.forest.http.ForestCookie in project forest by dromara.
the class AbstractHttpclientRequestSender method getCookiesFromHttpCookieStore.
protected ForestCookies getCookiesFromHttpCookieStore(CookieStore cookieStore) {
ForestCookies cookies = new ForestCookies();
for (Cookie httpCookie : cookieStore.getCookies()) {
ForestCookie cookie = ForestCookie.createFromHttpclientCookie(httpCookie);
cookies.addCookie(cookie);
}
return cookies;
}
use of com.dtflys.forest.http.ForestCookie in project forest by dromara.
the class CookieTest method testCookie.
@Test
public void testCookie() throws InterruptedException {
Date date = new Date();
Duration maxAge = Duration.ofMillis(1000L);
String domain = "localhost";
String path = "/";
boolean secure = true;
boolean httpOnly = false;
boolean hostOnly = true;
boolean persistent = false;
ForestCookie cookie = new ForestCookie("foo", "bar", date, maxAge, domain, path, secure, httpOnly, hostOnly, persistent);
long expiresTime = date.getTime() + maxAge.toMillis();
assertThat(cookie.getName()).isEqualTo("foo");
assertThat(cookie.getValue()).isEqualTo("bar");
assertThat(cookie.getCreateTime()).isEqualTo(date);
assertThat(cookie.getMaxAge()).isEqualTo(maxAge);
assertThat(cookie.getPath()).isEqualTo(path);
assertThat(cookie.isSecure()).isEqualTo(true);
assertThat(cookie.isHttpOnly()).isEqualTo(false);
assertThat(cookie.isHostOnly()).isEqualTo(true);
assertThat(cookie.isPersistent()).isEqualTo(false);
assertThat(cookie.getExpiresTime()).isEqualTo(expiresTime);
assertThat(cookie.isExpired(new Date())).isFalse();
assertThat(cookie.toString()).isEqualTo("foo=bar; path=/; secure");
Thread.sleep(1000L);
assertThat(cookie.isExpired(new Date())).isTrue();
maxAge = Duration.ofMillis(0L);
secure = false;
httpOnly = true;
hostOnly = false;
persistent = true;
cookie = new ForestCookie("foo", "bar", date, maxAge, domain, path, secure, httpOnly, hostOnly, persistent);
assertThat(cookie.toString()).isEqualTo("foo=bar; max-age=0; domain=localhost; path=/; httponly");
maxAge = Duration.ofMillis(1000L);
secure = false;
httpOnly = false;
hostOnly = false;
persistent = true;
cookie = new ForestCookie("foo", "bar", date, maxAge, domain, path, secure, httpOnly, hostOnly, persistent);
assertThat(cookie.toString()).isEqualTo("foo=bar; max-age=1; domain=localhost; path=/");
}
use of com.dtflys.forest.http.ForestCookie in project forest by dromara.
the class CookieTest method testParseCookie.
@Test
public void testParseCookie() {
Duration maxAge = Duration.ofSeconds(1L);
String url = "http://forest.dtflyx.com/docs";
String setCookie = "foo=bar; max-age=" + maxAge.getSeconds();
ForestCookie cookie = ForestCookie.parse(url, setCookie);
assertThat(cookie).isNotNull();
assertThat(cookie.getName()).isEqualTo("foo");
assertThat(cookie.getValue()).isEqualTo("bar");
assertThat(cookie.getMaxAge().getSeconds()).isEqualTo(maxAge.getSeconds());
long expiresTime = cookie.getCreateTime().getTime() + cookie.getMaxAge().toMillis();
assertThat(cookie.getExpiresTime()).isEqualTo(expiresTime);
assertThat(cookie.getDomain()).isEqualTo("forest.dtflyx.com");
assertThat(cookie.getPath()).isEqualTo("/");
assertThat(cookie.isPersistent()).isTrue();
assertThat(cookie.isSecure()).isFalse();
assertThat(cookie.isHostOnly()).isTrue();
assertThat(cookie.isHttpOnly()).isFalse();
setCookie = "foo=bar; max-age=" + maxAge.getSeconds() + "; domain=dtflyx.com; secure";
cookie = ForestCookie.parse(url, setCookie);
assertThat(cookie).isNotNull();
assertThat(cookie.getName()).isEqualTo("foo");
assertThat(cookie.getValue()).isEqualTo("bar");
assertThat(cookie.getMaxAge().getSeconds()).isEqualTo(maxAge.getSeconds());
expiresTime = cookie.getCreateTime().getTime() + cookie.getMaxAge().toMillis();
assertThat(cookie.getExpiresTime()).isEqualTo(expiresTime);
assertThat(cookie.getDomain()).isEqualTo("dtflyx.com");
assertThat(cookie.getPath()).isEqualTo("/");
assertThat(cookie.isPersistent()).isTrue();
assertThat(cookie.isSecure()).isTrue();
assertThat(cookie.isHostOnly()).isFalse();
assertThat(cookie.isHttpOnly()).isFalse();
}
use of com.dtflys.forest.http.ForestCookie in project forest by dromara.
the class CookieTest method testMatchPath.
@Test
public void testMatchPath() {
Date date = new Date();
Duration maxAge = Duration.ofMillis(1000L);
String domain = "localhost";
String path = "/";
boolean secure = true;
boolean httpOnly = false;
boolean hostOnly = true;
boolean persistent = false;
ForestCookie cookie = new ForestCookie("foo", "bar", date, maxAge, domain, path, secure, httpOnly, hostOnly, persistent);
assertThat(cookie.matchPath("/")).isTrue();
assertThat(cookie.matchPath("/foo")).isTrue();
assertThat(cookie.matchPath("/foo/bar")).isTrue();
path = "/foo";
cookie = new ForestCookie("foo", "bar", date, maxAge, domain, path, secure, httpOnly, hostOnly, persistent);
assertThat(cookie.matchPath("/")).isFalse();
assertThat(cookie.matchPath("/foo")).isTrue();
assertThat(cookie.matchPath("/foo/bar")).isTrue();
}
Aggregations