Skip to content

Commit

Permalink
Handle HttpURLConnection switching GET->POST and not supporting PATCH
Browse files Browse the repository at this point in the history
  • Loading branch information
Xtansia committed Nov 12, 2024
1 parent 12b2fc6 commit 53f190b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@

package software.amazon.awssdk.http.urlconnection;

import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.net.ProtocolException;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import software.amazon.awssdk.http.SdkHttpClient;
import software.amazon.awssdk.http.SdkHttpClientDefaultTestSuite;
import software.amazon.awssdk.http.SdkHttpMethod;

public class UrlConnectionHttpClientDefaultWireMockTest extends SdkHttpClientDefaultTestSuite {

Expand All @@ -32,4 +37,20 @@ protected SdkHttpClient createSdkHttpClient() {
public void reset() {
HttpsURLConnection.setDefaultSSLSocketFactory((SSLSocketFactory) SSLSocketFactory.getDefault());
}

@Test
@Override
public void supportsRequestBodyOnGetRequest() throws Exception {
// HttpURLConnection is hard-coded to switch GET requests with a body to POST requests, in #getOutputStream0.
testForResponseCode(200, SdkHttpMethod.GET, SdkHttpMethod.POST, true);
}

@Test
@Override
public void supportsRequestBodyOnPatchRequest() {
// HttpURLConnection does not support PATCH requests.
assertThatThrownBy(super::supportsRequestBodyOnPatchRequest)
.hasRootCauseInstanceOf(ProtocolException.class)
.hasRootCauseMessage("Invalid HTTP method: PATCH");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,14 @@ private void testForResponseCode(int returnCode) throws Exception {
testForResponseCode(returnCode, SdkHttpMethod.POST, true);
}

private void testForResponseCode(int returnCode, SdkHttpMethod method, boolean includeBody) throws Exception {
protected void testForResponseCode(int returnCode, SdkHttpMethod method, boolean includeBody) throws Exception {
testForResponseCode(returnCode, method, method, includeBody);
}

protected void testForResponseCode(int returnCode,
SdkHttpMethod method,
SdkHttpMethod expectedMethod,
boolean includeBody) throws Exception {
SdkHttpClient client = createSdkHttpClient();

stubForMockRequest(returnCode);
Expand All @@ -156,7 +163,7 @@ private void testForResponseCode(int returnCode, SdkHttpMethod method, boolean i
.build())
.call();

validateResponse(rsp, returnCode, method, includeBody);
validateResponse(rsp, returnCode, expectedMethod, includeBody);
}

protected void testForResponseCodeUsingHttps(SdkHttpClient client, int returnCode) throws Exception {
Expand Down

0 comments on commit 53f190b

Please sign in to comment.