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 <[email protected]>
  • Loading branch information
manusa committed Jan 4, 2024
1 parent 3fa8fbf commit dbd1971
Show file tree
Hide file tree
Showing 37 changed files with 684 additions and 648 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
Loading

0 comments on commit dbd1971

Please sign in to comment.