From fce97ec60766ab96d7747360237f9a22da974eab Mon Sep 17 00:00:00 2001 From: Diego Alonso Marquez Palacios Date: Wed, 11 Sep 2024 10:49:43 -0400 Subject: [PATCH 1/3] chore(apache-v5): confirm behavior of connect timeout --- .../apache/v5/Apache5HttpTransportIT.java | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpTransportIT.java diff --git a/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpTransportIT.java b/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpTransportIT.java new file mode 100644 index 000000000..b66b38a89 --- /dev/null +++ b/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpTransportIT.java @@ -0,0 +1,67 @@ +/* + * Copyright 2024 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.api.client.http.apache.v5; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import com.google.api.client.http.GenericUrl; +import com.google.api.client.http.HttpTransport; +import com.google.common.collect.ImmutableMap; +import java.io.IOException; +import org.apache.hc.client5.http.ConnectTimeoutException; +import org.junit.Test; + +public class Apache5HttpTransportIT { + private static final ImmutableMap payload = + ImmutableMap.of("foo", "bar"); + + // Sets a 5 second delay before response + private static final String NO_CONNECT_URL = "https://google.com:81"; + + @Test(timeout = 10_000L) + public void testConnectTimeoutGet() throws IOException { + HttpTransport transport = new Apache5HttpTransport(); + try { + transport + .createRequestFactory() + .buildGetRequest(new GenericUrl(NO_CONNECT_URL)) + .setConnectTimeout(100) + .execute(); + fail("No exception thrown for HTTP error response"); + } catch (ConnectTimeoutException e) { + assertTrue( + "Expected exception message to contain a connection timeout message", + e.getMessage().contains("Connect timed out")); + } + } + + @Test(timeout = 10_000L) + public void testConnectTimeoutPost() throws IOException { + Apache5HttpTransport transport = new Apache5HttpTransport(); + Apache5HttpRequest request = transport.buildRequest("POST", NO_CONNECT_URL); + request.setTimeout(100, 0); + try { + request.execute(); + fail("No exception thrown for HTTP error response"); + } catch (ConnectTimeoutException e) { + assertTrue( + "Expected exception message to contain a connection timeout message", + e.getMessage().contains("Connect timed out")); + } + } +} From bd7b5065327bf68399793e78ad8565242eca99ef Mon Sep 17 00:00:00 2001 From: Diego Alonso Marquez Palacios Date: Wed, 11 Sep 2024 13:34:48 -0400 Subject: [PATCH 2/3] move tests to old test file --- .../apache/v5/Apache5HttpTransportIT.java | 67 ------------------- .../apache/v5/Apache5HttpTransportTest.java | 33 +++++++++ 2 files changed, 33 insertions(+), 67 deletions(-) delete mode 100644 google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpTransportIT.java diff --git a/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpTransportIT.java b/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpTransportIT.java deleted file mode 100644 index b66b38a89..000000000 --- a/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpTransportIT.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright 2024 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.api.client.http.apache.v5; - -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import com.google.api.client.http.GenericUrl; -import com.google.api.client.http.HttpTransport; -import com.google.common.collect.ImmutableMap; -import java.io.IOException; -import org.apache.hc.client5.http.ConnectTimeoutException; -import org.junit.Test; - -public class Apache5HttpTransportIT { - private static final ImmutableMap payload = - ImmutableMap.of("foo", "bar"); - - // Sets a 5 second delay before response - private static final String NO_CONNECT_URL = "https://google.com:81"; - - @Test(timeout = 10_000L) - public void testConnectTimeoutGet() throws IOException { - HttpTransport transport = new Apache5HttpTransport(); - try { - transport - .createRequestFactory() - .buildGetRequest(new GenericUrl(NO_CONNECT_URL)) - .setConnectTimeout(100) - .execute(); - fail("No exception thrown for HTTP error response"); - } catch (ConnectTimeoutException e) { - assertTrue( - "Expected exception message to contain a connection timeout message", - e.getMessage().contains("Connect timed out")); - } - } - - @Test(timeout = 10_000L) - public void testConnectTimeoutPost() throws IOException { - Apache5HttpTransport transport = new Apache5HttpTransport(); - Apache5HttpRequest request = transport.buildRequest("POST", NO_CONNECT_URL); - request.setTimeout(100, 0); - try { - request.execute(); - fail("No exception thrown for HTTP error response"); - } catch (ConnectTimeoutException e) { - assertTrue( - "Expected exception message to contain a connection timeout message", - e.getMessage().contains("Connect timed out")); - } - } -} diff --git a/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpTransportTest.java b/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpTransportTest.java index 99045d99d..d45026480 100644 --- a/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpTransportTest.java +++ b/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpTransportTest.java @@ -61,6 +61,39 @@ /** Tests {@link Apache5HttpTransport}. */ public class Apache5HttpTransportTest { + private static final String NO_CONNECT_URL = "https://google.com:81"; + + @Test(timeout = 10_000L) + public void testConnectTimeoutGet() throws IOException { + HttpTransport transport = new Apache5HttpTransport(); + try { + transport + .createRequestFactory() + .buildGetRequest(new GenericUrl(NO_CONNECT_URL)) + .setConnectTimeout(100) + .execute(); + fail("No exception thrown for HTTP error response"); + } catch (ConnectTimeoutException e) { + assertTrue( + "Expected exception message to contain a connection timeout message", + e.getMessage().contains("Connect timed out")); + } + } + + @Test(timeout = 10_000L) + public void testConnectTimeoutPost() throws IOException { + Apache5HttpTransport transport = new Apache5HttpTransport(); + Apache5HttpRequest request = transport.buildRequest("POST", NO_CONNECT_URL); + request.setTimeout(100, 0); + try { + request.execute(); + fail("No exception thrown for HTTP error response"); + } catch (ConnectTimeoutException e) { + assertTrue( + "Expected exception message to contain a connection timeout message", + e.getMessage().contains("Connect timed out")); + } + } @Test public void testApacheHttpTransport() { From c64f27307e2d255121a669c5bf66eb5671650b5d Mon Sep 17 00:00:00 2001 From: Diego Alonso Marquez Palacios Date: Fri, 13 Sep 2024 10:34:33 -0400 Subject: [PATCH 3/3] decrease connect timeout --- .../api/client/http/apache/v5/Apache5HttpTransportTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpTransportTest.java b/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpTransportTest.java index d45026480..4bb55a20a 100644 --- a/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpTransportTest.java +++ b/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpTransportTest.java @@ -70,7 +70,7 @@ public void testConnectTimeoutGet() throws IOException { transport .createRequestFactory() .buildGetRequest(new GenericUrl(NO_CONNECT_URL)) - .setConnectTimeout(100) + .setConnectTimeout(10) .execute(); fail("No exception thrown for HTTP error response"); } catch (ConnectTimeoutException e) { @@ -84,7 +84,7 @@ public void testConnectTimeoutGet() throws IOException { public void testConnectTimeoutPost() throws IOException { Apache5HttpTransport transport = new Apache5HttpTransport(); Apache5HttpRequest request = transport.buildRequest("POST", NO_CONNECT_URL); - request.setTimeout(100, 0); + request.setTimeout(10, 0); try { request.execute(); fail("No exception thrown for HTTP error response");