Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correct connect timeout setting for ApacheHttpRequest #803

Merged
merged 7 commits into from Sep 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -52,7 +52,7 @@ public void addHeader(String name, String value) {

@Override
public void setTimeout(int connectTimeout, int readTimeout) throws IOException {
requestConfig.setConnectionRequestTimeout(connectTimeout)
requestConfig.setConnectTimeout(connectTimeout)
.setSocketTimeout(readTimeout);
}

Expand Down
Expand Up @@ -18,11 +18,13 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.LowLevelHttpResponse;
import com.google.api.client.util.ByteArrayStreamingContent;
import com.sun.net.httpserver.HttpExchange;
Expand All @@ -44,6 +46,8 @@
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.HttpHostConnectException;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.protocol.HttpContext;
Expand Down Expand Up @@ -183,6 +187,23 @@ public void process(HttpRequest request, HttpContext context)
assertTrue("Expected to have called our test interceptor", interceptorCalled.get());
}

@Test(timeout = 10_000L)
public void testConnectTimeout() {
// Apache HttpClient doesn't appear to behave correctly on windows
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a bug filed against Apache we could reference here?

assumeTrue(!isWindows());

HttpTransport httpTransport = new ApacheHttpTransport();
GenericUrl url = new GenericUrl("http://google.com:81");
try {
httpTransport.createRequestFactory().buildGetRequest(url).setConnectTimeout(100).execute();
fail("should have thrown an exception");
} catch (HttpHostConnectException | ConnectTimeoutException expected) {
// expected
} catch (IOException e) {
fail("unexpected IOException: " + e.getClass().getName());
}
}

@Test
public void testNormalizedUrl() throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
Expand Down Expand Up @@ -211,4 +232,8 @@ public void handle(HttpExchange httpExchange) throws IOException {
assertEquals(200, response.getStatusCode());
assertEquals("/foo//bar", response.parseAsString());
}

private boolean isWindows() {
return System.getProperty("os.name").startsWith("Windows");
}
}