Skip to content

Commit 806ef13

Browse files
committed
feat: MockWebServer is based on Vert.x Web
Signed-off-by: Marc Nuri <marc@marcnuri.com>
1 parent 9ad6f43 commit 806ef13

29 files changed

+486
-568
lines changed

junit/mockwebserver/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@
4747
<groupId>io.fabric8</groupId>
4848
<artifactId>zjsonpatch</artifactId>
4949
</dependency>
50+
<dependency>
51+
<!-- Self Signed certificates can't be generated by all JREs enforce the BouncyCastle for that purpose -->
52+
<groupId>org.bouncycastle</groupId>
53+
<artifactId>bcpkix-jdk18on</artifactId>
54+
<scope>compile</scope>
55+
</dependency>
5056
<dependency>
5157
<groupId>io.vertx</groupId>
5258
<artifactId>vertx-web-client</artifactId>

junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/DefaultMockServer.java

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@
1616
package io.fabric8.mockwebserver;
1717

1818
import io.fabric8.mockwebserver.dsl.MockServerExpectation;
19+
import io.fabric8.mockwebserver.http.Dispatcher;
20+
import io.fabric8.mockwebserver.http.RecordedRequest;
1921
import io.fabric8.mockwebserver.internal.MockDispatcher;
20-
import io.fabric8.mockwebserver.internal.MockSSLContextFactory;
2122
import io.fabric8.mockwebserver.internal.MockServerExpectationImpl;
22-
import okhttp3.mockwebserver.Dispatcher;
23-
import okhttp3.mockwebserver.MockWebServer;
24-
import okhttp3.mockwebserver.RecordedRequest;
23+
import io.vertx.core.net.SelfSignedCertificate;
2524

26-
import java.io.IOException;
2725
import java.net.InetAddress;
2826
import java.net.Proxy;
2927
import java.util.HashMap;
@@ -63,8 +61,9 @@ public DefaultMockServer(Context context, MockWebServer server, Map<ServerReques
6361
this(context, server, responses, new MockDispatcher(responses), useHttps);
6462
}
6563

66-
public DefaultMockServer(Context context, MockWebServer server, Map<ServerRequest, Queue<ServerResponse>> responses,
67-
Dispatcher dispatcher, boolean useHttps) {
64+
public DefaultMockServer(
65+
Context context, MockWebServer server, Map<ServerRequest, Queue<ServerResponse>> responses, Dispatcher dispatcher,
66+
boolean useHttps) {
6867
this.context = context;
6968
this.useHttps = useHttps;
7069
this.server = server;
@@ -77,7 +76,7 @@ public DefaultMockServer(Context context, MockWebServer server, Map<ServerReques
7776
private void startInternal() {
7877
if (initialized.compareAndSet(false, true)) {
7978
if (useHttps) {
80-
server.useHttps(MockSSLContextFactory.create().getSocketFactory(), false);
79+
server.useHttps();
8180
}
8281
onStart();
8382
}
@@ -90,37 +89,23 @@ private void shutdownInternal() {
9089
}
9190

9291
public final void start() {
93-
try {
94-
startInternal();
95-
server.start();
96-
} catch (IOException e) {
97-
throw new MockServerException("Exception when starting DefaultMockServer", e);
98-
}
92+
startInternal();
93+
server.start();
9994
}
10095

10196
public final void start(int port) {
102-
try {
103-
startInternal();
104-
server.start(port);
105-
} catch (IOException e) {
106-
throw new MockServerException("Exception when starting DefaultMockServer with port", e);
107-
}
97+
startInternal();
98+
server.start(port);
10899
}
109100

110101
public final void start(InetAddress inetAddress, int port) {
111-
try {
112-
startInternal();
113-
server.start(inetAddress, port);
114-
} catch (IOException e) {
115-
throw new MockServerException("Exception when starting DefaultMockServer with InetAddress and port", e);
116-
}
102+
startInternal();
103+
server.start(inetAddress, port);
117104
}
118105

119106
public final void shutdown() {
120107
try {
121108
server.shutdown();
122-
} catch (IOException e) {
123-
throw new MockServerException("Exception when stopping DefaultMockServer", e);
124109
} finally {
125110
shutdownInternal();
126111
}
@@ -158,6 +143,11 @@ public Proxy toProxyAddress() {
158143
return server.toProxyAddress();
159144
}
160145

146+
@Override
147+
public SelfSignedCertificate getSelfSignedCertificate() {
148+
return server.getSelfSignedCertificate();
149+
}
150+
161151
/**
162152
* {@inheritDoc}
163153
*/

junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/MockServer.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
package io.fabric8.mockwebserver;
1717

1818
import io.fabric8.mockwebserver.dsl.MockServerExpectation;
19-
import okhttp3.mockwebserver.RecordedRequest;
19+
import io.fabric8.mockwebserver.http.RecordedRequest;
20+
import io.vertx.core.net.SelfSignedCertificate;
2021

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

3839
/**
39-
* The port for the {@link okhttp3.mockwebserver.MockWebServer}.
40+
* The port for the {@link MockWebServer}.
4041
*
4142
* @return the MockWebServer port.
4243
*/
4344
int getPort();
4445

4546
/**
46-
* The host name for the {@link okhttp3.mockwebserver.MockWebServer}.
47-
*
47+
* The host name for the {@link MockWebServer}.
48+
*
4849
* @return the MockWebServer host name;
4950
*/
5051
String getHostName();
5152

5253
/**
53-
* Returns a {@link Proxy} for the {@link okhttp3.mockwebserver.MockWebServer} with the current HostName and Port.
54+
* Returns a {@link Proxy} for the {@link MockWebServer} with the current HostName and Port.
5455
*
5556
* @return a Proxy for the MockWebServer.
5657
*/
5758
Proxy toProxyAddress();
5859

60+
/**
61+
* Returns the {@link SelfSignedCertificate} for the Mock Web Server.
62+
*
63+
* @return the SelfSignedCertificate for the MockWebServer.
64+
*/
65+
SelfSignedCertificate getSelfSignedCertificate();
66+
5967
/**
6068
* Returns a String URL for connecting to this server.
6169
*
@@ -97,7 +105,7 @@ default void onShutdown() {
97105
RecordedRequest takeRequest(long timeout, TimeUnit unit) throws InterruptedException;
98106

99107
/**
100-
* Returns the last (most recent) HTTP request processed by the {@link okhttp3.mockwebserver.MockWebServer}.
108+
* Returns the last (most recent) HTTP request processed by the {@link MockWebServer}.
101109
*
102110
* n.b. This method clears the request queue.
103111
*

0 commit comments

Comments
 (0)