44import static org .junit .Assert .assertThat ;
55
66import java .io .IOException ;
7+ import java .util .Timer ;
8+ import java .util .TimerTask ;
79
10+ import org .apache .http .HttpResponse ;
811import org .apache .http .client .config .RequestConfig ;
912import org .apache .http .client .methods .CloseableHttpResponse ;
1013import org .apache .http .client .methods .HttpGet ;
14+ import org .apache .http .client .params .ClientPNames ;
1115import org .apache .http .config .SocketConfig ;
1216import org .apache .http .conn .ConnectTimeoutException ;
1317import org .apache .http .impl .client .CloseableHttpClient ;
18+ import org .apache .http .impl .client .DefaultHttpClient ;
1419import org .apache .http .impl .client .HttpClientBuilder ;
20+ import org .apache .http .params .CoreConnectionPNames ;
21+ import org .apache .http .params .HttpParams ;
1522import org .junit .After ;
1623import org .junit .Test ;
1724
@@ -25,6 +32,20 @@ public final void after() throws IllegalStateException, IOException {
2532 }
2633
2734 // tests
35+ @ Test
36+ public final void givenUsingOldApi_whenSettingTimeoutViaParameter_thenCorrect () throws IOException {
37+
38+ DefaultHttpClient httpClient = new DefaultHttpClient ();
39+ int timeout = 5 ; // seconds
40+ HttpParams httpParams = httpClient .getParams ();
41+ httpParams .setParameter (CoreConnectionPNames .CONNECTION_TIMEOUT , timeout * 1000 );
42+ httpParams .setParameter (CoreConnectionPNames .SO_TIMEOUT , timeout * 1000 );
43+ httpParams .setParameter (ClientPNames .CONN_MANAGER_TIMEOUT , new Long (timeout * 1000 ));
44+
45+ final HttpGet request = new HttpGet ("http://www.github.com" );
46+ HttpResponse execute = httpClient .execute (request );
47+ assertThat (execute .getStatusLine ().getStatusCode (), equalTo (200 ));
48+ }
2849
2950 @ Test
3051 public final void givenUsingNewApi_whenSettingTimeoutViaRequestConfig_thenCorrect () throws IOException {
@@ -33,8 +54,6 @@ public final void givenUsingNewApi_whenSettingTimeoutViaRequestConfig_thenCorrec
3354 final CloseableHttpClient client = HttpClientBuilder .create ().setDefaultRequestConfig (config ).build ();
3455 final HttpGet request = new HttpGet ("http://www.github.com" );
3556
36- // httpParams.setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUT, new Long(timeout * 1000)); // https://issues.apache.org/jira/browse/HTTPCLIENT-1418
37-
3857 response = client .execute (request );
3958
4059 assertThat (response .getStatusLine ().getStatusCode (), equalTo (200 ));
@@ -81,5 +100,28 @@ public final void givenTimeoutIsConfigured_whenTimingOut_thenTimeoutException()
81100 final HttpGet request = new HttpGet ("http://www.google.com:81" );
82101 client .execute (request );
83102 }
84-
103+
104+ @ Test
105+ public void whenSecuredRestApiIsConsumed_then200OK () throws IOException {
106+ CloseableHttpClient httpClient = HttpClientBuilder .create ().build ();
107+
108+ int timeout = 20 ; // seconds
109+ RequestConfig requestConfig = RequestConfig .custom ().setConnectionRequestTimeout (timeout * 1000 )
110+ .setConnectTimeout (timeout * 1000 ).setSocketTimeout (timeout * 1000 ).build ();
111+ HttpGet getMethod = new HttpGet ("http://localhost:8082/httpclient-simple/api/bars/1" );
112+ getMethod .setConfig (requestConfig );
113+
114+ int hardTimeout = 5 ; // seconds
115+ TimerTask task = new TimerTask () {
116+ @ Override
117+ public void run () {
118+ getMethod .abort ();
119+ }
120+ };
121+ new Timer (true ).schedule (task , hardTimeout * 1000 );
122+
123+ HttpResponse response = httpClient .execute (getMethod );
124+ System .out .println ("HTTP Status of response: " + response .getStatusLine ().getStatusCode ());
125+ }
126+
85127}
0 commit comments