Test AWS HttpClient TLS connection (#2308)

This commit is contained in:
Anuraag Agrawal 2020-12-16 14:38:29 +09:00 committed by GitHub
parent 099b5620da
commit 05a2049095
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 1 deletions

View File

@ -33,7 +33,10 @@ class JdkHttpClient {
private static final int TIMEOUT_MILLIS = 2000;
String fetchString(
String httpMethod, String urlStr, Map<String, String> requestPropertyMap, String certPath) {
String httpMethod,
String urlStr,
Map<String, String> requestPropertyMap,
@Nullable String certPath) {
final HttpURLConnection connection;
try {

View File

@ -10,10 +10,19 @@ import static org.assertj.core.api.Assertions.assertThat;
import com.google.common.collect.ImmutableMap;
import com.linecorp.armeria.common.AggregatedHttpRequest;
import com.linecorp.armeria.common.HttpResponse;
import com.linecorp.armeria.server.ServerBuilder;
import com.linecorp.armeria.testing.junit5.server.SelfSignedCertificateExtension;
import com.linecorp.armeria.testing.junit5.server.ServerExtension;
import com.linecorp.armeria.testing.junit5.server.mock.MockWebServerExtension;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Map;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.io.TempDir;
class JdkHttpClientTest {
@ -44,4 +53,60 @@ class JdkHttpClientTest {
String result = jdkHttpClient.fetchString("GET", urlStr, requestPropertyMap, null);
assertThat(result).isEmpty();
}
static class HttpsServerTest {
@RegisterExtension
@Order(1)
public static SelfSignedCertificateExtension certificate = new SelfSignedCertificateExtension();
@RegisterExtension
@Order(2)
public static ServerExtension server =
new ServerExtension() {
@Override
protected void configure(ServerBuilder sb) {
sb.tls(certificate.certificateFile(), certificate.privateKeyFile());
sb.service("/", (ctx, req) -> HttpResponse.of("Thanks for trusting me"));
}
};
@Test
void goodCert() {
JdkHttpClient jdkHttpClient = new JdkHttpClient();
String result =
jdkHttpClient.fetchString(
"GET",
"https://localhost:" + server.httpsPort() + "/",
Collections.emptyMap(),
certificate.certificateFile().getAbsolutePath());
assertThat(result).isEqualTo("Thanks for trusting me");
}
@Test
void missingCert() {
JdkHttpClient jdkHttpClient = new JdkHttpClient();
String result =
jdkHttpClient.fetchString(
"GET",
"https://localhost:" + server.httpsPort() + "/",
Collections.emptyMap(),
"/foo/bar/bad");
assertThat(result).isEmpty();
}
@Test
void badCert(@TempDir Path tempDir) throws Exception {
Path certFile = tempDir.resolve("test.crt");
Files.write(certFile, "bad cert".getBytes(StandardCharsets.UTF_8));
JdkHttpClient jdkHttpClient = new JdkHttpClient();
String result =
jdkHttpClient.fetchString(
"GET",
"https://localhost:" + server.httpsPort() + "/",
Collections.emptyMap(),
certFile.toString());
assertThat(result).isEmpty();
}
}
}