Skip to content

Commit

Permalink
feat: MockWebServer is based on Vert.x Web
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <marc@marcnuri.com>
  • Loading branch information
manusa committed Dec 22, 2023
1 parent ed8054c commit 402c179
Show file tree
Hide file tree
Showing 32 changed files with 652 additions and 587 deletions.
4 changes: 2 additions & 2 deletions junit/mockwebserver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@

<dependencies>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver</artifactId>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@
package io.fabric8.mockwebserver;

import io.fabric8.mockwebserver.dsl.MockServerExpectation;
import io.fabric8.mockwebserver.http.Dispatcher;
import io.fabric8.mockwebserver.http.RecordedRequest;
import io.fabric8.mockwebserver.internal.MockDispatcher;
import io.fabric8.mockwebserver.internal.MockSSLContextFactory;
import io.fabric8.mockwebserver.internal.MockServerExpectationImpl;
import okhttp3.mockwebserver.Dispatcher;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import io.vertx.core.net.SelfSignedCertificate;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Proxy;
import java.util.HashMap;
Expand Down Expand Up @@ -64,8 +62,9 @@ public DefaultMockServer(Context context, MockWebServer server, Map<ServerReques
this(context, server, responses, new MockDispatcher(responses), useHttps);
}

public DefaultMockServer(Context context, MockWebServer server, Map<ServerRequest, Queue<ServerResponse>> responses,
Dispatcher dispatcher, boolean useHttps) {
public DefaultMockServer(
Context context, MockWebServer server, Map<ServerRequest, Queue<ServerResponse>> responses, Dispatcher dispatcher,
boolean useHttps) {
this.context = context;
this.useHttps = useHttps;
this.server = server;
Expand All @@ -78,7 +77,7 @@ public DefaultMockServer(Context context, MockWebServer server, Map<ServerReques
private void startInternal() {
if (initialized.compareAndSet(false, true)) {
if (useHttps) {
server.useHttps(MockSSLContextFactory.create().getSocketFactory(), false);
server.useHttps();
}
onStart();
}
Expand All @@ -91,37 +90,23 @@ private void shutdownInternal() {
}

public final void start() {
try {
startInternal();
server.start();
} catch (IOException e) {
throw new MockServerException("Exception when starting DefaultMockServer", e);
}
startInternal();
server.start();
}

public final void start(int port) {
try {
startInternal();
server.start(port);
} catch (IOException e) {
throw new MockServerException("Exception when starting DefaultMockServer with port", e);
}
startInternal();
server.start(port);
}

public final void start(InetAddress inetAddress, int port) {
try {
startInternal();
server.start(inetAddress, port);
} catch (IOException e) {
throw new MockServerException("Exception when starting DefaultMockServer with InetAddress and port", e);
}
startInternal();
server.start(inetAddress, port);
}

public final void shutdown() {
try {
server.shutdown();
} catch (IOException e) {
throw new MockServerException("Exception when stopping DefaultMockServer", e);
} finally {
shutdownInternal();
}
Expand Down Expand Up @@ -159,6 +144,11 @@ public Proxy toProxyAddress() {
return server.toProxyAddress();
}

@Override
public SelfSignedCertificate getSelfSignedCertificate() {
return server.getSelfSignedCertificate();
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
package io.fabric8.mockwebserver;

import io.fabric8.mockwebserver.dsl.MockServerExpectation;
import okhttp3.mockwebserver.RecordedRequest;
import io.fabric8.mockwebserver.http.RecordedRequest;
import io.vertx.core.net.SelfSignedCertificate;

import java.net.Proxy;
import java.util.concurrent.TimeUnit;
Expand All @@ -36,26 +37,33 @@ default void onShutdown() {
}

/**
* The port for the {@link okhttp3.mockwebserver.MockWebServer}.
* The port for the Mock Web Server.
*
* @return the MockWebServer port.
*/
int getPort();

/**
* The host name for the {@link okhttp3.mockwebserver.MockWebServer}.
*
* The host name for the Mock Web Server.
*
* @return the MockWebServer host name;
*/
String getHostName();

/**
* Returns a {@link Proxy} for the {@link okhttp3.mockwebserver.MockWebServer} with the current HostName and Port.
* Returns a {@link Proxy} for the Mock Web Server with the current HostName and Port.
*
* @return a Proxy for the MockWebServer.
*/
Proxy toProxyAddress();

/**
* Returns the {@link SelfSignedCertificate} for the Mock Web Server.
*
* @return the SelfSignedCertificate for the MockWebServer.
*/
SelfSignedCertificate getSelfSignedCertificate();

/**
* Returns a String URL for connecting to this server.
*
Expand Down Expand Up @@ -97,7 +105,7 @@ default void onShutdown() {
RecordedRequest takeRequest(long timeout, TimeUnit unit) throws InterruptedException;

/**
* Returns the last (most recent) HTTP request processed by the {@link okhttp3.mockwebserver.MockWebServer}.
* Returns the last (most recent) HTTP request processed by the {@link MockWebServer}.
*
* n.b. This method clears the request queue.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 io.fabric8.mockwebserver;

import io.fabric8.mockwebserver.http.Dispatcher;
import io.fabric8.mockwebserver.http.HttpUrl;
import io.fabric8.mockwebserver.http.MockResponse;
import io.fabric8.mockwebserver.http.QueueDispatcher;
import io.fabric8.mockwebserver.http.RecordedHttpConnection;
import io.fabric8.mockwebserver.http.RecordedRequest;
import io.fabric8.mockwebserver.vertx.HttpServerRequestHandler;
import io.fabric8.mockwebserver.vertx.Protocol;
import io.netty.handler.ssl.ClientAuth;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.net.NetServerOptions;
import io.vertx.core.net.SelfSignedCertificate;

import java.io.Closeable;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

import static io.vertx.core.net.SSLOptions.DEFAULT_ENABLED_SECURE_TRANSPORT_PROTOCOLS;

public class MockWebServer implements Closeable {

private static final String[] SUPPORTED_WEBSOCKET_SUB_PROTOCOLS = new String[] {
"v1.channel.k8s.io", "v2.channel.k8s.io", "v3.channel.k8s.io", "v4.channel.k8s.io"
};

private final Vertx vertx;
private final BlockingQueue<RecordedRequest> requestQueue;
private final AtomicInteger requestCount;
private final List<MockWebServerListener> listeners;
private Dispatcher dispatcher;
private ClientAuth clientAuth;
private final List<String> enabledSecuredTransportProtocols;
private boolean ssl;
private SelfSignedCertificate selfSignedCertificate;
private HttpServer httpServer;
private int port;
private InetAddress inetAddress;
private List<Protocol> protocols;
private boolean started;

public MockWebServer() {
vertx = Vertx.vertx();
requestQueue = new LinkedBlockingQueue<>();
requestCount = new AtomicInteger();
listeners = new ArrayList<>();
dispatcher = new QueueDispatcher();
clientAuth = ClientAuth.NONE;
enabledSecuredTransportProtocols = new ArrayList<>();
enabledSecuredTransportProtocols.addAll(DEFAULT_ENABLED_SECURE_TRANSPORT_PROTOCOLS);
protocols = Arrays.asList(Protocol.HTTP_2, Protocol.HTTP_1_1);
}

private void before() {
if (started) {
return;
}
start();
}

public void start() {
start(NetServerOptions.DEFAULT_PORT);
}

public void start(int port) {
start(InetAddress.getLoopbackAddress(), port);
}

public synchronized void start(InetAddress inetAddress, int port) {
if (started) {
throw new IllegalStateException("start() already called");
}
this.started = true;
this.inetAddress = inetAddress;
final HttpServerOptions options = new HttpServerOptions()
.setHost(inetAddress.getHostAddress())
.setPort(port)
.setAlpnVersions(protocols.stream().map(Protocol::getHttpVersion).collect(Collectors.toList()))
.setWebSocketSubProtocols(Arrays.asList(SUPPORTED_WEBSOCKET_SUB_PROTOCOLS));
if (ssl) {
selfSignedCertificate = SelfSignedCertificate.create(getHostName());
options
.setSsl(true)
.setEnabledSecureTransportProtocols(new HashSet<>(enabledSecuredTransportProtocols))
.setTrustOptions(selfSignedCertificate.trustOptions())
.setKeyCertOptions(selfSignedCertificate.keyCertOptions());
}
httpServer = vertx.createHttpServer(options);
httpServer.connectionHandler(event -> {
final RecordedHttpConnection connection = new RecordedHttpConnection(
event.remoteAddress(), event.localAddress(), ssl);
listeners.forEach(listener -> listener.onConnection(connection));
event.closeHandler(res -> listeners.forEach(listener -> listener.onConnectionClosed(connection)));
});
httpServer.requestHandler(new HttpServerRequestHandler(vertx) {
@Override
protected MockResponse onHttpRequest(RecordedRequest request) {
requestCount.incrementAndGet();
requestQueue.add(request);
return dispatcher.dispatch(request);
}
});
await(httpServer.listen(), "Unable to start MockWebServer");
this.port = httpServer.actualPort();
}

public synchronized void shutdown() {
if (!started) {
return;
}
if (httpServer == null) {
throw new IllegalStateException("shutdown() before start()");
}
dispatcher.shutdown();
await(httpServer.close(), "Unable to close MockWebServer");
}

@Override
public void close() throws IOException {
shutdown();
}

public int getPort() {
before();
return port;
}

public String getHostName() {
before();
return inetAddress.getCanonicalHostName();
}

public Proxy toProxyAddress() {
before();
final InetSocketAddress address = new InetSocketAddress(getHostName(), getPort());
return new Proxy(Proxy.Type.HTTP, address);
}

public SelfSignedCertificate getSelfSignedCertificate() {
return selfSignedCertificate;
}

public HttpUrl url(String path) {
final String schema = ssl ? "https" : "http";
return HttpUrl.parse(schema + "://" + getHostName() + ":" + getPort() + "/" + path);
}

public RecordedRequest takeRequest() throws InterruptedException {
return requestQueue.take();
}

public RecordedRequest takeRequest(long timeout, TimeUnit unit) throws InterruptedException {
return requestQueue.poll(timeout, unit);
}

public int getRequestCount() {
return requestCount.get();
}

public void useHttps() {
this.ssl = true;
}

public void enqueue(MockResponse response) {
if (dispatcher instanceof QueueDispatcher) {
((QueueDispatcher) dispatcher).enqueueResponse(response);
} else {
throw new IllegalStateException("Dispatcher is not a QueueDispatcher");
}
}

public void setDispatcher(Dispatcher dispatcher) {
this.dispatcher = dispatcher;
}

public void setProtocols(List<Protocol> protocols) {
this.protocols = protocols;
}

private static <T> T await(Future<T> vertxFuture, String errorMessage) {
final CompletableFuture<T> future = new CompletableFuture<>();
vertxFuture.onComplete(r -> {
if (r.succeeded()) {
future.complete(r.result());
} else {
future.completeExceptionally(r.cause());
}
});
try {
return future.get(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IllegalStateException(e);
} catch (ExecutionException | TimeoutException e) {
throw new IllegalStateException(errorMessage, e);
}
}
}

0 comments on commit 402c179

Please sign in to comment.