[JAVA-15016] Upgraded to apache http client 5.2 (#13269)
* [JAVA-15016] Upgraded to apache http client 5.2 * [JAVA-15016] Clean up * [JAVA-15016] Junit 5 clean up Co-authored-by: panagiotiskakos <panagiotis.kakos@libra-is.com>
This commit is contained in:
parent
dcc35801e7
commit
0a76db37d6
@ -1,36 +1,41 @@
|
|||||||
package com.baeldung.httpclient.cookies;
|
package com.baeldung.httpclient.cookies;
|
||||||
|
|
||||||
import org.apache.http.client.CookieStore;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.hc.client5.http.classic.methods.HttpGet;
|
||||||
import org.apache.http.client.protocol.HttpClientContext;
|
import org.apache.hc.client5.http.cookie.BasicCookieStore;
|
||||||
import org.apache.http.cookie.ClientCookie;
|
import org.apache.hc.client5.http.cookie.Cookie;
|
||||||
import org.apache.http.cookie.Cookie;
|
import org.apache.hc.client5.http.cookie.CookieStore;
|
||||||
import org.apache.http.impl.client.BasicCookieStore;
|
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.hc.client5.http.impl.classic.BasicHttpClientResponseHandler;
|
||||||
import org.apache.http.impl.client.HttpClients;
|
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||||
import org.apache.http.impl.cookie.BasicClientCookie;
|
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
|
||||||
|
|
||||||
|
import org.apache.hc.client5.http.impl.cookie.BasicClientCookie;
|
||||||
|
import org.apache.hc.client5.http.protocol.HttpClientContext;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
class HttpClientGettingCookieValueUnitTest {
|
||||||
|
|
||||||
|
|
||||||
public class HttpClientGettingCookieValueUnitTest {
|
|
||||||
private static Logger log = LoggerFactory.getLogger(HttpClientGettingCookieValueUnitTest.class);
|
private static Logger log = LoggerFactory.getLogger(HttpClientGettingCookieValueUnitTest.class);
|
||||||
|
|
||||||
private static final String SAMPLE_URL = "http://www.baeldung.com/";
|
private static final String SAMPLE_URL = "http://www.baeldung.com/";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenSettingCustomCookieOnTheRequest_thenGettingTheSameCookieFromTheResponse() throws IOException {
|
void whenSettingCustomCookieOnTheRequest_thenGettingTheSameCookieFromTheResponse() throws IOException {
|
||||||
|
|
||||||
HttpClientContext context = HttpClientContext.create();
|
HttpClientContext context = HttpClientContext.create();
|
||||||
context.setAttribute(HttpClientContext.COOKIE_STORE, createCustomCookieStore());
|
context.setAttribute(HttpClientContext.COOKIE_STORE, createCustomCookieStore());
|
||||||
|
|
||||||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
final HttpGet request = new HttpGet(SAMPLE_URL);
|
||||||
try (CloseableHttpResponse response = httpClient.execute(new HttpGet(SAMPLE_URL), context)) {
|
|
||||||
|
try (CloseableHttpClient client = HttpClientBuilder.create()
|
||||||
|
.build()) {
|
||||||
|
client.execute(request, context, new BasicHttpClientResponseHandler());
|
||||||
CookieStore cookieStore = context.getCookieStore();
|
CookieStore cookieStore = context.getCookieStore();
|
||||||
Cookie customCookie = cookieStore.getCookies()
|
Cookie customCookie = cookieStore.getCookies()
|
||||||
.stream()
|
.stream()
|
||||||
@ -42,13 +47,12 @@ public class HttpClientGettingCookieValueUnitTest {
|
|||||||
assertEquals("test_value", customCookie.getValue());
|
assertEquals("test_value", customCookie.getValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private BasicCookieStore createCustomCookieStore() {
|
private BasicCookieStore createCustomCookieStore() {
|
||||||
BasicCookieStore cookieStore = new BasicCookieStore();
|
BasicCookieStore cookieStore = new BasicCookieStore();
|
||||||
BasicClientCookie cookie = new BasicClientCookie("custom_cookie", "test_value");
|
BasicClientCookie cookie = new BasicClientCookie("custom_cookie", "test_value");
|
||||||
cookie.setDomain("baeldung.com");
|
cookie.setDomain("baeldung.com");
|
||||||
cookie.setAttribute(ClientCookie.DOMAIN_ATTR, "true");
|
cookie.setAttribute("domain", "true");
|
||||||
cookie.setPath("/");
|
cookie.setPath("/");
|
||||||
cookieStore.addCookie(cookie);
|
cookieStore.addCookie(cookie);
|
||||||
return cookieStore;
|
return cookieStore;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user