Skip to content

Commit df21ebc

Browse files
authored
fix: correct connect timeout setting for ApacheHttpRequest (#803)
* fix: correct connect timeout setting for ApacheHttpRequest * test: add timeout test * test: deflake test by increasing test timeout * test: try to deflake connect timeout test * test: try a higher timeout * test: skip timeout test on windows
1 parent 5862e7d commit df21ebc

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void addHeader(String name, String value) {
5252

5353
@Override
5454
public void setTimeout(int connectTimeout, int readTimeout) throws IOException {
55-
requestConfig.setConnectionRequestTimeout(connectTimeout)
55+
requestConfig.setConnectTimeout(connectTimeout)
5656
.setSocketTimeout(readTimeout);
5757
}
5858

google-http-client-apache-v2/src/test/java/com/google/api/client/http/apache/v2/ApacheHttpTransportTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
import static org.junit.Assert.assertNotNull;
1919
import static org.junit.Assert.assertTrue;
2020
import static org.junit.Assert.fail;
21+
import static org.junit.Assume.assumeTrue;
2122
import static org.mockito.Matchers.any;
2223
import static org.mockito.Mockito.mock;
2324
import static org.mockito.Mockito.when;
2425

2526
import com.google.api.client.http.GenericUrl;
27+
import com.google.api.client.http.HttpTransport;
2628
import com.google.api.client.http.LowLevelHttpResponse;
2729
import com.google.api.client.util.ByteArrayStreamingContent;
2830
import com.sun.net.httpserver.HttpExchange;
@@ -44,6 +46,8 @@
4446
import org.apache.http.HttpVersion;
4547
import org.apache.http.client.HttpClient;
4648
import org.apache.http.client.methods.HttpUriRequest;
49+
import org.apache.http.conn.ConnectTimeoutException;
50+
import org.apache.http.conn.HttpHostConnectException;
4751
import org.apache.http.impl.client.HttpClients;
4852
import org.apache.http.message.BasicHttpResponse;
4953
import org.apache.http.protocol.HttpContext;
@@ -183,6 +187,23 @@ public void process(HttpRequest request, HttpContext context)
183187
assertTrue("Expected to have called our test interceptor", interceptorCalled.get());
184188
}
185189

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+
186207
@Test
187208
public void testNormalizedUrl() throws IOException {
188209
HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
@@ -211,4 +232,8 @@ public void handle(HttpExchange httpExchange) throws IOException {
211232
assertEquals(200, response.getStatusCode());
212233
assertEquals("/foo//bar", response.parseAsString());
213234
}
235+
236+
private boolean isWindows() {
237+
return System.getProperty("os.name").startsWith("Windows");
238+
}
214239
}

0 commit comments

Comments
 (0)