|
1 | 1 | package com.baeldung.httpclient; |
2 | 2 |
|
| 3 | +import static org.hamcrest.MatcherAssert.assertThat; |
3 | 4 | import static org.hamcrest.Matchers.equalTo; |
4 | | -import static org.junit.Assert.assertThat; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 6 | +import static org.mockito.Mockito.when; |
| 7 | + |
| 8 | +import org.junit.jupiter.api.Disabled; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
5 | 11 |
|
6 | 12 | import java.io.IOException; |
7 | 13 | import java.util.Timer; |
8 | 14 | import java.util.TimerTask; |
| 15 | +import java.util.concurrent.TimeUnit; |
9 | 16 |
|
10 | | -import org.apache.http.HttpResponse; |
11 | | -import org.apache.http.client.config.RequestConfig; |
12 | | -import org.apache.http.client.methods.CloseableHttpResponse; |
13 | | -import org.apache.http.client.methods.HttpGet; |
14 | | -import org.apache.http.client.params.ClientPNames; |
15 | | -import org.apache.http.config.SocketConfig; |
16 | | -import org.apache.http.conn.ConnectTimeoutException; |
17 | | -import org.apache.http.impl.client.CloseableHttpClient; |
18 | | -import org.apache.http.impl.client.DefaultHttpClient; |
19 | | -import org.apache.http.impl.client.HttpClientBuilder; |
20 | | -import org.apache.http.params.CoreConnectionPNames; |
21 | | -import org.apache.http.params.HttpParams; |
22 | | -import org.junit.After; |
23 | | -import org.junit.Ignore; |
24 | | -import org.junit.Test; |
25 | | - |
26 | | -public class HttpClientTimeoutLiveTest { |
27 | | - |
28 | | - private CloseableHttpResponse response; |
29 | | - |
30 | | - @After |
31 | | - public final void after() throws IllegalStateException, IOException { |
32 | | - ResponseUtil.closeResponse(response); |
33 | | - } |
| 17 | +import org.apache.hc.client5.http.ConnectTimeoutException; |
| 18 | +import org.apache.hc.client5.http.classic.methods.HttpGet; |
| 19 | +import org.apache.hc.client5.http.config.ConnectionConfig; |
| 20 | +import org.apache.hc.client5.http.config.RequestConfig; |
| 21 | +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; |
| 22 | +import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; |
| 23 | +import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; |
34 | 24 |
|
35 | | - // tests |
36 | | - @Test |
37 | | - public final void givenUsingOldApi_whenSettingTimeoutViaParameter_thenCorrect() throws IOException { |
38 | | - |
39 | | - DefaultHttpClient httpClient = new DefaultHttpClient(); |
40 | | - int timeout = 5; // seconds |
41 | | - HttpParams httpParams = httpClient.getParams(); |
42 | | - httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout * 1000); |
43 | | - httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout * 1000); |
44 | | - httpParams.setParameter(ClientPNames.CONN_MANAGER_TIMEOUT, new Long(timeout * 1000)); |
45 | | - |
46 | | - final HttpGet request = new HttpGet("http://www.github.com"); |
47 | | - HttpResponse execute = httpClient.execute(request); |
48 | | - assertThat(execute.getStatusLine().getStatusCode(), equalTo(200)); |
49 | | - } |
| 25 | +import org.apache.hc.client5.http.impl.io.BasicHttpClientConnectionManager; |
50 | 26 |
|
51 | | - @Test |
52 | | - public final void givenUsingNewApi_whenSettingTimeoutViaRequestConfig_thenCorrect() throws IOException { |
53 | | - final int timeout = 2; |
54 | | - final RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout * 1000).setConnectionRequestTimeout(timeout * 1000).setSocketTimeout(timeout * 1000).build(); |
55 | | - final CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).build(); |
56 | | - final HttpGet request = new HttpGet("http://www.github.com"); |
| 27 | +import org.apache.hc.core5.http.HttpStatus; |
| 28 | +import org.apache.hc.core5.http.io.SocketConfig; |
| 29 | +import org.apache.hc.core5.util.Timeout; |
57 | 30 |
|
58 | | - response = client.execute(request); |
59 | 31 |
|
60 | | - assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); |
61 | | - } |
| 32 | +import com.baeldung.handler.CustomHttpClientResponseHandler; |
| 33 | + |
| 34 | +class HttpClientTimeoutLiveTest { |
62 | 35 |
|
63 | 36 | @Test |
64 | | - public final void givenUsingNewApi_whenSettingTimeoutViaSocketConfig_thenCorrect() throws IOException { |
| 37 | + void givenUsingNewApi_whenSettingTimeoutViaHighLevelApi_thenCorrect() throws IOException { |
65 | 38 | final int timeout = 2; |
66 | 39 |
|
67 | | - final SocketConfig config = SocketConfig.custom().setSoTimeout(timeout * 1000).build(); |
68 | | - final CloseableHttpClient client = HttpClientBuilder.create().setDefaultSocketConfig(config).build(); |
| 40 | + ConnectionConfig connConfig = ConnectionConfig.custom() |
| 41 | + .setConnectTimeout(timeout, TimeUnit.MILLISECONDS) |
| 42 | + .setSocketTimeout(timeout, TimeUnit.MILLISECONDS) |
| 43 | + .build(); |
| 44 | + |
| 45 | + RequestConfig requestConfig = RequestConfig.custom() |
| 46 | + .setConnectionRequestTimeout(Timeout.ofMilliseconds(2000L)) |
| 47 | + .build(); |
| 48 | + |
| 49 | + BasicHttpClientConnectionManager cm = new BasicHttpClientConnectionManager(); |
| 50 | + cm.setConnectionConfig(connConfig); |
| 51 | + |
69 | 52 |
|
70 | 53 | final HttpGet request = new HttpGet("http://www.github.com"); |
71 | 54 |
|
72 | | - response = client.execute(request); |
| 55 | + try (CloseableHttpClient client = HttpClientBuilder.create() |
| 56 | + .setDefaultRequestConfig(requestConfig) |
| 57 | + .setConnectionManager(cm) |
| 58 | + .build(); |
| 59 | + |
| 60 | + CloseableHttpResponse response = (CloseableHttpResponse) client |
| 61 | + .execute(request, new CustomHttpClientResponseHandler())) { |
73 | 62 |
|
74 | | - assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); |
| 63 | + final int statusCode = response.getCode(); |
| 64 | + assertThat(statusCode, equalTo(HttpStatus.SC_OK)); |
| 65 | + } |
75 | 66 | } |
76 | 67 |
|
77 | 68 | @Test |
78 | | - public final void givenUsingNewApi_whenSettingTimeoutViaHighLevelApi_thenCorrect() throws IOException { |
79 | | - final int timeout = 5; |
| 69 | + void givenUsingNewApi_whenSettingTimeoutViaSocketConfig_thenCorrect() throws IOException { |
| 70 | + final int timeout = 2000; |
| 71 | + final SocketConfig config = SocketConfig.custom().setSoTimeout(timeout, TimeUnit.MILLISECONDS).build(); |
80 | 72 |
|
81 | | - final RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout * 1000).setConnectionRequestTimeout(timeout * 1000).setSocketTimeout(timeout * 1000).build(); |
82 | | - final CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).build(); |
| 73 | + BasicHttpClientConnectionManager cm = new BasicHttpClientConnectionManager(); |
| 74 | + cm.setSocketConfig(config); |
83 | 75 |
|
84 | 76 | final HttpGet request = new HttpGet("http://www.github.com"); |
85 | 77 |
|
86 | | - response = client.execute(request); |
| 78 | + try (CloseableHttpClient client = HttpClientBuilder.create() |
| 79 | + .setConnectionManager(cm) |
| 80 | + .build(); |
87 | 81 |
|
88 | | - assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); |
| 82 | + CloseableHttpResponse response = (CloseableHttpResponse) client |
| 83 | + .execute(request, new CustomHttpClientResponseHandler())) { |
| 84 | + |
| 85 | + final int statusCode = response.getCode(); |
| 86 | + assertThat(statusCode, equalTo(HttpStatus.SC_OK)); |
| 87 | + } |
89 | 88 | } |
90 | 89 |
|
| 90 | + |
91 | 91 | /** |
92 | 92 | * This simulates a timeout against a domain with multiple routes/IPs to it (not a single raw IP) |
93 | 93 | */ |
94 | | - @Test(expected = ConnectTimeoutException.class) |
95 | | - @Ignore |
96 | | - public final void givenTimeoutIsConfigured_whenTimingOut_thenTimeoutException() throws IOException { |
| 94 | + @Disabled |
| 95 | + void givenTimeoutIsConfigured_whenTimingOut_thenTimeoutException() throws IOException { |
97 | 96 | final int timeout = 3; |
98 | 97 |
|
99 | | - final RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout * 1000).setConnectionRequestTimeout(timeout * 1000).setSocketTimeout(timeout * 1000).build(); |
100 | | - final CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).build(); |
| 98 | + ConnectionConfig connConfig = ConnectionConfig.custom() |
| 99 | + .setConnectTimeout(timeout, TimeUnit.MILLISECONDS) |
| 100 | + .setSocketTimeout(timeout, TimeUnit.MILLISECONDS) |
| 101 | + .build(); |
| 102 | + |
| 103 | + RequestConfig requestConfig = RequestConfig.custom() |
| 104 | + .setConnectionRequestTimeout(Timeout.ofMilliseconds(3000L)) |
| 105 | + .build(); |
| 106 | + |
| 107 | + BasicHttpClientConnectionManager cm = new BasicHttpClientConnectionManager(); |
| 108 | + cm.setConnectionConfig(connConfig); |
101 | 109 |
|
102 | 110 | final HttpGet request = new HttpGet("http://www.google.com:81"); |
103 | | - client.execute(request); |
| 111 | + |
| 112 | + assertThrows(ConnectTimeoutException.class, () -> { |
| 113 | + try (CloseableHttpClient client = HttpClientBuilder.create() |
| 114 | + .setDefaultRequestConfig(requestConfig) |
| 115 | + .setConnectionManager(cm) |
| 116 | + .build(); |
| 117 | + |
| 118 | + CloseableHttpResponse response = (CloseableHttpResponse) client |
| 119 | + .execute(request, new CustomHttpClientResponseHandler())) { |
| 120 | + |
| 121 | + final int statusCode = response.getCode(); |
| 122 | + assertThat(statusCode, equalTo(HttpStatus.SC_OK)); |
| 123 | + } |
| 124 | + }); |
104 | 125 | } |
105 | 126 |
|
106 | 127 | @Test |
107 | | - public void whenSecuredRestApiIsConsumed_then200OK() throws IOException { |
108 | | - CloseableHttpClient httpClient = HttpClientBuilder.create().build(); |
| 128 | + void whenSecuredRestApiIsConsumed_then200OK() throws IOException { |
| 129 | + int timeout = 20000; // milliseconds |
| 130 | + |
| 131 | + ConnectionConfig connConfig = ConnectionConfig.custom() |
| 132 | + .setConnectTimeout(timeout, TimeUnit.MILLISECONDS) |
| 133 | + .setSocketTimeout(timeout, TimeUnit.MILLISECONDS) |
| 134 | + .build(); |
| 135 | + |
| 136 | + RequestConfig requestConfig = RequestConfig.custom() |
| 137 | + .setConnectionRequestTimeout(Timeout.ofMilliseconds(20000L)) |
| 138 | + .build(); |
109 | 139 |
|
110 | | - int timeout = 20; // seconds |
111 | | - RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeout * 1000) |
112 | | - .setConnectTimeout(timeout * 1000).setSocketTimeout(timeout * 1000).build(); |
113 | 140 | HttpGet getMethod = new HttpGet("http://localhost:8082/httpclient-simple/api/bars/1"); |
114 | 141 | getMethod.setConfig(requestConfig); |
115 | 142 |
|
116 | | - int hardTimeout = 5; // seconds |
| 143 | + |
| 144 | + BasicHttpClientConnectionManager cm = new BasicHttpClientConnectionManager(); |
| 145 | + cm.setConnectionConfig(connConfig); |
| 146 | + |
| 147 | + int hardTimeout = 5000; // milliseconds |
117 | 148 | TimerTask task = new TimerTask() { |
118 | 149 | @Override |
119 | 150 | public void run() { |
120 | 151 | getMethod.abort(); |
121 | 152 | } |
122 | 153 | }; |
123 | | - new Timer(true).schedule(task, hardTimeout * 1000); |
| 154 | + new Timer(true).schedule(task, hardTimeout); |
| 155 | + |
| 156 | + try (CloseableHttpClient client = HttpClientBuilder.create() |
| 157 | + .setDefaultRequestConfig(requestConfig) |
| 158 | + .setConnectionManager(cm) |
| 159 | + .build(); |
| 160 | + |
| 161 | + CloseableHttpResponse response = (CloseableHttpResponse) client |
| 162 | + .execute(getMethod, new CustomHttpClientResponseHandler())) { |
| 163 | + |
| 164 | + final int statusCode = response.getCode(); |
| 165 | + System.out.println("HTTP Status of response: " + statusCode); |
| 166 | + assertThat(statusCode, equalTo(HttpStatus.SC_OK)); |
| 167 | + } |
124 | 168 |
|
125 | | - HttpResponse response = httpClient.execute(getMethod); |
126 | | - System.out.println("HTTP Status of response: " + response.getStatusLine().getStatusCode()); |
127 | 169 | } |
128 | 170 |
|
129 | 171 | } |
0 commit comments