small fixes and formatting work
This commit is contained in:
parent
1c79503594
commit
84d3367a37
|
@ -11,6 +11,7 @@ import org.apache.http.client.ClientProtocolException;
|
||||||
import org.apache.http.client.config.RequestConfig;
|
import org.apache.http.client.config.RequestConfig;
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
|
import org.apache.http.conn.ConnectTimeoutException;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.impl.client.DefaultHttpClient;
|
import org.apache.http.impl.client.DefaultHttpClient;
|
||||||
import org.apache.http.impl.client.HttpClientBuilder;
|
import org.apache.http.impl.client.HttpClientBuilder;
|
||||||
|
@ -93,17 +94,15 @@ public class HttpClientTimeoutLiveTest {
|
||||||
/**
|
/**
|
||||||
* This simulates a timeout against a domain with multiple routes/IPs to it (not a single raw IP)
|
* This simulates a timeout against a domain with multiple routes/IPs to it (not a single raw IP)
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test(expected = ConnectTimeoutException.class)
|
||||||
public final void givenTimeoutIsConfigured_whenTimingOut_thenCorrect() throws ClientProtocolException, IOException {
|
public final void givenTimeoutIsConfigured_whenTimingOut_thenTimeoutException() throws ClientProtocolException, IOException {
|
||||||
final int timeout = 3;
|
final int timeout = 3;
|
||||||
|
|
||||||
final RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout * 1000).setConnectionRequestTimeout(timeout * 1000).setSocketTimeout(timeout * 1000).build();
|
final RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout * 1000).setConnectionRequestTimeout(timeout * 1000).setSocketTimeout(timeout * 1000).build();
|
||||||
final CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
|
final CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
|
||||||
|
|
||||||
final HttpGet request = new HttpGet("http://www.google.com:81");
|
final HttpGet request = new HttpGet("http://www.google.com:81");
|
||||||
response = client.execute(request);
|
client.execute(request);
|
||||||
|
|
||||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,85 +35,77 @@ import org.junit.Test;
|
||||||
* */
|
* */
|
||||||
public class HttpsClientSslLiveTest {
|
public class HttpsClientSslLiveTest {
|
||||||
|
|
||||||
// "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1" // local
|
// "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1" // local
|
||||||
// "https://mms.nw.ru/" // hosted
|
// "https://mms.nw.ru/" // hosted
|
||||||
private static final String HOST_WITH_SSL = "https://mms.nw.ru/";
|
private static final String HOST_WITH_SSL = "https://mms.nw.ru/";
|
||||||
|
|
||||||
// tests
|
// tests
|
||||||
|
|
||||||
@Test(expected = SSLException.class)
|
@Test(expected = SSLException.class)
|
||||||
public final void whenHttpsUrlIsConsumed_thenException() throws ClientProtocolException, IOException {
|
public final void whenHttpsUrlIsConsumed_thenException() throws ClientProtocolException, IOException {
|
||||||
final CloseableHttpClient httpClient = HttpClientBuilder.create().build();
|
final CloseableHttpClient httpClient = HttpClientBuilder.create().build();
|
||||||
|
|
||||||
final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
|
final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
|
||||||
final HttpResponse response = httpClient.execute(getMethod);
|
final HttpResponse response = httpClient.execute(getMethod);
|
||||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
@Test
|
@Test
|
||||||
public final void givenHttpClientPre4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException {
|
public final void givenHttpClientPre4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException {
|
||||||
final TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
|
final TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
|
||||||
@Override
|
@Override
|
||||||
public final boolean isTrusted(final X509Certificate[] certificate, final String authType) {
|
public final boolean isTrusted(final X509Certificate[] certificate, final String authType) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
final SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
|
final SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
|
||||||
final SchemeRegistry registry = new SchemeRegistry();
|
final SchemeRegistry registry = new SchemeRegistry();
|
||||||
registry.register(new Scheme("https", 443, sf));
|
registry.register(new Scheme("https", 443, sf));
|
||||||
final ClientConnectionManager ccm = new PoolingClientConnectionManager(registry);
|
final ClientConnectionManager ccm = new PoolingClientConnectionManager(registry);
|
||||||
|
|
||||||
final CloseableHttpClient httpClient = new DefaultHttpClient(ccm);
|
final CloseableHttpClient httpClient = new DefaultHttpClient(ccm);
|
||||||
|
|
||||||
final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
|
final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
|
||||||
final HttpResponse response = httpClient.execute(getMethod);
|
final HttpResponse response = httpClient.execute(getMethod);
|
||||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||||
|
|
||||||
httpClient.close();
|
httpClient.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenHttpClientAfter4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException {
|
public final void givenHttpClientAfter4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException {
|
||||||
final TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
|
final TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
|
||||||
@Override
|
@Override
|
||||||
public final boolean isTrusted(final X509Certificate[] certificate, final String authType) {
|
public final boolean isTrusted(final X509Certificate[] certificate, final String authType) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
final SSLContext sslContext = SSLContexts.custom()
|
final SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
|
||||||
.loadTrustMaterial(null, acceptingTrustStrategy).build();
|
|
||||||
|
|
||||||
final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
|
final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
|
||||||
sslContext,
|
|
||||||
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
|
|
||||||
|
|
||||||
final CloseableHttpClient httpClient = HttpClients
|
final CloseableHttpClient httpClient = HttpClients.custom().setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER).setSSLSocketFactory(sslsf).build();
|
||||||
.custom()
|
|
||||||
.setHostnameVerifier(
|
|
||||||
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)
|
|
||||||
.setSSLSocketFactory(sslsf).build();
|
|
||||||
|
|
||||||
|
final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
|
||||||
|
final HttpResponse response = httpClient.execute(getMethod);
|
||||||
|
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||||
|
|
||||||
final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
|
httpClient.close();
|
||||||
final HttpResponse response = httpClient.execute(getMethod);
|
}
|
||||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
|
||||||
|
|
||||||
httpClient.close();
|
@Test
|
||||||
}
|
public final void givenHttpClientPost4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException {
|
||||||
|
final SSLContextBuilder builder = new SSLContextBuilder();
|
||||||
|
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
|
||||||
|
final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
|
||||||
|
final CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
|
||||||
|
|
||||||
@Test
|
// new
|
||||||
public final void givenHttpClientPost4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException {
|
|
||||||
final SSLContextBuilder builder = new SSLContextBuilder();
|
|
||||||
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
|
|
||||||
final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
|
|
||||||
final CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
|
|
||||||
|
|
||||||
// new
|
final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
|
||||||
|
final HttpResponse response = httpClient.execute(getMethod);
|
||||||
final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
|
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||||
final HttpResponse response = httpClient.execute(getMethod);
|
}
|
||||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,132 +25,132 @@ import com.google.common.collect.Lists;
|
||||||
|
|
||||||
public class HttpClientUnshortenLiveTest {
|
public class HttpClientUnshortenLiveTest {
|
||||||
|
|
||||||
private CloseableHttpClient client;
|
private CloseableHttpClient client;
|
||||||
|
|
||||||
// fixtures
|
// fixtures
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public final void before() {
|
public final void before() {
|
||||||
client = HttpClientBuilder.create().disableRedirectHandling().build();
|
client = HttpClientBuilder.create().disableRedirectHandling().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
// tests
|
// tests
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenShortenedOnce_whenUrlIsUnshortened_thenCorrectResult() throws IOException {
|
public final void givenShortenedOnce_whenUrlIsUnshortened_thenCorrectResult() throws IOException {
|
||||||
final String expectedResult = "http://www.baeldung.com/rest-versioning";
|
final String expectedResult = "http://www.baeldung.com/rest-versioning";
|
||||||
final String actualResult = expandSingleLevel("http://bit.ly/13jEoS1");
|
final String actualResult = expandSingleLevel("http://bit.ly/13jEoS1");
|
||||||
assertThat(actualResult, equalTo(expectedResult));
|
assertThat(actualResult, equalTo(expectedResult));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenShortenedMultiple_whenUrlIsUnshortened_thenCorrectResult() throws IOException {
|
public final void givenShortenedMultiple_whenUrlIsUnshortened_thenCorrectResult() throws IOException {
|
||||||
final String expectedResult = "http://www.baeldung.com/rest-versioning";
|
final String expectedResult = "http://www.baeldung.com/rest-versioning";
|
||||||
final String actualResult = expand("http://t.co/e4rDDbnzmk");
|
final String actualResult = expand("http://t.co/e4rDDbnzmk");
|
||||||
assertThat(actualResult, equalTo(expectedResult));
|
assertThat(actualResult, equalTo(expectedResult));
|
||||||
}
|
}
|
||||||
|
|
||||||
// API
|
// API
|
||||||
|
|
||||||
final String expand(final String urlArg) throws IOException {
|
final String expand(final String urlArg) throws IOException {
|
||||||
String originalUrl = urlArg;
|
String originalUrl = urlArg;
|
||||||
String newUrl = expandSingleLevel(originalUrl);
|
String newUrl = expandSingleLevel(originalUrl);
|
||||||
while (!originalUrl.equals(newUrl)) {
|
while (!originalUrl.equals(newUrl)) {
|
||||||
originalUrl = newUrl;
|
originalUrl = newUrl;
|
||||||
newUrl = expandSingleLevel(originalUrl);
|
newUrl = expandSingleLevel(originalUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
return newUrl;
|
return newUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
final String expandSafe(final String urlArg) throws IOException {
|
final String expandSafe(final String urlArg) throws IOException {
|
||||||
String originalUrl = urlArg;
|
String originalUrl = urlArg;
|
||||||
String newUrl = expandSingleLevelSafe(originalUrl).getRight();
|
String newUrl = expandSingleLevelSafe(originalUrl).getRight();
|
||||||
final List<String> alreadyVisited = Lists.newArrayList(originalUrl, newUrl);
|
final List<String> alreadyVisited = Lists.newArrayList(originalUrl, newUrl);
|
||||||
while (!originalUrl.equals(newUrl)) {
|
while (!originalUrl.equals(newUrl)) {
|
||||||
originalUrl = newUrl;
|
originalUrl = newUrl;
|
||||||
final Pair<Integer, String> statusAndUrl = expandSingleLevelSafe(originalUrl);
|
final Pair<Integer, String> statusAndUrl = expandSingleLevelSafe(originalUrl);
|
||||||
newUrl = statusAndUrl.getRight();
|
newUrl = statusAndUrl.getRight();
|
||||||
final boolean isRedirect = statusAndUrl.getLeft() == 301 || statusAndUrl.getLeft() == 302;
|
final boolean isRedirect = statusAndUrl.getLeft() == 301 || statusAndUrl.getLeft() == 302;
|
||||||
if (isRedirect && alreadyVisited.contains(newUrl)) {
|
if (isRedirect && alreadyVisited.contains(newUrl)) {
|
||||||
throw new IllegalStateException("Likely a redirect loop");
|
throw new IllegalStateException("Likely a redirect loop");
|
||||||
}
|
}
|
||||||
alreadyVisited.add(newUrl);
|
alreadyVisited.add(newUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
return newUrl;
|
return newUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
final Pair<Integer, String> expandSingleLevelSafe(final String url) throws IOException {
|
final Pair<Integer, String> expandSingleLevelSafe(final String url) throws IOException {
|
||||||
HttpGet request = null;
|
HttpGet request = null;
|
||||||
HttpEntity httpEntity = null;
|
HttpEntity httpEntity = null;
|
||||||
InputStream entityContentStream = null;
|
InputStream entityContentStream = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
request = new HttpGet(url);
|
request = new HttpGet(url);
|
||||||
final HttpResponse httpResponse = client.execute(request);
|
final HttpResponse httpResponse = client.execute(request);
|
||||||
|
|
||||||
httpEntity = httpResponse.getEntity();
|
httpEntity = httpResponse.getEntity();
|
||||||
entityContentStream = httpEntity.getContent();
|
entityContentStream = httpEntity.getContent();
|
||||||
|
|
||||||
final int statusCode = httpResponse.getStatusLine().getStatusCode();
|
final int statusCode = httpResponse.getStatusLine().getStatusCode();
|
||||||
if (statusCode != 301 && statusCode != 302) {
|
if (statusCode != 301 && statusCode != 302) {
|
||||||
return new ImmutablePair<Integer, String>(statusCode, url);
|
return new ImmutablePair<Integer, String>(statusCode, url);
|
||||||
}
|
}
|
||||||
final Header[] headers = httpResponse.getHeaders(HttpHeaders.LOCATION);
|
final Header[] headers = httpResponse.getHeaders(HttpHeaders.LOCATION);
|
||||||
Preconditions.checkState(headers.length == 1);
|
Preconditions.checkState(headers.length == 1);
|
||||||
final String newUrl = headers[0].getValue();
|
final String newUrl = headers[0].getValue();
|
||||||
|
|
||||||
return new ImmutablePair<Integer, String>(statusCode, newUrl);
|
return new ImmutablePair<Integer, String>(statusCode, newUrl);
|
||||||
} catch (final IllegalArgumentException uriEx) {
|
} catch (final IllegalArgumentException uriEx) {
|
||||||
return new ImmutablePair<Integer, String>(500, url);
|
return new ImmutablePair<Integer, String>(500, url);
|
||||||
} finally {
|
} finally {
|
||||||
if (request != null) {
|
if (request != null) {
|
||||||
request.releaseConnection();
|
request.releaseConnection();
|
||||||
}
|
}
|
||||||
if (entityContentStream != null) {
|
if (entityContentStream != null) {
|
||||||
entityContentStream.close();
|
entityContentStream.close();
|
||||||
}
|
}
|
||||||
if (httpEntity != null) {
|
if (httpEntity != null) {
|
||||||
EntityUtils.consume(httpEntity);
|
EntityUtils.consume(httpEntity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final String expandSingleLevel(final String url) throws IOException {
|
final String expandSingleLevel(final String url) throws IOException {
|
||||||
HttpGet request = null;
|
HttpGet request = null;
|
||||||
HttpEntity httpEntity = null;
|
HttpEntity httpEntity = null;
|
||||||
InputStream entityContentStream = null;
|
InputStream entityContentStream = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
request = new HttpGet(url);
|
request = new HttpGet(url);
|
||||||
final HttpResponse httpResponse = client.execute(request);
|
final HttpResponse httpResponse = client.execute(request);
|
||||||
|
|
||||||
httpEntity = httpResponse.getEntity();
|
httpEntity = httpResponse.getEntity();
|
||||||
entityContentStream = httpEntity.getContent();
|
entityContentStream = httpEntity.getContent();
|
||||||
|
|
||||||
final int statusCode = httpResponse.getStatusLine().getStatusCode();
|
final int statusCode = httpResponse.getStatusLine().getStatusCode();
|
||||||
if (statusCode != 301 && statusCode != 302) {
|
if (statusCode != 301 && statusCode != 302) {
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
final Header[] headers = httpResponse.getHeaders(HttpHeaders.LOCATION);
|
final Header[] headers = httpResponse.getHeaders(HttpHeaders.LOCATION);
|
||||||
Preconditions.checkState(headers.length == 1);
|
Preconditions.checkState(headers.length == 1);
|
||||||
final String newUrl = headers[0].getValue();
|
final String newUrl = headers[0].getValue();
|
||||||
|
|
||||||
return newUrl;
|
return newUrl;
|
||||||
} catch (final IllegalArgumentException uriEx) {
|
} catch (final IllegalArgumentException uriEx) {
|
||||||
return url;
|
return url;
|
||||||
} finally {
|
} finally {
|
||||||
if (request != null) {
|
if (request != null) {
|
||||||
request.releaseConnection();
|
request.releaseConnection();
|
||||||
}
|
}
|
||||||
if (entityContentStream != null) {
|
if (entityContentStream != null) {
|
||||||
entityContentStream.close();
|
entityContentStream.close();
|
||||||
}
|
}
|
||||||
if (httpEntity != null) {
|
if (httpEntity != null) {
|
||||||
EntityUtils.consume(httpEntity);
|
EntityUtils.consume(httpEntity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,95 +24,94 @@ import org.junit.Test;
|
||||||
|
|
||||||
public class HttpClientCookieLiveTest {
|
public class HttpClientCookieLiveTest {
|
||||||
|
|
||||||
private CloseableHttpClient instance;
|
private CloseableHttpClient instance;
|
||||||
|
|
||||||
private CloseableHttpResponse response;
|
private CloseableHttpResponse response;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public final void before() {
|
public final void before() {
|
||||||
instance = HttpClientBuilder.create().build();
|
instance = HttpClientBuilder.create().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public final void after() throws IllegalStateException, IOException {
|
public final void after() throws IllegalStateException, IOException {
|
||||||
if (response == null) {
|
if (response == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final HttpEntity entity = response.getEntity();
|
final HttpEntity entity = response.getEntity();
|
||||||
if (entity != null) {
|
if (entity != null) {
|
||||||
final InputStream instream = entity.getContent();
|
final InputStream instream = entity.getContent();
|
||||||
instream.close();
|
instream.close();
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
response.close();
|
response.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// tests
|
// tests
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenSettingCookiesOnARequest_thenCorrect() throws ClientProtocolException, IOException {
|
public final void whenSettingCookiesOnARequest_thenCorrect() throws ClientProtocolException, IOException {
|
||||||
instance = HttpClientBuilder.create().build();
|
instance = HttpClientBuilder.create().build();
|
||||||
final HttpGet request = new HttpGet("http://www.github.com");
|
final HttpGet request = new HttpGet("http://www.github.com");
|
||||||
request.setHeader("Cookie", "JSESSIONID=1234");
|
request.setHeader("Cookie", "JSESSIONID=1234");
|
||||||
|
|
||||||
response = instance.execute(request);
|
response = instance.execute(request);
|
||||||
|
|
||||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenUsingDeprecatedApi_whenSettingCookiesOnTheHttpClient_thenCorrect() throws ClientProtocolException, IOException {
|
public final void givenUsingDeprecatedApi_whenSettingCookiesOnTheHttpClient_thenCorrect() throws ClientProtocolException, IOException {
|
||||||
final BasicCookieStore cookieStore = new BasicCookieStore();
|
final BasicCookieStore cookieStore = new BasicCookieStore();
|
||||||
final BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
|
final BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
|
||||||
cookie.setDomain(".github.com");
|
cookie.setDomain(".github.com");
|
||||||
cookie.setPath("/");
|
cookie.setPath("/");
|
||||||
cookieStore.addCookie(cookie);
|
cookieStore.addCookie(cookie);
|
||||||
final HttpClient client = HttpClientBuilder.create()
|
final HttpClient client = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();
|
||||||
.setDefaultCookieStore(cookieStore).build();
|
|
||||||
|
|
||||||
final HttpGet request = new HttpGet("http://www.github.com");
|
final HttpGet request = new HttpGet("http://www.github.com");
|
||||||
|
|
||||||
response = (CloseableHttpResponse) client.execute(request);
|
response = (CloseableHttpResponse) client.execute(request);
|
||||||
|
|
||||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenSettingCookiesOnTheHttpClient_thenCookieSentCorrectly() throws ClientProtocolException, IOException {
|
public final void whenSettingCookiesOnTheHttpClient_thenCookieSentCorrectly() throws ClientProtocolException, IOException {
|
||||||
final BasicCookieStore cookieStore = new BasicCookieStore();
|
final BasicCookieStore cookieStore = new BasicCookieStore();
|
||||||
final BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
|
final BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
|
||||||
cookie.setDomain(".github.com");
|
cookie.setDomain(".github.com");
|
||||||
cookie.setPath("/");
|
cookie.setPath("/");
|
||||||
cookieStore.addCookie(cookie);
|
cookieStore.addCookie(cookie);
|
||||||
instance = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();
|
instance = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();
|
||||||
|
|
||||||
final HttpGet request = new HttpGet("http://www.github.com");
|
final HttpGet request = new HttpGet("http://www.github.com");
|
||||||
|
|
||||||
response = instance.execute(request);
|
response = instance.execute(request);
|
||||||
|
|
||||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenSettingCookiesOnTheRequest_thenCookieSentCorrectly() throws ClientProtocolException, IOException {
|
public final void whenSettingCookiesOnTheRequest_thenCookieSentCorrectly() throws ClientProtocolException, IOException {
|
||||||
final BasicCookieStore cookieStore = new BasicCookieStore();
|
final BasicCookieStore cookieStore = new BasicCookieStore();
|
||||||
final BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
|
final BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
|
||||||
cookie.setDomain(".github.com");
|
cookie.setDomain(".github.com");
|
||||||
cookie.setPath("/");
|
cookie.setPath("/");
|
||||||
cookieStore.addCookie(cookie);
|
cookieStore.addCookie(cookie);
|
||||||
instance = HttpClientBuilder.create().build();
|
instance = HttpClientBuilder.create().build();
|
||||||
|
|
||||||
final HttpGet request = new HttpGet("http://www.github.com");
|
final HttpGet request = new HttpGet("http://www.github.com");
|
||||||
|
|
||||||
final HttpContext localContext = new BasicHttpContext();
|
final HttpContext localContext = new BasicHttpContext();
|
||||||
localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
|
localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
|
||||||
// localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); // before 4.3
|
// localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); // before 4.3
|
||||||
response = instance.execute(request, localContext);
|
response = instance.execute(request, localContext);
|
||||||
|
|
||||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue