Search in sources :

Example 6 with ForestCookie

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);
    }
}
Also used : BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) ForestCookies(com.dtflys.forest.http.ForestCookies) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) ForestCookie(com.dtflys.forest.http.ForestCookie) Date(java.util.Date)

Example 7 with ForestCookie

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;
}
Also used : Cookie(org.apache.http.cookie.Cookie) ForestCookie(com.dtflys.forest.http.ForestCookie) ForestCookies(com.dtflys.forest.http.ForestCookies) ForestCookie(com.dtflys.forest.http.ForestCookie)

Example 8 with ForestCookie

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=/");
}
Also used : Duration(java.time.Duration) Date(java.util.Date) ForestCookie(com.dtflys.forest.http.ForestCookie) Test(org.junit.Test)

Example 9 with ForestCookie

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();
}
Also used : Duration(java.time.Duration) ForestCookie(com.dtflys.forest.http.ForestCookie) Test(org.junit.Test)

Example 10 with ForestCookie

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();
}
Also used : Duration(java.time.Duration) Date(java.util.Date) ForestCookie(com.dtflys.forest.http.ForestCookie) Test(org.junit.Test)

Aggregations

ForestCookie (com.dtflys.forest.http.ForestCookie)10 Duration (java.time.Duration)7 Test (org.junit.Test)7 Date (java.util.Date)6 ForestCookies (com.dtflys.forest.http.ForestCookies)3 Cookie (okhttp3.Cookie)2 HttpUrl (okhttp3.HttpUrl)2 OkHttpResponseBody (com.dtflys.forest.backend.okhttp3.response.OkHttpResponseBody)1 ForestProxy (com.dtflys.forest.http.ForestProxy)1 ForestRequest (com.dtflys.forest.http.ForestRequest)1 InetSocketAddress (java.net.InetSocketAddress)1 Proxy (java.net.Proxy)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Nullable (javax.annotation.Nullable)1 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)1 Authenticator (okhttp3.Authenticator)1 CookieJar (okhttp3.CookieJar)1 OkHttpClient (okhttp3.OkHttpClient)1