use of com.dtflys.forest.http.ForestCookie in project forest by dromara.
the class TestCookieClient method testCookieWithCallback.
@Test
public void testCookieWithCallback() {
server.enqueue(new MockResponse().setResponseCode(200).setBody(EXPECTED).setHeader(HttpHeaders.ACCEPT, "text/plain").setHeader("Set-Cookie", "cookie_foo=cookie_bar"));
AtomicReference<ForestCookie> cookieAtomic = new AtomicReference<>(null);
cookieClient.testLoginWithCallback((request, cookies) -> cookieAtomic.set(cookies.allCookies().get(0)));
ForestCookie cookie = cookieAtomic.get();
assertThat(cookie).isNotNull().extracting(ForestCookie::getDomain, ForestCookie::getPath, ForestCookie::getName, ForestCookie::getValue).contains("localhost", "/", "cookie_foo", "cookie_bar");
mockRequest(server).assertMethodEquals("POST").assertPathEquals("/login");
server.enqueue(new MockResponse().setResponseCode(200).setBody(EXPECTED).setHeader(HttpHeaders.ACCEPT, "text/plain"));
assertThat(cookieClient.testCookieWithCallback((request, cookies) -> {
cookies.addCookie(cookie);
})).isNotNull().extracting(ForestResponse::getStatusCode, ForestResponse::getResult).contains(200, EXPECTED);
mockRequest(server).assertMethodEquals("POST").assertPathEquals("/test").assertHeaderEquals("Cookie", "cookie_foo=cookie_bar");
}
use of com.dtflys.forest.http.ForestCookie in project forest by dromara.
the class CookieTest method testCreateCookieFromOkHttp.
@Test
public void testCreateCookieFromOkHttp() {
Duration maxAge = Duration.ofSeconds(0L);
String url = "http://forest.dtflyx.com/docs";
String setCookie = "foo=bar; max-age=" + maxAge.getSeconds();
HttpUrl httpUrl = HttpUrl.parse(url);
long currentTime = System.currentTimeMillis();
Cookie okCookie = Cookie.parse(httpUrl, setCookie);
ForestCookie cookie = ForestCookie.createFromOkHttpCookie(currentTime, okCookie);
assertThat(cookie).isNotNull();
assertThat(cookie.getName()).isEqualTo("foo");
assertThat(cookie.getValue()).isEqualTo("bar");
assertThat(cookie.getMaxAge().getSeconds()).isEqualTo(0);
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();
}
use of com.dtflys.forest.http.ForestCookie in project forest by dromara.
the class CookieTest method testCreateCookieFromHttpclient.
@Test
public void testCreateCookieFromHttpclient() {
Duration maxAge = Duration.ofSeconds(0L);
long currentTime = System.currentTimeMillis();
long expiresTime = currentTime + maxAge.toMillis();
BasicClientCookie2 httpCookie = new BasicClientCookie2("foo", "bar");
httpCookie.setDomain("forest.dtflyx.com");
httpCookie.setExpiryDate(new Date(expiresTime));
httpCookie.setPath("/");
httpCookie.setSecure(false);
httpCookie.setDiscard(true);
ForestCookie cookie = ForestCookie.createFromHttpclientCookie(httpCookie);
assertThat(cookie).isNotNull();
assertThat(cookie.getName()).isEqualTo("foo");
assertThat(cookie.getValue()).isEqualTo("bar");
assertThat(cookie.getMaxAge().getSeconds()).isEqualTo(0);
assertThat(cookie.getDomain()).isEqualTo("forest.dtflyx.com");
assertThat(cookie.getPath()).isEqualTo("/");
assertThat(cookie.isPersistent()).isFalse();
assertThat(cookie.isSecure()).isFalse();
}
use of com.dtflys.forest.http.ForestCookie in project forest by dromara.
the class CookieTest method testMatchDomain.
@Test
public void testMatchDomain() {
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.matchDomain("localhost")).isTrue();
assertThat(cookie.matchDomain("baidu.com")).isFalse();
domain = "forest.dtflyx.com";
cookie = new ForestCookie("foo", "bar", date, maxAge, domain, path, secure, httpOnly, hostOnly, persistent);
assertThat(cookie.matchDomain("forest.dtflyx.com")).isTrue();
assertThat(cookie.matchDomain("dtflyx.com")).isTrue();
}
use of com.dtflys.forest.http.ForestCookie in project forest by dromara.
the class OkHttp3ConnectionManager method getClient.
public OkHttpClient getClient(ForestRequest request, LifeCycleHandler lifeCycleHandler) {
Integer timeout = request.getTimeout();
Integer connectTimeout = request.connectTimeout();
Integer readTimeout = request.readTimeout();
if (TimeUtils.isNone(connectTimeout)) {
connectTimeout = timeout;
}
if (TimeUtils.isNone(readTimeout)) {
readTimeout = timeout;
}
OkHttpClient.Builder builder = new OkHttpClient.Builder().connectionPool(pool).dispatcher(dispatcher).connectTimeout(connectTimeout, TimeUnit.MILLISECONDS).readTimeout(readTimeout, TimeUnit.MILLISECONDS).protocols(getProtocols(request)).followRedirects(false).followSslRedirects(false).cookieJar(new CookieJar() {
@Override
public void saveFromResponse(HttpUrl url, List<Cookie> okCookies) {
ForestCookies cookies = new ForestCookies();
for (Cookie okCookie : okCookies) {
long currentTime = System.currentTimeMillis();
ForestCookie cookie = ForestCookie.createFromOkHttpCookie(currentTime, okCookie);
cookies.addCookie(cookie);
}
lifeCycleHandler.handleSaveCookie(request, cookies);
}
@Override
public List<Cookie> loadForRequest(HttpUrl url) {
ForestCookies cookies = new ForestCookies();
lifeCycleHandler.handleLoadCookie(request, cookies);
List<ForestCookie> forestCookies = cookies.allCookies();
List<Cookie> okCookies = new ArrayList<>(forestCookies.size());
for (ForestCookie cookie : forestCookies) {
Duration maxAge = cookie.getMaxAge();
Date createTime = cookie.getCreateTime();
long expiresAt = createTime.getTime() + maxAge.toMillis();
Cookie.Builder cookieBuilder = new Cookie.Builder();
cookieBuilder.name(cookie.getName()).value(cookie.getValue()).expiresAt(expiresAt).path(cookie.getPath());
if (cookie.isHostOnly()) {
cookieBuilder.hostOnlyDomain(cookie.getDomain());
} else {
cookieBuilder.domain(cookie.getDomain());
}
if (cookie.isHttpOnly()) {
cookieBuilder.httpOnly();
}
if (cookie.isSecure()) {
cookieBuilder.secure();
}
Cookie okCookie = cookieBuilder.build();
okCookies.add(okCookie);
}
return okCookies;
}
});
// set proxy
ForestProxy proxy = request.getProxy();
if (proxy != null) {
Proxy okProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy.getHost(), proxy.getPort()));
builder.proxy(okProxy);
if (StringUtils.isNotEmpty(proxy.getUsername())) {
builder.proxyAuthenticator(new Authenticator() {
@Nullable
@Override
public Request authenticate(@Nullable Route route, Response response) {
Request.Builder proxyBuilder = response.request().newBuilder();
String credential = Credentials.basic(proxy.getUsername(), proxy.getPassword());
proxyBuilder.addHeader("Proxy-Authorization", credential);
return proxyBuilder.build();
}
});
}
}
if (request.isSSL()) {
SSLSocketFactory sslSocketFactory = request.getSSLSocketFactory();
builder.sslSocketFactory(sslSocketFactory, getX509TrustManager(request)).hostnameVerifier(request.hostnameVerifier());
}
// add default interceptor
builder.addNetworkInterceptor(chain -> {
Response response = chain.proceed(chain.request());
return response.newBuilder().body(new OkHttpResponseBody(request, response.body(), lifeCycleHandler)).build();
});
return builder.build();
}
Aggregations