Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,18 @@

package software.amazon.awssdk.http.nio.netty;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.URI;

public class BaseMockServer {

protected int httpPort;
protected int httpsPort;

public BaseMockServer() throws IOException {
httpPort = getUnusedPort();
httpsPort = getUnusedPort();
}

public URI getHttpUri() {
return URI.create(String.format("http://localhost:%s", httpPort));
}

public URI getHttpsUri() {
return URI.create(String.format("https://localhost:%s", httpsPort));
}

public static int getUnusedPort() throws IOException {
try (ServerSocket socket = new ServerSocket(0)) {
socket.setReuseAddress(true);
return socket.getLocalPort();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,25 @@
*/
public class MockH2Server extends BaseMockServer {
private final Server server;
private final ServerConnector httpConnector;
private final ServerConnector http2Connector;
private final HttpConfiguration httpConfiguration;

public MockH2Server(boolean usingAlpn) throws IOException {
server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(httpPort);

// HTTP Configuration
HttpConfiguration httpConfiguration = new HttpConfiguration();
httpConfiguration = new HttpConfiguration();
httpConfiguration.setSecureScheme("https");
httpConfiguration.setSecurePort(httpsPort);
httpConfiguration.setSendXPoweredBy(true);
httpConfiguration.setSendServerVersion(true);

// HTTP Connector
ServerConnector http = new ServerConnector(server,
new HttpConnectionFactory(httpConfiguration),
new HTTP2CServerConnectionFactory(httpConfiguration));
http.setPort(httpPort);
server.addConnector(http);
// HTTP Connector, port 0 makes the kernel assign a free port atomically at bind time
httpConnector = new ServerConnector(server,
new HttpConnectionFactory(httpConfiguration),
new HTTP2CServerConnectionFactory(httpConfiguration));
httpConnector.setPort(0);
server.addConnector(httpConnector);


// HTTPS Configuration
Expand All @@ -86,7 +86,6 @@ public MockH2Server(boolean usingAlpn) throws IOException {

// SSL Connection Factory
SslConnectionFactory ssl;
ServerConnector http2Connector;

if (usingAlpn) {
ssl = new SslConnectionFactory(sslContextFactory, "alpn");
Expand All @@ -98,7 +97,7 @@ public MockH2Server(boolean usingAlpn) throws IOException {
http2Connector = new ServerConnector(server, ssl, h2, new HttpConnectionFactory(https));
}

http2Connector.setPort(httpsPort);
http2Connector.setPort(0);
server.addConnector(http2Connector);

ServletContextHandler context = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
Expand All @@ -108,6 +107,9 @@ public MockH2Server(boolean usingAlpn) throws IOException {

public void start() throws Exception {
server.start();
httpPort = httpConnector.getLocalPort();
httpsPort = http2Connector.getLocalPort();
httpConfiguration.setSecurePort(httpsPort);
}

public void stop() throws Exception {
Expand Down
Loading