1111import org .apache .http .client .config .RequestConfig ;
1212import org .apache .http .client .methods .CloseableHttpResponse ;
1313import org .apache .http .client .methods .HttpGet ;
14- import org .apache .http .conn .ConnectTimeoutException ;
14+ import org .apache .http .config .SocketConfig ;
15+ import org .apache .http .conn .HttpHostConnectException ;
1516import org .apache .http .impl .client .CloseableHttpClient ;
16- import org .apache .http .impl .client .DefaultHttpClient ;
1717import org .apache .http .impl .client .HttpClientBuilder ;
18- import org .apache .http .params .CoreConnectionPNames ;
19- import org .apache .http .params .HttpConnectionParams ;
20- import org .apache .http .params .HttpParams ;
2118import org .junit .After ;
2219import org .junit .Test ;
2320
@@ -45,36 +42,31 @@ public final void after() throws IllegalStateException, IOException {
4542 // tests
4643
4744 @ Test
48- public final void givenUsingDeprecatedApi_whenSettingTimeoutViaRawParams_thenCorrect () throws ClientProtocolException , IOException {
45+ public final void givenUsingNewApi_whenSettingTimeoutViaRequestConfig_thenCorrect () throws ClientProtocolException , IOException {
4946 final int timeout = 2 ;
50- final DefaultHttpClient client = new DefaultHttpClient ();
47+ final RequestConfig config = RequestConfig .custom ().setConnectTimeout (timeout * 1000 ).setConnectionRequestTimeout (timeout * 1000 ).setSocketTimeout (timeout * 1000 ).build ();
48+ final CloseableHttpClient client = HttpClientBuilder .create ().setDefaultRequestConfig (config ).build ();
5149 final HttpGet request = new HttpGet ("http://www.github.com" );
5250
53- final HttpParams httpParams = client .getParams ();
54- httpParams .setParameter (CoreConnectionPNames .CONNECTION_TIMEOUT , timeout * 1000 );
55- httpParams .setParameter (CoreConnectionPNames .SO_TIMEOUT , timeout * 1000 );
5651 // httpParams.setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUT, new Long(timeout * 1000)); // https://issues.apache.org/jira/browse/HTTPCLIENT-1418
5752
5853 response = client .execute (request );
5954
6055 assertThat (response .getStatusLine ().getStatusCode (), equalTo (200 ));
61- client .close ();
6256 }
6357
6458 @ Test
65- public final void givenUsingDeprecatedApi_whenSettingTimeoutViaHigherLevelApi_thenCorrect () throws ClientProtocolException , IOException {
59+ public final void givenUsingNewApi_whenSettingTimeoutViaSocketConfig_thenCorrect () throws ClientProtocolException , IOException {
6660 final int timeout = 2 ;
67- final DefaultHttpClient client = new DefaultHttpClient ();
68- final HttpGet request = new HttpGet ("http://www.github.com" );
6961
70- final HttpParams httpParams = client .getParams ();
71- HttpConnectionParams .setConnectionTimeout (httpParams , timeout * 1000 ); // http.connection.timeout
72- HttpConnectionParams .setSoTimeout (httpParams , timeout * 1000 ); // http.socket.timeout
62+ final SocketConfig config = SocketConfig .custom ().setSoTimeout (timeout * 1000 ).build ();
63+ final CloseableHttpClient client = HttpClientBuilder .create ().setDefaultSocketConfig (config ).build ();
64+
65+ final HttpGet request = new HttpGet ("http://www.github.com" );
7366
7467 response = client .execute (request );
7568
7669 assertThat (response .getStatusLine ().getStatusCode (), equalTo (200 ));
77- client .close ();
7870 }
7971
8072 @ Test
@@ -94,7 +86,7 @@ public final void givenUsingNewApi_whenSettingTimeoutViaHighLevelApi_thenCorrect
9486 /**
9587 * This simulates a timeout against a domain with multiple routes/IPs to it (not a single raw IP)
9688 */
97- @ Test (expected = ConnectTimeoutException .class )
89+ @ Test (expected = HttpHostConnectException .class )
9890 public final void givenTimeoutIsConfigured_whenTimingOut_thenTimeoutException () throws ClientProtocolException , IOException {
9991 final int timeout = 3 ;
10092
0 commit comments