-
Notifications
You must be signed in to change notification settings - Fork 869
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Skip ALPN if request is HTTP endpoint
- Loading branch information
Showing
5 changed files
with
152 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
test/codegen-generated-classes-test/src/main/resources/codegen-resources/h2/service-2.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"version":"2.0", | ||
"metadata":{ | ||
"apiVersion":"2016-03-11", | ||
"endpointPrefix":"h2-service", | ||
"jsonVersion":"1.1", | ||
"protocol":"rest-json", | ||
"protocolSettings":{"h2":"eventstream"}, | ||
"serviceAbbreviation":"H2 Service", | ||
"serviceFullName":"H2 Test Service", | ||
"serviceId":"H2 Service", | ||
"signatureVersion":"v4", | ||
"targetPrefix":"ProtocolTestsService", | ||
"uid":"restjson-2016-03-11" | ||
}, | ||
"operations":{ | ||
"OneOperation":{ | ||
"name":"OneOperation", | ||
"http":{ | ||
"method":"POST", | ||
"requestUri":"/2016-03-11/oneoperation" | ||
}, | ||
"input":{"shape":"OneShape"} | ||
} | ||
}, | ||
"shapes": { | ||
"OneShape": { | ||
"type": "structure", | ||
"members": { | ||
"StringMember": { | ||
"shape": "String" | ||
} | ||
} | ||
}, | ||
"String":{"type":"string"} | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
...generated-classes-test/src/test/java/software/amazon/awssdk/services/h2/AlpnHttpTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.services.h2; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.lang.invoke.MethodHandle; | ||
import java.lang.invoke.MethodHandles; | ||
import java.lang.invoke.MethodType; | ||
import java.net.ConnectException; | ||
import java.net.URI; | ||
import java.security.AccessController; | ||
import java.security.PrivilegedExceptionAction; | ||
import java.util.concurrent.CompletionException; | ||
import javax.net.ssl.SSLContext; | ||
import javax.net.ssl.SSLEngine; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.condition.EnabledIf; | ||
import software.amazon.awssdk.core.exception.SdkClientException; | ||
import software.amazon.awssdk.http.Protocol; | ||
import software.amazon.awssdk.http.ProtocolNegotiation; | ||
import software.amazon.awssdk.http.nio.netty.NettyNioAsyncHttpClient; | ||
|
||
public class AlpnHttpTest { | ||
|
||
@Test | ||
@EnabledIf("alpnSupported") | ||
public void clientWithDefaultSettingAlpn_httpRequest_doesNotThrowUnsupportedOperationException() { | ||
H2AsyncClient client = clientBuilderWithHttpEndpoint().build(); | ||
|
||
CompletionException e = assertThrows(CompletionException.class, () -> makeRequest(client)); | ||
assertThat(e).hasCauseInstanceOf(SdkClientException.class); | ||
assertThat(e.getCause()).hasCauseInstanceOf(ConnectException.class); | ||
assertThat(e.getMessage()).contains("Connection refused"); | ||
assertThat(e.getMessage()).doesNotContain("ALPN can only be used with HTTPS, not HTTP"); | ||
} | ||
|
||
@Test | ||
@EnabledIf("alpnSupported") | ||
public void clientWithUserConfiguredAlpn_httpRequest_throwsUnsupportedOperationException() { | ||
H2AsyncClient client = clientBuilderWithHttpEndpoint().httpClient(NettyNioAsyncHttpClient.builder() | ||
.protocolNegotiation(ProtocolNegotiation.ALPN) | ||
.protocol(Protocol.HTTP2) | ||
.build()) | ||
.build(); | ||
|
||
CompletionException e = assertThrows(CompletionException.class, () -> makeRequest(client)); | ||
assertThat(e).hasCauseInstanceOf(SdkClientException.class); | ||
assertThat(e.getCause()).hasCauseInstanceOf(UnsupportedOperationException.class); | ||
assertThat(e.getMessage()).contains("ALPN can only be used with HTTPS, not HTTP"); | ||
} | ||
|
||
private H2AsyncClientBuilder clientBuilderWithHttpEndpoint() { | ||
return H2AsyncClient.builder() | ||
.endpointOverride(URI.create("http://localhost:8080")); | ||
} | ||
|
||
private void makeRequest(H2AsyncClient client) { | ||
client.oneOperation(c -> c.stringMember("payload")).join(); | ||
} | ||
|
||
private static boolean alpnSupported() { | ||
try { | ||
SSLContext context = SSLContext.getInstance("TLS"); | ||
context.init(null, null, null); | ||
SSLEngine engine = context.createSSLEngine(); | ||
MethodHandles.Lookup lookup = MethodHandles.lookup(); | ||
|
||
MethodHandle getApplicationProtocol = AccessController.doPrivileged( | ||
(PrivilegedExceptionAction<MethodHandle>) () -> | ||
lookup.findVirtual(SSLEngine.class, "getApplicationProtocol", MethodType.methodType(String.class))); | ||
|
||
getApplicationProtocol.invoke(engine); | ||
return true; | ||
} catch (Throwable t) { | ||
return false; | ||
} | ||
} | ||
} |