Skip to content
This repository has been archived by the owner on Jun 27, 2024. It is now read-only.

Commit

Permalink
feat: HTTP requests added (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
lholota committed May 21, 2020
1 parent cfe48a4 commit ac5a838
Show file tree
Hide file tree
Showing 9 changed files with 254 additions and 0 deletions.
3 changes: 3 additions & 0 deletions nginx/generate_cert.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

sudo openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout localhost.key -out localhost.crt -config localhost.conf
25 changes: 25 additions & 0 deletions nginx/localhost.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[req]
default_bits = 2048
default_keyfile = localhost.key
distinguished_name = req_distinguished_name
req_extensions = req_ext
x509_extensions = v3_ca
prompt = no

[req_distinguished_name]
countryName = CZ
stateOrProvinceName = Prague
localityName = Prague
organizationName = Homecentr
organizationalUnitName = Development
commonName = localhost

[req_ext]
subjectAltName = @alt_names

[v3_ca]
subjectAltName = @alt_names

[alt_names]
DNS.1 = localhost
DNS.2 = 127.0.0.1
21 changes: 21 additions & 0 deletions nginx/localhost.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-----BEGIN CERTIFICATE-----
MIIDizCCAnOgAwIBAgIUJma/ZXRRXvdFjh4w27l+zXs6I4IwDQYJKoZIhvcNAQEL
BQAwbTELMAkGA1UEBhMCQ1oxDzANBgNVBAgMBlByYWd1ZTEPMA0GA1UEBwwGUHJh
Z3VlMRIwEAYDVQQKDAlIb21lY2VudHIxFDASBgNVBAsMC0RldmVsb3BtZW50MRIw
EAYDVQQDDAlsb2NhbGhvc3QwHhcNMjAwNTIxMTAzNTQ0WhcNMzAwNTE5MTAzNTQ0
WjBtMQswCQYDVQQGEwJDWjEPMA0GA1UECAwGUHJhZ3VlMQ8wDQYDVQQHDAZQcmFn
dWUxEjAQBgNVBAoMCUhvbWVjZW50cjEUMBIGA1UECwwLRGV2ZWxvcG1lbnQxEjAQ
BgNVBAMMCWxvY2FsaG9zdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB
AJYAaN8IgUu4iKV9nJL/YJ4kusa28oqR+PsEVKwwczMN6V9f9tbgFuXm8rh0P4Y4
CtYHq+HI5Bo8bkBchQ9AnpcZ5SkaXEy6ZFbarPU+Zal2njm3lFSKgquDrJTEKhdl
IDaeZ3F36A71dmGO3hIxL+8OmhqWV8HVQVPrQ6j4u/Q058jMvfPmhJL7Lq6LYtBr
McXNPWMKAh3Ss+DJuhH+PljvKLLTi6Z/U7/vZ1cqi4Z1SerF32yg/xg/7vnW0cOB
gAG8tcmTVoH73dye5K0DMQo7o0Ibxe2yD5GJp0b3nYp/nMFk+tzxKNCDS1ciDBIf
5yS2zy8/EtLDZGD5fzu9u2UCAwEAAaMjMCEwHwYDVR0RBBgwFoIJbG9jYWxob3N0
ggkxMjcuMC4wLjEwDQYJKoZIhvcNAQELBQADggEBAH8adjtXnRhriN9+ASbsAfhE
uWbnEaFnHRiTLhe5MesslVhNSoVNtHvJtRQ3hTih4gfsOYItPeVQCy29zLa71CA7
pUtZr44P57im2NtgQsofigAR9H07QtFJlEtIYbMYURqz8JWbcV16ULdFnpGoZ4cS
JCd8158TdQrg36bAL4nEGqNxY5okq2d9Hb4vtYu71yE3rqDJhH3WGjnUZ8qrJ740
Xd9lWKI9mlHsj5pjSwLCYtk3UspA1SUe15nNVBApeyv6wXnePc3TF0FJeyr32kSM
nJDR+DHPIeBkNmfPkquocb7/zFuu/598rwZWeOeDLYgSENjrQKpJhlOJDtivXUI=
-----END CERTIFICATE-----
28 changes: 28 additions & 0 deletions nginx/localhost.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCWAGjfCIFLuIil
fZyS/2CeJLrGtvKKkfj7BFSsMHMzDelfX/bW4Bbl5vK4dD+GOArWB6vhyOQaP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muUlIvYj7lVVrR1o7mmtbjTkOAE
26N+0gmXsplSfQvqdHTMnFsWkckm1ccNIAcPJZfJxPrcIyFqvmajG1g9AoGALFGm
zZ+Syehsx60XQyCNHgkb9jwIp1zk5rsbMP98fHa+PpNesPMdmd0NQOb8cFOY8ebl
V+Jxn1i0W94IYp/tGZ2YS5Beq/jq8nZpw/Hve0E95NKdm7KOCVRWGprY5aOc6F0X
wnMTCVOlgswWRCC1hD/qTeE2Y8UzDrL6rhd6W7kCgYAEqxMH3zWFE2qAcuRAUrWm
MOGqN1jxrvU6cF7aDTl/6L17hFhE0LbJsTrE4RzzJg+BcbJni3nnZMYBCJBUNXWj
emcM5l80teCr5U9lK1Wh+rONABpjqZsi3KUBc0GZWX2iS6h/r0z48JTmQcBQoUIQ
nI5yTshTPAil8B2IwJAcCg==
-----END PRIVATE KEY-----
22 changes: 22 additions & 0 deletions nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
server {
listen 80;
listen 443 ssl;
listen [::]:443 ssl;

ssl_certificate /etc/ssl/certs/localhost.crt;
ssl_certificate_key /etc/ssl/private/localhost.key;

ssl_protocols TLSv1.2 TLSv1.1 TLSv1;

root /var/www/html;

index index.html index.nginx-debian.html;

location /200 {
return 200 'Hello, world!';
}

location /500 {
return 500 'Hello, world!';
}
}
36 changes: 36 additions & 0 deletions src/main/java/io/homecentr/testcontainers/SslVerification.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.homecentr.testcontainers;

import javax.net.ssl.*;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;

public class SslVerification {
public static void disable() throws NoSuchAlgorithmException, KeyManagementException {
TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}
};

// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

// Create all-trusting host name verifier
HostnameVerifier allHostsValid = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};

// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.output.OutputFrame;

import javax.net.ssl.HttpsURLConnection;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;

public class GenericContainerEx<SELF extends GenericContainerEx<SELF>> extends GenericContainer<SELF> {
Expand All @@ -19,6 +24,10 @@ public GenericContainerEx(String imageTag) {
super(imageTag);
}

public GenericContainerEx<SELF> withRelativeFileSystemBind(Path path, String containerPath) {
return withRelativeFileSystemBind(path.toString(), containerPath);
}

public GenericContainerEx<SELF> withRelativeFileSystemBind(String relativePath, String containerPath) {
return withRelativeFileSystemBind(relativePath, containerPath, BindMode.READ_WRITE);
}
Expand Down Expand Up @@ -72,6 +81,21 @@ public LogAnalyzer getLogsAnalyzer() {
return new LogAnalyzer(logs);
}

public HttpResponse makeHttpRequest(int port, String pathAndQuery) throws IOException {
return makeHttpRequest("http", port, pathAndQuery);
}

public HttpResponse makeHttpRequest(String protocol, int port, String pathAndQuery) throws IOException {
int mappedPort = getMappedPort(port);
URL target = new URL(String.format("%s:https://%s:%d%s", protocol, getContainerIpAddress(), mappedPort, pathAndQuery));

HttpURLConnection connection = (HttpURLConnection)target.openConnection();

connection.connect();

return new HttpResponse(connection);
}

@NotNull
private String getShellExecutable() throws IOException, InterruptedException {
if(this.execInContainer("bash", "--help").getExitCode() == 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.homecentr.testcontainers.containers;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.util.stream.Collectors;

public class HttpResponse implements AutoCloseable {
private final HttpURLConnection _connection;

private String _responseContent = null;

public HttpResponse(HttpURLConnection connection) {
_connection = connection;
}

public int getResponseCode() throws IOException {
return _connection.getResponseCode();
}

public String getResponseContent() throws IOException {
if(_responseContent == null) {
try (InputStreamReader inputReader = new InputStreamReader(_connection.getInputStream())) {
try (BufferedReader reader = new BufferedReader(inputReader)) {
_responseContent = reader.lines().collect(Collectors.joining());
}
}
}

return _responseContent;
}

@Override
public void close() {
_connection.disconnect();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.homecentr.testcontainers.containers;

import io.homecentr.testcontainers.SslVerification;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;
import java.nio.file.Paths;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;

import static org.junit.Assert.assertEquals;

public class GenericContainerEx_makeHttpRequestShould {
private static GenericContainerEx _nginxContainer;

@BeforeClass
public static void before() throws KeyManagementException, NoSuchAlgorithmException {
_nginxContainer = new GenericContainerEx("nginx")
.withRelativeFileSystemBind(Paths.get( "nginx", "nginx.conf"), "/etc/nginx/conf.d/default.conf")
.withRelativeFileSystemBind(Paths.get("nginx", "localhost.crt"), "/etc/ssl/certs/localhost.crt")
.withRelativeFileSystemBind(Paths.get("nginx", "localhost.key"), "/etc/ssl/private/localhost.key");

_nginxContainer.addExposedPort(443);

_nginxContainer.start();

SslVerification.disable();
}

@AfterClass
public static void after() {
_nginxContainer.close();
}

@Test
public void returnResponseContent() throws IOException {
HttpResponse response = _nginxContainer.makeHttpRequest(80, "/200");

assertEquals("Hello, world!", response.getResponseContent());
}

@Test
public void returnResponseStatus() throws IOException {
HttpResponse response = _nginxContainer.makeHttpRequest(80, "/500");

assertEquals(500, response.getResponseCode());
}

@Test
public void supportHttps() throws IOException {
HttpResponse response = _nginxContainer.makeHttpRequest("https", 443, "/200");

assertEquals("Hello, world!", response.getResponseContent());
}
}

0 comments on commit ac5a838

Please sign in to comment.