cleanup and further http client examples
This commit is contained in:
		
							parent
							
								
									e597d29ca3
								
							
						
					
					
						commit
						ccb9c4a046
					
				| @ -8,7 +8,6 @@ import static org.junit.Assert.assertThat; | |||||||
| 
 | 
 | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
| import java.io.InputStream; | import java.io.InputStream; | ||||||
| import java.net.SocketTimeoutException; |  | ||||||
| 
 | 
 | ||||||
| import org.apache.http.Header; | import org.apache.http.Header; | ||||||
| import org.apache.http.HttpEntity; | import org.apache.http.HttpEntity; | ||||||
| @ -18,8 +17,10 @@ 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.client.methods.HttpPost; | import org.apache.http.client.methods.HttpPost; | ||||||
|  | import org.apache.http.conn.ConnectTimeoutException; | ||||||
| import org.apache.http.entity.ContentType; | import org.apache.http.entity.ContentType; | ||||||
| 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.HttpClientBuilder; | import org.apache.http.impl.client.HttpClientBuilder; | ||||||
| import org.apache.http.impl.conn.BasicHttpClientConnectionManager; | import org.apache.http.impl.conn.BasicHttpClientConnectionManager; | ||||||
| import org.apache.http.util.EntityUtils; | import org.apache.http.util.EntityUtils; | ||||||
| @ -29,7 +30,7 @@ import org.junit.Test; | |||||||
| 
 | 
 | ||||||
| public class HttpClientLiveTest { | public class HttpClientLiveTest { | ||||||
| 
 | 
 | ||||||
|     private static final String SAMPLE_URL = "http://www.google.com"; |     private static final String SAMPLE_URL = "http://www.github.com"; | ||||||
| 
 | 
 | ||||||
|     private CloseableHttpClient instance; |     private CloseableHttpClient instance; | ||||||
| 
 | 
 | ||||||
| @ -88,9 +89,9 @@ public class HttpClientLiveTest { | |||||||
|         assertThat(bodyAsString, notNullValue()); |         assertThat(bodyAsString, notNullValue()); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Test(expected = SocketTimeoutException.class) |     @Test(expected = ConnectTimeoutException.class) | ||||||
|     public final void givenLowTimeout_whenExecutingRequestWithTimeout_thenException() throws ClientProtocolException, IOException { |     public final void givenLowTimeout_whenExecutingRequestWithTimeout_thenException() throws ClientProtocolException, IOException { | ||||||
|         final RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(50).setConnectTimeout(50).setSocketTimeout(50).build(); |         final RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(50).setConnectTimeout(50).setSocketTimeout(20).build(); | ||||||
|         final HttpGet request = new HttpGet(SAMPLE_URL); |         final HttpGet request = new HttpGet(SAMPLE_URL); | ||||||
|         request.setConfig(requestConfig); |         request.setConfig(requestConfig); | ||||||
|         response = instance.execute(request); |         response = instance.execute(request); | ||||||
| @ -111,13 +112,6 @@ public class HttpClientLiveTest { | |||||||
|         response = instance.execute(new HttpGet(SAMPLE_URL)); |         response = instance.execute(new HttpGet(SAMPLE_URL)); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Test |  | ||||||
|     public final void givenRedirectsAreDisabled_whenConsumingUrlWhichRedirects_thenNotRedirected() throws ClientProtocolException, IOException { |  | ||||||
|         instance = HttpClientBuilder.create().disableRedirectHandling().build(); |  | ||||||
|         response = instance.execute(new HttpGet("http://t.co/I5YYd9tddw")); |  | ||||||
|         assertThat(response.getStatusLine().getStatusCode(), equalTo(301)); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     @Test |     @Test | ||||||
|     public final void givenCustomHeaderIsSet_whenSendingRequest_thenNoExceptions() throws ClientProtocolException, IOException { |     public final void givenCustomHeaderIsSet_whenSendingRequest_thenNoExceptions() throws ClientProtocolException, IOException { | ||||||
|         final HttpGet request = new HttpGet(SAMPLE_URL); |         final HttpGet request = new HttpGet(SAMPLE_URL); | ||||||
| @ -133,4 +127,18 @@ public class HttpClientLiveTest { | |||||||
|         assertThat(headers, not(emptyArray())); |         assertThat(headers, not(emptyArray())); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     // tests - headers | ||||||
|  | 
 | ||||||
|  |     @SuppressWarnings("deprecation") | ||||||
|  |     @Test | ||||||
|  |     public final void givenDeprecatedApi_whenRequestHasCustomUserAgent_thenCorrect() throws ClientProtocolException, IOException { | ||||||
|  |         instance = new DefaultHttpClient(); | ||||||
|  |         final HttpGet request = new HttpGet(SAMPLE_URL); | ||||||
|  |         request.setHeader(HttpHeaders.USER_AGENT, "Mozilla/5.0 Firefox/26.0"); | ||||||
|  | 
 | ||||||
|  |         response = instance.execute(request); | ||||||
|  | 
 | ||||||
|  |         System.out.println(response); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
| } | } | ||||||
|  | |||||||
| @ -0,0 +1,130 @@ | |||||||
|  | package org.baeldung.httpclient; | ||||||
|  | 
 | ||||||
|  | import static org.hamcrest.Matchers.equalTo; | ||||||
|  | import static org.junit.Assert.assertThat; | ||||||
|  | 
 | ||||||
|  | import java.io.IOException; | ||||||
|  | import java.io.InputStream; | ||||||
|  | 
 | ||||||
|  | import org.apache.http.HttpEntity; | ||||||
|  | import org.apache.http.client.ClientProtocolException; | ||||||
|  | import org.apache.http.client.methods.CloseableHttpResponse; | ||||||
|  | import org.apache.http.client.methods.HttpGet; | ||||||
|  | import org.apache.http.client.methods.HttpHead; | ||||||
|  | import org.apache.http.client.methods.HttpPost; | ||||||
|  | import org.apache.http.client.params.ClientPNames; | ||||||
|  | import org.apache.http.client.params.HttpClientParams; | ||||||
|  | import org.apache.http.impl.client.CloseableHttpClient; | ||||||
|  | import org.apache.http.impl.client.DefaultHttpClient; | ||||||
|  | import org.apache.http.impl.client.DefaultRedirectStrategy; | ||||||
|  | import org.apache.http.impl.client.HttpClientBuilder; | ||||||
|  | import org.apache.http.impl.client.LaxRedirectStrategy; | ||||||
|  | import org.apache.http.params.BasicHttpParams; | ||||||
|  | import org.apache.http.params.HttpParams; | ||||||
|  | import org.junit.After; | ||||||
|  | import org.junit.Before; | ||||||
|  | import org.junit.Test; | ||||||
|  | 
 | ||||||
|  | public class HttpClientRedirectLiveTest { | ||||||
|  | 
 | ||||||
|  |     private CloseableHttpClient instance; | ||||||
|  | 
 | ||||||
|  |     private CloseableHttpResponse response; | ||||||
|  | 
 | ||||||
|  |     @Before | ||||||
|  |     public final void before() { | ||||||
|  |         instance = HttpClientBuilder.create().build(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @After | ||||||
|  |     public final void after() throws IllegalStateException, IOException { | ||||||
|  |         if (response == null) { | ||||||
|  |             return; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         try { | ||||||
|  |             final HttpEntity entity = response.getEntity(); | ||||||
|  |             if (entity != null) { | ||||||
|  |                 final InputStream instream = entity.getContent(); | ||||||
|  |                 instream.close(); | ||||||
|  |             } | ||||||
|  |         } finally { | ||||||
|  |             response.close(); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     // tests | ||||||
|  | 
 | ||||||
|  |     @SuppressWarnings("deprecation") | ||||||
|  |     @Test | ||||||
|  |     public final void givenRedirectsAreDisabledViaDeprecatedApi_whenConsumingUrlWhichRedirects_thenNotRedirected() throws ClientProtocolException, IOException { | ||||||
|  |         instance = new DefaultHttpClient(); | ||||||
|  | 
 | ||||||
|  |         final HttpParams params = new BasicHttpParams(); | ||||||
|  |         params.setParameter(ClientPNames.HANDLE_REDIRECTS, false); | ||||||
|  |         HttpClientParams.setRedirecting(params, false); | ||||||
|  | 
 | ||||||
|  |         final HttpGet httpGet = new HttpGet("http://t.co/I5YYd9tddw"); | ||||||
|  |         httpGet.setParams(params); | ||||||
|  |         response = instance.execute(httpGet); | ||||||
|  | 
 | ||||||
|  |         assertThat(response.getStatusLine().getStatusCode(), equalTo(301)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public final void givenRedirectsAreDisabled_whenConsumingUrlWhichRedirects_thenNotRedirected() throws ClientProtocolException, IOException { | ||||||
|  |         instance = HttpClientBuilder.create().disableRedirectHandling().build(); | ||||||
|  |         response = instance.execute(new HttpGet("http://t.co/I5YYd9tddw")); | ||||||
|  |         assertThat(response.getStatusLine().getStatusCode(), equalTo(301)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     // redirect with POST | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public final void givenPostRequest_whenConsumingUrlWhichRedirects_thenNotRedirected() throws ClientProtocolException, IOException { | ||||||
|  |         instance = HttpClientBuilder.create().build(); | ||||||
|  |         response = instance.execute(new HttpPost("http://t.co/I5YYd9tddw")); | ||||||
|  |         assertThat(response.getStatusLine().getStatusCode(), equalTo(301)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @SuppressWarnings("deprecation") | ||||||
|  |     @Test | ||||||
|  |     public final void givenRedirectingPOSTViaPre4_2Api_whenConsumingUrlWhichRedirectsWithPOST_thenRedirected() throws ClientProtocolException, IOException { | ||||||
|  |         final DefaultHttpClient client = new DefaultHttpClient(); | ||||||
|  |         client.setRedirectStrategy(new DefaultRedirectStrategy() { | ||||||
|  |             /** Redirectable methods. */ | ||||||
|  |             private final String[] REDIRECT_METHODS = new String[] { HttpGet.METHOD_NAME, HttpPost.METHOD_NAME, HttpHead.METHOD_NAME }; | ||||||
|  | 
 | ||||||
|  |             @Override | ||||||
|  |             protected boolean isRedirectable(final String method) { | ||||||
|  |                 for (final String m : REDIRECT_METHODS) { | ||||||
|  |                     if (m.equalsIgnoreCase(method)) { | ||||||
|  |                         return true; | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|  |                 return false; | ||||||
|  |             } | ||||||
|  |         }); | ||||||
|  | 
 | ||||||
|  |         response = client.execute(new HttpPost("http://t.co/I5YYd9tddw")); | ||||||
|  |         assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @SuppressWarnings("deprecation") | ||||||
|  |     @Test | ||||||
|  |     public final void givenRedirectingPOSTVia4_2Api_whenConsumingUrlWhichRedirectsWithPOST_thenRedirected() throws ClientProtocolException, IOException { | ||||||
|  |         final DefaultHttpClient client = new DefaultHttpClient(); | ||||||
|  |         client.setRedirectStrategy(new LaxRedirectStrategy()); | ||||||
|  | 
 | ||||||
|  |         response = client.execute(new HttpPost("http://t.co/I5YYd9tddw")); | ||||||
|  |         assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public final void givenRedirectingPOST_whenConsumingUrlWhichRedirectsWithPOST_thenRedirected() throws ClientProtocolException, IOException { | ||||||
|  |         instance = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build(); | ||||||
|  |         response = instance.execute(new HttpPost("http://t.co/I5YYd9tddw")); | ||||||
|  |         assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user