From c279296918a4eafbd1826fda4057c1f38cc4a5d0 Mon Sep 17 00:00:00 2001 From: Austin Brooks Date: Wed, 2 Jan 2019 11:04:44 -0800 Subject: [PATCH] Disable following redirects on UrlConnectionHttpClient * Also include a Location header if the request is 3xx in SdkHttpClientTestSuite --- .../bugfix-URLConnectionHTTPClient-2357bd0.json | 5 +++++ .../http/urlconnection/UrlConnectionHttpClient.java | 4 ++++ .../amazon/awssdk/http/SdkHttpClientTestSuite.java | 12 ++++++++++-- 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 .changes/next-release/bugfix-URLConnectionHTTPClient-2357bd0.json diff --git a/.changes/next-release/bugfix-URLConnectionHTTPClient-2357bd0.json b/.changes/next-release/bugfix-URLConnectionHTTPClient-2357bd0.json new file mode 100644 index 000000000000..61c95547d459 --- /dev/null +++ b/.changes/next-release/bugfix-URLConnectionHTTPClient-2357bd0.json @@ -0,0 +1,5 @@ +{ + "category": "URLConnection HTTP Client", + "type": "bugfix", + "description": "Disable following redirects automatically since doing so causes SDK response handling to fail" +} diff --git a/http-clients/url-connection-client/src/main/java/software/amazon/awssdk/http/urlconnection/UrlConnectionHttpClient.java b/http-clients/url-connection-client/src/main/java/software/amazon/awssdk/http/urlconnection/UrlConnectionHttpClient.java index c4613b171e65..389db41045b0 100644 --- a/http-clients/url-connection-client/src/main/java/software/amazon/awssdk/http/urlconnection/UrlConnectionHttpClient.java +++ b/http-clients/url-connection-client/src/main/java/software/amazon/awssdk/http/urlconnection/UrlConnectionHttpClient.java @@ -112,6 +112,10 @@ private HttpURLConnection createAndConfigureConnection(HttpExecuteRequest reques connection.setDoOutput(true); } + // Disable following redirects since it breaks SDK error handling and matches Apache. + // See: https://github.com/aws/aws-sdk-java-v2/issues/975 + connection.setInstanceFollowRedirects(false); + return connection; } diff --git a/test/http-client-tests/src/main/java/software/amazon/awssdk/http/SdkHttpClientTestSuite.java b/test/http-client-tests/src/main/java/software/amazon/awssdk/http/SdkHttpClientTestSuite.java index a76af72bcd8b..970bd2ea656b 100644 --- a/test/http-client-tests/src/main/java/software/amazon/awssdk/http/SdkHttpClientTestSuite.java +++ b/test/http-client-tests/src/main/java/software/amazon/awssdk/http/SdkHttpClientTestSuite.java @@ -28,6 +28,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder; import com.github.tomakehurst.wiremock.junit.WireMockRule; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -129,8 +130,15 @@ protected void testForResponseCodeUsingHttps(SdkHttpClient client, int returnCod } private void stubForMockRequest(int returnCode) { - stubFor(any(urlPathEqualTo("/")).willReturn( - aResponse().withStatus(returnCode).withHeader("Some-Header", "With Value").withBody("hello"))); + ResponseDefinitionBuilder responseBuilder = aResponse().withStatus(returnCode) + .withHeader("Some-Header", "With Value") + .withBody("hello"); + + if (returnCode >= 300 && returnCode <= 399) { + responseBuilder.withHeader("Location", "Some New Location"); + } + + stubFor(any(urlPathEqualTo("/")).willReturn(responseBuilder)); } private void validateResponse(HttpExecuteResponse response, int returnCode) throws IOException {