|
18 | 18 | import static org.junit.Assert.assertNotNull;
|
19 | 19 | import static org.junit.Assert.assertTrue;
|
20 | 20 | import static org.junit.Assert.fail;
|
| 21 | +import static org.junit.Assume.assumeTrue; |
21 | 22 | import static org.mockito.Matchers.any;
|
22 | 23 | import static org.mockito.Mockito.mock;
|
23 | 24 | import static org.mockito.Mockito.when;
|
24 | 25 |
|
25 | 26 | import com.google.api.client.http.GenericUrl;
|
| 27 | +import com.google.api.client.http.HttpTransport; |
26 | 28 | import com.google.api.client.http.LowLevelHttpResponse;
|
27 | 29 | import com.google.api.client.util.ByteArrayStreamingContent;
|
28 | 30 | import com.sun.net.httpserver.HttpExchange;
|
|
44 | 46 | import org.apache.http.HttpVersion;
|
45 | 47 | import org.apache.http.client.HttpClient;
|
46 | 48 | import org.apache.http.client.methods.HttpUriRequest;
|
| 49 | +import org.apache.http.conn.ConnectTimeoutException; |
| 50 | +import org.apache.http.conn.HttpHostConnectException; |
47 | 51 | import org.apache.http.impl.client.HttpClients;
|
48 | 52 | import org.apache.http.message.BasicHttpResponse;
|
49 | 53 | import org.apache.http.protocol.HttpContext;
|
@@ -183,6 +187,23 @@ public void process(HttpRequest request, HttpContext context)
|
183 | 187 | assertTrue("Expected to have called our test interceptor", interceptorCalled.get());
|
184 | 188 | }
|
185 | 189 |
|
| 190 | + @Test(timeout = 10_000L) |
| 191 | + public void testConnectTimeout() { |
| 192 | + // Apache HttpClient doesn't appear to behave correctly on windows |
| 193 | + assumeTrue(!isWindows()); |
| 194 | + |
| 195 | + HttpTransport httpTransport = new ApacheHttpTransport(); |
| 196 | + GenericUrl url = new GenericUrl("http://google.com:81"); |
| 197 | + try { |
| 198 | + httpTransport.createRequestFactory().buildGetRequest(url).setConnectTimeout(100).execute(); |
| 199 | + fail("should have thrown an exception"); |
| 200 | + } catch (HttpHostConnectException | ConnectTimeoutException expected) { |
| 201 | + // expected |
| 202 | + } catch (IOException e) { |
| 203 | + fail("unexpected IOException: " + e.getClass().getName()); |
| 204 | + } |
| 205 | + } |
| 206 | + |
186 | 207 | @Test
|
187 | 208 | public void testNormalizedUrl() throws IOException {
|
188 | 209 | HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
|
@@ -211,4 +232,8 @@ public void handle(HttpExchange httpExchange) throws IOException {
|
211 | 232 | assertEquals(200, response.getStatusCode());
|
212 | 233 | assertEquals("/foo//bar", response.parseAsString());
|
213 | 234 | }
|
| 235 | + |
| 236 | + private boolean isWindows() { |
| 237 | + return System.getProperty("os.name").startsWith("Windows"); |
| 238 | + } |
214 | 239 | }
|
0 commit comments