diff --git a/junit/mockwebserver/pom.xml b/junit/mockwebserver/pom.xml index 1b598652e1d..b7cae35f49e 100644 --- a/junit/mockwebserver/pom.xml +++ b/junit/mockwebserver/pom.xml @@ -32,8 +32,8 @@ - com.squareup.okhttp3 - mockwebserver + io.vertx + vertx-web com.fasterxml.jackson.core diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/DefaultMockServer.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/DefaultMockServer.java index ff9c1ad7154..8a9cffbd428 100644 --- a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/DefaultMockServer.java +++ b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/DefaultMockServer.java @@ -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; @@ -64,8 +62,9 @@ public DefaultMockServer(Context context, MockWebServer server, Map> responses, - Dispatcher dispatcher, boolean useHttps) { + public DefaultMockServer( + Context context, MockWebServer server, Map> responses, Dispatcher dispatcher, + boolean useHttps) { this.context = context; this.useHttps = useHttps; this.server = server; @@ -78,7 +77,7 @@ public DefaultMockServer(Context context, MockWebServer server, Map requestQueue; + private final AtomicInteger requestCount; + private final List listeners; + private Dispatcher dispatcher; + private ClientAuth clientAuth; + private final List enabledSecuredTransportProtocols; + private boolean ssl; + private SelfSignedCertificate selfSignedCertificate; + private HttpServer httpServer; + private int port; + private InetAddress inetAddress; + private List 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) { + if (path.startsWith("/")) { + path = path.substring(1); + } + 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 addListener(MockWebServerListener listener) { + listeners.add(listener); + } + + public void setDispatcher(Dispatcher dispatcher) { + this.dispatcher = dispatcher; + } + + public void setProtocols(List protocols) { + this.protocols = protocols; + } + + private static T await(Future vertxFuture, String errorMessage) { + final CompletableFuture 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); + } + } +} diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/MockWebServerListener.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/MockWebServerListener.java new file mode 100644 index 00000000000..4c14a973ae1 --- /dev/null +++ b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/MockWebServerListener.java @@ -0,0 +1,26 @@ +/** + * 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.RecordedHttpConnection; + +public interface MockWebServerListener { + default void onConnection(RecordedHttpConnection connection) { + } + + default void onConnectionClosed(RecordedHttpConnection connection) { + } +} diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/ServerResponse.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/ServerResponse.java index 2b5e9cb34d4..9ae95693b90 100644 --- a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/ServerResponse.java +++ b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/ServerResponse.java @@ -15,8 +15,8 @@ */ package io.fabric8.mockwebserver; -import okhttp3.mockwebserver.MockResponse; -import okhttp3.mockwebserver.RecordedRequest; +import io.fabric8.mockwebserver.http.MockResponse; +import io.fabric8.mockwebserver.http.RecordedRequest; public interface ServerResponse { diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/crud/CrudDispatcher.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/crud/CrudDispatcher.java index f1d059b6115..f88a2febae7 100644 --- a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/crud/CrudDispatcher.java +++ b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/crud/CrudDispatcher.java @@ -18,10 +18,10 @@ import com.fasterxml.jackson.databind.JsonNode; import io.fabric8.mockwebserver.Context; import io.fabric8.mockwebserver.MockServerException; +import io.fabric8.mockwebserver.http.Dispatcher; +import io.fabric8.mockwebserver.http.MockResponse; +import io.fabric8.mockwebserver.http.RecordedRequest; import io.fabric8.zjsonpatch.JsonPatch; -import okhttp3.mockwebserver.Dispatcher; -import okhttp3.mockwebserver.MockResponse; -import okhttp3.mockwebserver.RecordedRequest; import java.net.HttpURLConnection; import java.util.ArrayList; @@ -32,12 +32,6 @@ public class CrudDispatcher extends Dispatcher { - private static final String POST = "POST"; - private static final String PUT = "PUT"; - private static final String PATCH = "PATCH"; - private static final String GET = "GET"; - private static final String DELETE = "DELETE"; - protected final Map map = Collections.synchronizedMap(new LinkedHashMap<>()); protected final Context context; @@ -53,7 +47,7 @@ public CrudDispatcher(Context context, AttributeExtractor attributeExtractor, Re @Override public MockResponse dispatch(RecordedRequest request) { String path = request.getPath(); - switch (request.getMethod().toUpperCase()) { + switch (request.method()) { case POST: return handleCreate(request); case PUT: diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/dsl/HttpMethod.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/dsl/HttpMethod.java index 817af73b793..c3037ac0859 100644 --- a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/dsl/HttpMethod.java +++ b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/dsl/HttpMethod.java @@ -25,6 +25,17 @@ public enum HttpMethod { DELETE, OPTIONS, CONNECT, - ANY + ANY; + + public static HttpMethod fromVertx(io.vertx.core.http.HttpMethod method) { + if (method != null) { + for (HttpMethod m : HttpMethod.values()) { + if (m.toString().equalsIgnoreCase(method.toString())) { + return m; + } + } + } + return null; + } } diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/MockResponse.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/MockResponse.java index 690d0ee0d5b..6781d9ac2a0 100644 --- a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/MockResponse.java +++ b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/MockResponse.java @@ -166,10 +166,6 @@ public MockResponse removeHeader(String name) { } public MockResponse withWebSocketUpgrade(WebSocketListener listener) { - // TODO: Check if this is necessary with Vert.x - // setStatus("HTTP/1.1 101 Switching Protocols"); - // setHeader("Connection", "Upgrade"); - // setHeader("Upgrade", "websocket"); body = null; webSocketListener = listener; return this; diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/internal/ChunkedResponse.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/internal/ChunkedResponse.java index d5d6df848b8..8a280ed9caa 100644 --- a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/internal/ChunkedResponse.java +++ b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/internal/ChunkedResponse.java @@ -16,10 +16,10 @@ package io.fabric8.mockwebserver.internal; import io.fabric8.mockwebserver.ServerResponse; +import io.fabric8.mockwebserver.http.MockResponse; +import io.fabric8.mockwebserver.http.RecordedRequest; import io.fabric8.mockwebserver.utils.ResponseProvider; import io.fabric8.mockwebserver.utils.ResponseProviders; -import okhttp3.mockwebserver.MockResponse; -import okhttp3.mockwebserver.RecordedRequest; import java.util.List; import java.util.concurrent.TimeUnit; diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/internal/MockDispatcher.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/internal/MockDispatcher.java index 8aa6d41bb64..16c8d9f2b06 100644 --- a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/internal/MockDispatcher.java +++ b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/internal/MockDispatcher.java @@ -18,10 +18,9 @@ import io.fabric8.mockwebserver.ServerRequest; import io.fabric8.mockwebserver.ServerResponse; -import io.fabric8.mockwebserver.dsl.HttpMethod; -import okhttp3.mockwebserver.Dispatcher; -import okhttp3.mockwebserver.MockResponse; -import okhttp3.mockwebserver.RecordedRequest; +import io.fabric8.mockwebserver.http.Dispatcher; +import io.fabric8.mockwebserver.http.MockResponse; +import io.fabric8.mockwebserver.http.RecordedRequest; import java.util.Collection; import java.util.Map; @@ -43,9 +42,8 @@ public MockResponse dispatch(RecordedRequest request) { webSocketSession.dispatch(request); } - HttpMethod method = HttpMethod.valueOf(request.getMethod()); String path = request.getPath(); - SimpleRequest key = new SimpleRequest(method, path); + SimpleRequest key = new SimpleRequest(request.method(), path); SimpleRequest keyForAnyMethod = new SimpleRequest(path); if (responses.containsKey(key)) { Queue queue = responses.get(key); diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/internal/MockSSLContextFactory.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/internal/MockSSLContextFactory.java deleted file mode 100644 index 3a3a1279ffc..00000000000 --- a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/internal/MockSSLContextFactory.java +++ /dev/null @@ -1,40 +0,0 @@ -/** - * 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.internal; - -import io.fabric8.mockwebserver.MockServerException; -import io.fabric8.mockwebserver.utils.SSLUtils; - -import javax.net.ssl.KeyManager; -import javax.net.ssl.SSLContext; - -public class MockSSLContextFactory { - - private MockSSLContextFactory() { - } - - public static SSLContext create() { - try { - KeyManager[] keyManagers = SSLUtils.keyManagers(MockSSLContextFactory.class.getResourceAsStream("/ssl/fabric8.crt"), - MockSSLContextFactory.class.getResourceAsStream("/ssl/fabric8-private-key.pem"), - "RSA", ""); - return SSLUtils.sslContext(keyManagers, null, true); - } catch (Exception e) { - throw new MockServerException("Exception creating SSLContext", e); - } - } -} diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/internal/MockServerExpectationImpl.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/internal/MockServerExpectationImpl.java index 2e30817cf20..5a9e9828a80 100644 --- a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/internal/MockServerExpectationImpl.java +++ b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/internal/MockServerExpectationImpl.java @@ -28,11 +28,11 @@ import io.fabric8.mockwebserver.dsl.ReturnOrWebsocketable; import io.fabric8.mockwebserver.dsl.TimesOnceableOrHttpHeaderable; import io.fabric8.mockwebserver.dsl.WebSocketSessionBuilder; +import io.fabric8.mockwebserver.http.Headers; +import io.fabric8.mockwebserver.http.RecordedRequest; import io.fabric8.mockwebserver.utils.BodyProvider; import io.fabric8.mockwebserver.utils.ResponseProvider; import io.fabric8.mockwebserver.utils.ResponseProviders; -import okhttp3.Headers; -import okhttp3.mockwebserver.RecordedRequest; import java.util.ArrayDeque; import java.util.ArrayList; diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/internal/SimpleResponse.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/internal/SimpleResponse.java index 9515d398a53..5b055d0557f 100644 --- a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/internal/SimpleResponse.java +++ b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/internal/SimpleResponse.java @@ -17,10 +17,10 @@ package io.fabric8.mockwebserver.internal; import io.fabric8.mockwebserver.ServerResponse; +import io.fabric8.mockwebserver.http.MockResponse; +import io.fabric8.mockwebserver.http.RecordedRequest; import io.fabric8.mockwebserver.utils.ResponseProvider; import io.fabric8.mockwebserver.utils.ResponseProviders; -import okhttp3.mockwebserver.MockResponse; -import okhttp3.mockwebserver.RecordedRequest; import java.util.Objects; import java.util.concurrent.TimeUnit; diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/internal/WebSocketSession.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/internal/WebSocketSession.java index ae399ced89f..7cd8ba18118 100644 --- a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/internal/WebSocketSession.java +++ b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/internal/WebSocketSession.java @@ -17,12 +17,11 @@ package io.fabric8.mockwebserver.internal; import io.fabric8.mockwebserver.MockServerException; -import io.fabric8.mockwebserver.dsl.HttpMethod; -import okhttp3.Response; -import okhttp3.WebSocket; -import okhttp3.WebSocketListener; -import okhttp3.mockwebserver.RecordedRequest; -import okio.ByteString; +import io.fabric8.mockwebserver.http.ByteString; +import io.fabric8.mockwebserver.http.RecordedRequest; +import io.fabric8.mockwebserver.http.Response; +import io.fabric8.mockwebserver.http.WebSocket; +import io.fabric8.mockwebserver.http.WebSocketListener; import java.util.ArrayList; import java.util.Collection; @@ -115,9 +114,8 @@ private void checkIfShouldSendAgain(WebSocket ws, WebSocketMessage msg) { } public void dispatch(RecordedRequest request) { - HttpMethod method = HttpMethod.valueOf(request.getMethod()); - String path = request.getPath(); - SimpleRequest key = new SimpleRequest(method, path); + final String path = request.getPath(); + SimpleRequest key = new SimpleRequest(request.method(), path); SimpleRequest keyForAnyMethod = new SimpleRequest(path); if (httpRequestEvents.containsKey(key)) { Queue queue = httpRequestEvents.get(key); @@ -186,6 +184,7 @@ public void shutdown() { executor.shutdownNow(); } } catch (InterruptedException e) { + Thread.currentThread().interrupt(); throw MockServerException.launderThrowable(e); } } diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/utils/BodyProvider.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/utils/BodyProvider.java index 1379f535223..08e67fa88ee 100644 --- a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/utils/BodyProvider.java +++ b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/utils/BodyProvider.java @@ -15,7 +15,7 @@ */ package io.fabric8.mockwebserver.utils; -import okhttp3.mockwebserver.RecordedRequest; +import io.fabric8.mockwebserver.http.RecordedRequest; /** * A class that allows returning the body of a response given a certain request. diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/utils/CertUtils.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/utils/CertUtils.java deleted file mode 100644 index c8dde0df712..00000000000 --- a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/utils/CertUtils.java +++ /dev/null @@ -1,113 +0,0 @@ -/** - * 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.utils; - -import okio.ByteString; - -import java.io.BufferedReader; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.security.KeyFactory; -import java.security.KeyStore; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; -import java.security.PrivateKey; -import java.security.cert.Certificate; -import java.security.cert.CertificateException; -import java.security.cert.CertificateFactory; -import java.security.cert.X509Certificate; -import java.security.spec.InvalidKeySpecException; -import java.security.spec.PKCS8EncodedKeySpec; -import java.security.spec.RSAPrivateCrtKeySpec; -import java.util.Base64; - -public class CertUtils { - - private CertUtils() { - } - - public static InputStream getInputStreamFromDataOrFile(String data, String file) throws FileNotFoundException { - if (data != null) { - final byte[] bytes; - ByteString decoded = ByteString.decodeBase64(data); - if (decoded != null) { - bytes = decoded.toByteArray(); - } else { - bytes = data.getBytes(); - } - - return new ByteArrayInputStream(bytes); - } - if (file != null) { - return new FileInputStream(file); - } - return null; - } - - public static KeyStore createKeyStore(InputStream certInputStream, InputStream keyInputStream, String clientKeyAlgo, - char[] clientKeyPassphrase) - throws IOException, CertificateException, NoSuchAlgorithmException, InvalidKeySpecException, KeyStoreException { - CertificateFactory certFactory = CertificateFactory.getInstance("X509"); - X509Certificate cert = (X509Certificate) certFactory.generateCertificate(certInputStream); - - byte[] keyBytes = decodeKey(keyInputStream); - - PrivateKey privateKey; - - KeyFactory keyFactory = KeyFactory.getInstance(clientKeyAlgo); - try { - // First let's try PKCS8 - privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(keyBytes)); - } catch (InvalidKeySpecException e) { - // Otherwise try PKCS8 - RSAPrivateCrtKeySpec keySpec = PKCS1Util.decodePKCS1(keyBytes); - privateKey = keyFactory.generatePrivate(keySpec); - } - - KeyStore keyStore = KeyStore.getInstance("JKS"); - keyStore.load(null, clientKeyPassphrase); - - String alias = cert.getSubjectX500Principal().getName(); - keyStore.setKeyEntry(alias, privateKey, clientKeyPassphrase, new Certificate[] { cert }); - - return keyStore; - } - - public static KeyStore createKeyStore(String clientCertData, String clientCertFile, String clientKeyData, - String clientKeyFile, String clientKeyAlgo, char[] clientKeyPassphrase) - throws IOException, CertificateException, NoSuchAlgorithmException, InvalidKeySpecException, KeyStoreException { - try (InputStream certInputStream = getInputStreamFromDataOrFile(clientCertData, clientCertFile); - InputStream keyInputStream = getInputStreamFromDataOrFile(clientKeyData, clientKeyFile)) { - return createKeyStore(certInputStream, keyInputStream, clientKeyAlgo, clientKeyPassphrase); - } - } - - private static byte[] decodeKey(InputStream keyInputStream) throws IOException { - try (BufferedReader keyReader = new BufferedReader(new InputStreamReader(keyInputStream)); - ByteArrayOutputStream baos = new ByteArrayOutputStream()) { - String line; - while ((line = keyReader.readLine()) != null) { - baos.write(line.trim().getBytes()); - } - return Base64.getDecoder().decode(baos.toByteArray()); - } - } -} diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/utils/PKCS1Util.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/utils/PKCS1Util.java deleted file mode 100644 index 31c1c82bcfa..00000000000 --- a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/utils/PKCS1Util.java +++ /dev/null @@ -1,141 +0,0 @@ -/** - * 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.utils; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.math.BigInteger; -import java.security.spec.RSAPrivateCrtKeySpec; - -/** - * This code is inspired and taken over from net.auth.core:oauth - * (albeit in a highly stripped variation): - *

- * Source is from http://oauth.googlecode.com/svn/code/java/ which is licensed - * under the APL (http://oauth.googlecode.com/svn/code/java/LICENSE.txt) - *

- * All credits go to the original author (zhang) - * - * @author roland - * @since 30/09/15 - */ -class PKCS1Util { - - private PKCS1Util() { - } - - public static RSAPrivateCrtKeySpec decodePKCS1(byte[] keyBytes) throws IOException { - DerParser parser = new DerParser(keyBytes); - Asn1Object sequence = parser.read(); - sequence.validateSequence(); - parser = new DerParser(sequence.getValue()); - parser.read(); - - return new RSAPrivateCrtKeySpec(next(parser), next(parser), - next(parser), next(parser), - next(parser), next(parser), - next(parser), next(parser)); - } - - // ========================================================================================== - - private static BigInteger next(DerParser parser) throws IOException { - return parser.read().getInteger(); - } - - static class DerParser { - - private final InputStream in; - - DerParser(byte[] bytes) { - this.in = new ByteArrayInputStream(bytes); - } - - Asn1Object read() throws IOException { - int tag = in.read(); - - if (tag == -1) { - throw new IOException("Invalid DER: stream too short, missing tag"); - } - - int length = getLength(); - byte[] value = new byte[length]; - if (in.read(value) < length) { - throw new IOException("Invalid DER: stream too short, missing value"); - } - - return new Asn1Object(tag, value); - } - - private int getLength() throws IOException { - int i = in.read(); - if (i == -1) { - throw new IOException("Invalid DER: length missing"); - } - - if ((i & ~0x7F) == 0) { - return i; - } - - int num = i & 0x7F; - if (i >= 0xFF || num > 4) { - throw new IOException("Invalid DER: length field too big (" - + i + ")"); - } - - byte[] bytes = new byte[num]; - if (in.read(bytes) < num) { - throw new IOException("Invalid DER: length too short"); - } - - return new BigInteger(1, bytes).intValue(); - } - } - - static class Asn1Object { - - private final int type; - private final byte[] value; - private final int tag; - - public Asn1Object(int tag, byte[] value) { - this.tag = tag; - this.type = tag & 0x1F; - this.value = value; - } - - public byte[] getValue() { - return value; - } - - BigInteger getInteger() throws IOException { - if (type != 0x02) { - throw new IOException("Invalid DER: object is not integer"); //$NON-NLS-1$ - } - return new BigInteger(value); - } - - void validateSequence() throws IOException { - if (type != 0x10) { - throw new IOException("Invalid DER: not a sequence"); - } - if ((tag & 0x20) != 0x20) { - throw new IOException("Invalid DER: can't parse primitive entity"); - } - } - } -} diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/utils/ResponseProvider.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/utils/ResponseProvider.java index 9e5ef4fa300..3735d342c36 100644 --- a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/utils/ResponseProvider.java +++ b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/utils/ResponseProvider.java @@ -15,8 +15,8 @@ */ package io.fabric8.mockwebserver.utils; -import okhttp3.Headers; -import okhttp3.mockwebserver.RecordedRequest; +import io.fabric8.mockwebserver.http.Headers; +import io.fabric8.mockwebserver.http.RecordedRequest; /** * A class that allows returning a response given a certain request. diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/utils/ResponseProviders.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/utils/ResponseProviders.java index 784176f6ba5..a07313d77e8 100644 --- a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/utils/ResponseProviders.java +++ b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/utils/ResponseProviders.java @@ -15,8 +15,8 @@ */ package io.fabric8.mockwebserver.utils; -import okhttp3.Headers; -import okhttp3.mockwebserver.RecordedRequest; +import io.fabric8.mockwebserver.http.Headers; +import io.fabric8.mockwebserver.http.RecordedRequest; import java.util.Arrays; import java.util.Collections; diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/utils/SSLUtils.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/utils/SSLUtils.java deleted file mode 100644 index e63e016d8fc..00000000000 --- a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/utils/SSLUtils.java +++ /dev/null @@ -1,72 +0,0 @@ -/** - * 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.utils; - -import java.io.IOException; -import java.io.InputStream; -import java.security.KeyManagementException; -import java.security.KeyStore; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; -import java.security.SecureRandom; -import java.security.UnrecoverableKeyException; -import java.security.cert.CertificateException; -import java.security.cert.X509Certificate; -import java.security.spec.InvalidKeySpecException; - -import javax.net.ssl.KeyManager; -import javax.net.ssl.KeyManagerFactory; -import javax.net.ssl.SSLContext; -import javax.net.ssl.TrustManager; -import javax.net.ssl.X509TrustManager; - -import static io.fabric8.mockwebserver.utils.CertUtils.createKeyStore; - -public final class SSLUtils { - - private SSLUtils() { - //Utility - } - - public static SSLContext sslContext(KeyManager[] keyManagers, TrustManager[] trustManagers, boolean trustCerts) - throws KeyManagementException, NoSuchAlgorithmException { - if (trustManagers == null && trustCerts) { - trustManagers = new TrustManager[] { new X509TrustManager() { - public void checkClientTrusted(X509Certificate[] chain, String s) { - } - - public void checkServerTrusted(X509Certificate[] chain, String s) { - } - - public X509Certificate[] getAcceptedIssuers() { - return new X509Certificate[0]; - } - } }; - } - SSLContext sslContext = SSLContext.getInstance("TLS"); - sslContext.init(keyManagers, trustManagers, new SecureRandom()); - return sslContext; - } - - public static KeyManager[] keyManagers(InputStream certInputStream, InputStream keyInputStream, String algo, - String passphrase) throws UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, CertificateException, - InvalidKeySpecException, IOException { - KeyStore keyStore = createKeyStore(certInputStream, keyInputStream, algo, passphrase.toCharArray()); - KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); - kmf.init(keyStore, passphrase.toCharArray()); - return kmf.getKeyManagers(); - } -} diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/vertx/HttpServerRequestHandler.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/vertx/HttpServerRequestHandler.java new file mode 100644 index 00000000000..69b732caa64 --- /dev/null +++ b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/vertx/HttpServerRequestHandler.java @@ -0,0 +1,86 @@ +/** + * 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.vertx; + +import io.fabric8.mockwebserver.dsl.HttpMethod; +import io.fabric8.mockwebserver.http.Headers; +import io.fabric8.mockwebserver.http.MockResponse; +import io.fabric8.mockwebserver.http.RecordedRequest; +import io.vertx.core.Future; +import io.vertx.core.Handler; +import io.vertx.core.Vertx; +import io.vertx.core.buffer.Buffer; +import io.vertx.core.http.HttpServerRequest; +import io.vertx.core.http.HttpServerResponse; + +public abstract class HttpServerRequestHandler implements Handler { + + private static final String CONTENT_LENGTH = "Content-Length"; + private static final String CONTENT_TYPE = "Content-Type"; + + private final Vertx vertx; + + protected HttpServerRequestHandler(Vertx vertx) { + this.vertx = vertx; + } + + protected abstract MockResponse onHttpRequest(RecordedRequest request); + + @Override + public final void handle(HttpServerRequest event) { + final Handler exceptionHandler = err -> event.response().setStatusCode(500).setStatusMessage(err.getMessage()) + .send(); + event.resume(); + final Future body; + if (event.headers().contains(CONTENT_LENGTH) || event.headers().contains(CONTENT_TYPE)) { + body = event.body(); + } else { + body = Future.succeededFuture(null); + } + body.onFailure(exceptionHandler); + body.onSuccess(bodyBuffer -> { + final RecordedRequest request = new RecordedRequest( + HttpMethod.fromVertx(event.method()), + event.uri(), + Headers.builder().addAll(event.headers()).build(), + new io.fabric8.mockwebserver.http.Buffer(bodyBuffer == null ? null : bodyBuffer.getBytes())); + final MockResponse mockResponse = onHttpRequest(request); + // WebSocket + if (mockResponse.getWebSocketListener() != null) { + event.toWebSocket() + .onFailure(exceptionHandler) + .onSuccess(new ServerWebSocketHandler(request, mockResponse)); + return; + } + // Standard Http Response + final HttpServerResponse vertxResponse = event.response(); + vertxResponse.setStatusCode(mockResponse.code()); + mockResponse.getHeaders().toMultimap().forEach((key, values) -> vertxResponse.headers().add(key, values)); + if (mockResponse.getBody() != null && mockResponse.getBody().size() > 0) { + vertxResponse.headers().add(CONTENT_LENGTH, String.valueOf(mockResponse.getBody().size())); + final io.vertx.core.buffer.Buffer toSend = io.vertx.core.buffer.Buffer.buffer(mockResponse.getBody().getBytes()); + if (mockResponse.getBodyDelay() != null) { + vertx.setTimer(mockResponse.getBodyDelay().toMillis(), timerId -> vertxResponse.send(toSend)); + } else { + vertxResponse.send(toSend); + } + } else { + vertxResponse.end(); + } + }); + + } +} diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/vertx/Protocol.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/vertx/Protocol.java new file mode 100644 index 00000000000..e460cb93b20 --- /dev/null +++ b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/vertx/Protocol.java @@ -0,0 +1,55 @@ +/** + * 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.vertx; + +import io.vertx.core.http.HttpVersion; + +/** + * Compatibility layer for OkHttp. + */ +public enum Protocol { + HTTP_1_0(HttpVersion.HTTP_1_0, "http/1.0"), + HTTP_1_1(HttpVersion.HTTP_1_1, "http/1.1"), + HTTP_2(HttpVersion.HTTP_2, "h2", "h2_prior_knowledge"); + + private final String[] protocolNames; + private final HttpVersion httpVersion; + + Protocol(HttpVersion httpVersion, String... protocolNames) { + this.httpVersion = httpVersion; + this.protocolNames = protocolNames; + } + + public static Protocol get(String protocol) { + for (Protocol p : Protocol.values()) { + for (String protocolName : p.protocolNames) { + if (protocolName.equals(protocol)) { + return p; + } + } + } + throw new IllegalArgumentException("Unknown protocol: " + protocol); + } + + public HttpVersion getHttpVersion() { + return httpVersion; + } + + @Override + public String toString() { + return protocolNames[0]; + } +} diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/vertx/ServerWebSocketHandler.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/vertx/ServerWebSocketHandler.java new file mode 100644 index 00000000000..5d2ed15aba6 --- /dev/null +++ b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/vertx/ServerWebSocketHandler.java @@ -0,0 +1,60 @@ +/** + * 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.vertx; + +import io.fabric8.mockwebserver.http.ByteString; +import io.fabric8.mockwebserver.http.RecordedRequest; +import io.fabric8.mockwebserver.http.Response; +import io.fabric8.mockwebserver.http.WebSocketListener; +import io.vertx.core.Handler; +import io.vertx.core.http.ServerWebSocket; + +public class ServerWebSocketHandler implements Handler { + + private static final int WEBSOCKET_CLOSE_CODE_SERVER_ERROR = 1011; + + private final RecordedRequest request; + private final Response response; + + public ServerWebSocketHandler(RecordedRequest request, Response response) { + this.request = request; + this.response = response; + } + + @Override + public void handle(ServerWebSocket serverWebSocket) { + final WebSocketListener wsListener = response.getWebSocketListener(); + final VertxMockWebSocket mockWebSocket = new VertxMockWebSocket(request, serverWebSocket); + serverWebSocket.textMessageHandler(text -> wsListener.onMessage(mockWebSocket, text)); + serverWebSocket.binaryMessageHandler(buff -> wsListener.onMessage(mockWebSocket, ByteString.of(buff.getBytes()))); + serverWebSocket.frameHandler(frame -> { + if (frame.isClose()) { + wsListener.onClosing(mockWebSocket, frame.closeStatusCode(), frame.closeReason()); + } + serverWebSocket.fetch(1); + }); + // use end, not close, because close is processed immediately vs. end is in frame order + serverWebSocket.endHandler(v -> wsListener.onClosed( + mockWebSocket, + serverWebSocket.closeStatusCode() == null ? WEBSOCKET_CLOSE_CODE_SERVER_ERROR + : serverWebSocket.closeStatusCode(), + serverWebSocket.closeReason())); + serverWebSocket.exceptionHandler(err -> wsListener.onFailure(mockWebSocket, err, response)); + serverWebSocket.accept(); + wsListener.onOpen(mockWebSocket, response); + serverWebSocket.fetch(1); + } +} diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/vertx/VertxMockWebSocket.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/vertx/VertxMockWebSocket.java new file mode 100644 index 00000000000..2efe5117615 --- /dev/null +++ b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/vertx/VertxMockWebSocket.java @@ -0,0 +1,64 @@ +/** + * 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.vertx; + +import io.fabric8.mockwebserver.http.RecordedRequest; +import io.fabric8.mockwebserver.http.WebSocket; +import io.vertx.core.Future; +import io.vertx.core.buffer.Buffer; +import io.vertx.core.http.ServerWebSocket; + +public class VertxMockWebSocket implements WebSocket { + private final RecordedRequest request; + private final ServerWebSocket webSocket; + + public VertxMockWebSocket(RecordedRequest request, ServerWebSocket webSocket) { + this.request = request; + this.webSocket = webSocket; + } + + @Override + public RecordedRequest request() { + return request; + } + + @Override + public boolean send(String text) { + final Future send = webSocket.writeTextMessage(text); + if (send.isComplete()) { + return send.succeeded(); + } + return true; + } + + @Override + public boolean send(byte[] bytes) { + final Future send = webSocket.writeBinaryMessage(Buffer.buffer(bytes)); + if (send.isComplete()) { + return send.succeeded(); + } + return true; + } + + @Override + public boolean close(int code, String reason) { + final Future close = webSocket.close((short) code, reason); + if (close.isComplete()) { + return close.succeeded(); + } + return true; + } +} diff --git a/junit/mockwebserver/src/main/resources/ssl/fabric8-private-key.pem b/junit/mockwebserver/src/main/resources/ssl/fabric8-private-key.pem deleted file mode 100644 index b6bb69d0c55..00000000000 --- a/junit/mockwebserver/src/main/resources/ssl/fabric8-private-key.pem +++ /dev/null @@ -1,50 +0,0 @@ -MIIJQgIBADANBgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQDeWNPC4SJE8DKX -GU5JISsrY1nnI249vmO6x+pEflcGBqaReQehRUNeMFGje92jZk+0xh8NbNFf4Ofs -wJnSXHAupOI6CFERS2aym4IuGX24c7WvoMeH0I8/w8TJTEKNmWCcduaZx5z041gd -kQHQKetYJkzVhh5p2tbIsYlBLe/XGH3IzAVbeFd6GUDK32EyFNrSSOtMEOwbejmH -2wJysLFxGsJFySbnvyEdjDvTdGpNdqPRCU93K/BfRo1ycqSZiSObAh605Ddo3DMQ -lE0rk0im4BQXxwvaVuhbzszR8XIln8/QT5HysiDY5R2hgQq1yJtXtbL7yFGQSeNe -1CG9Gb1JNnHCdAkf+n9RFVoisjsn8MGcSCxpTs0G16Oia41nQaKLByqh4++aithh -Ucd96ujYnIceljMycpkL0VMXzZ7xwHHU+aHPRkRQsDzbf0x0b7MwQD5XkE1rYPzw -/TwJ6qhpPoxzlQ/H5hqEl9zdPpZyQLcfr5YluaRDSTAaR1QruqlWM2Zzy5iBthvx -7hrYNQ/Re5pbp+b4M7h1I6zvohrMiCbtvMQrWYZWGtOHPsW+tUXTQyb2vIYIGIZM -sLZzOijXn78/0IA07xuivqlQ/jmC6jNZAZCC5PRss9KQLWJWu9V3jEcK5dCstJv5 -eocZ6RWy8ShoL2fupp2jYCRota88mQIDAQABAoICAGCtnuYNnij7iAPLLQ7/LRYg -jObDsRuHvTVp16MQjCZCngqC5Z5pz3EU8Wp0YVq7Ec04mCfuONvHnxCCbl/Zca3W -Y8d39kfe0Ti4BVKmItQg+87xydB0DtVa+iXM0uNe3XMq//m9obGZaKbydiddEsex -X5c4SeEdFmcNSvDcWHzeWVMF4w5ytRaSBGox1sE/8CWfLzBT61XHP1yjDd1wlrbn -O7G8VP5PTMbcQucep1onS/OIaNUYddv3gWlSD9/ykVjFAzUERlOB63I6CZP45o4o -wJPWKIE3aLEC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Jh9UA== diff --git a/junit/mockwebserver/src/main/resources/ssl/fabric8.crt b/junit/mockwebserver/src/main/resources/ssl/fabric8.crt deleted file mode 100644 index 5b01aa30127..00000000000 --- a/junit/mockwebserver/src/main/resources/ssl/fabric8.crt +++ /dev/null @@ -1,31 +0,0 @@ ------BEGIN CERTIFICATE----- -MIIFPzCCAycCFFa1f+dP0SR0nMoPfO+MrMRNfjHaMA0GCSqGSIb3DQEBCwUAMFwx -CzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMRUwEwYDVQQHDAxEZWZh -dWx0IENpdHkxITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0y -MTExMDMwODU0NTRaFw0zMTExMDEwODU0NTRaMFwxCzAJBgNVBAYTAkFVMRMwEQYD -VQQIDApTb21lLVN0YXRlMRUwEwYDVQQHDAxEZWZhdWx0IENpdHkxITAfBgNVBAoM -GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDCCAiIwDQYJKoZIhvcNAQEBBQADggIP -ADCCAgoCggIBAN5Y08LhIkTwMpcZTkkhKytjWecjbj2+Y7rH6kR+VwYGppF5B6FF -Q14wUaN73aNmT7TGHw1s0V/g5+zAmdJccC6k4joIURFLZrKbgi4Zfbhzta+gx4fQ -jz/DxMlMQo2ZYJx25pnHnPTjWB2RAdAp61gmTNWGHmna1sixiUEt79cYfcjMBVt4 -V3oZQMrfYTIU2tJI60wQ7Bt6OYfbAnKwsXEawkXJJue/IR2MO9N0ak12o9EJT3cr -8F9GjXJypJmJI5sCHrTkN2jcMxCUTSuTSKbgFBfHC9pW6FvOzNHxciWfz9BPkfKy -INjlHaGBCrXIm1e1svvIUZBJ417UIb0ZvUk2ccJ0CR/6f1EVWiKyOyfwwZxILGlO -zQbXo6JrjWdBoosHKqHj75qK2GFRx33q6Nichx6WMzJymQvRUxfNnvHAcdT5oc9G -RFCwPNt/THRvszBAPleQTWtg/PD9PAnqqGk+jHOVD8fmGoSX3N0+lnJAtx+vliW5 -pENJMBpHVCu6qVYzZnPLmIG2G/HuGtg1D9F7mlun5vgzuHUjrO+iGsyIJu28xCtZ -hlYa04c+xb61RdNDJva8hggYhkywtnM6KNefvz/QgDTvG6K+qVD+OYLqM1kBkILk -9Gyz0pAtYla71XeMRwrl0Ky0m/l6hxnpFbLxKGgvZ+6mnaNgJGi1rzyZAgMBAAEw -DQYJKoZIhvcNAQELBQADggIBAJ1tNTAnPgAbfhXVxtVnnNPFGsrmUgtBj0f8NsY3 -F0ODX50TIjbVLYp7j3u+dgZu9/ruTOHcGLywNi5mJWB+s27KJJn3nBFPmd9d/QIV -zmjn5IVvikXezEjECQOscwDhwpSbzHqLoieDTJntVUyaNctAZM1YOxVKO97pCDdw -tV74xDzdnI/4JQFQPfshD699r3dtU5ax/jiVCvqM5hTAJ2M/UVyQtxm3lKzMYLNu -77chlVf8/hTop9B6Q4tD6Ajj2KPxaHB7y+5lhci5Rvb2YLVDs0HLq8UJmoJW3FLw -slrjs0NerSWoz5JfhmOQ0N9E3NBdV/kGr27WUeSlNOYh5bqneDCX+hPrO/4NtvpG -WnnJX9W6S6e5GBFsNwQIB9SQCjj9zKWqgszS937HRd9gLmnOCPm7jbCO5uOjDo5q -0t+E20r9xv+4il1QV7tkGg13texGDR43aGzsSNQ66PXOwzeeCPkFzrSu1QFBh7LL -69VMJIbgm3ywYJjO0vIi0mW+kAiqcniIxbDTcCuEI0yuVLyRNaAe6kWWLMVaJLUw -V4TNAOT7x8ZYGQGjhz2DAImvXMwZTK2wRwyv8S11G+ebIIUb4EXGbMksjU6tTquq -ViHO3TGAKPTHIjCYdNT/ZGYQ/PHXLmaDGSOcoW8FPT9ROPxXRSNicNfzLJk/o4Im -AZC5 ------END CERTIFICATE----- diff --git a/junit/mockwebserver/src/main/resources/ssl/fabric8.csr b/junit/mockwebserver/src/main/resources/ssl/fabric8.csr deleted file mode 100644 index ef0eea5121d..00000000000 --- a/junit/mockwebserver/src/main/resources/ssl/fabric8.csr +++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BSJbwJDQm -Y4l4Lhg+7cEvqls+Mx9Dq0eSlM7hH7ezOl5c25U+lG74dHLT2gq5ornjdBk2JKnx -2c95646UomKJKVZtzfPLFRJhmVOr2ndkzooF1GlWXZsU57hflH0Y6argAqC+Y/Hu -AFqsm48Uwixex1FfX53aEFnZG1vkDYm48idGUDEa1QNqqC7Wt0qDM8iZtYaHoc9D -wOSD4KGOUOvzooqKmRzHRRRXfL/K3xzFOFAbxJf5YbVHmRGHEWbEXwnjhz1PHgmS -sXNtmVSt7/ycGKRUHyK4s2xIol45EaD7B+80st0fj0n5WGnpX0Wx/XxIepoD7/dG -H3HNjJD9UyGW3l2q6TojQrYLdTo+k9/CS6yMbbI++QyPlv/cnI1JpS/9+wvF8RrX -1AfWplKt+T8gOs64Ns7triUGD96IAqZfj46olQBN90BwCZ1BasneZyDYhClRCrfN -0znZT0cwgCs0q+UU+WmMcfBWO7ctKj3cz3+SmX+R16nTFi5Uuj3J9ED0V1o687jZ -YgtA3vz5F9lf9DaKJ/23GuA2X7HYWCUDiLtB2junYNJ0toJNJw== ------END CERTIFICATE REQUEST----- diff --git a/junit/mockwebserver/src/main/resources/ssl/fabric8.pub b/junit/mockwebserver/src/main/resources/ssl/fabric8.pub deleted file mode 100644 index ba5c2260a5e..00000000000 --- a/junit/mockwebserver/src/main/resources/ssl/fabric8.pub +++ /dev/null @@ -1 +0,0 @@ -ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDeWNPC4SJE8DKXGU5JISsrY1nnI249vmO6x+pEflcGBqaReQehRUNeMFGje92jZk+0xh8NbNFf4OfswJnSXHAupOI6CFERS2aym4IuGX24c7WvoMeH0I8/w8TJTEKNmWCcduaZx5z041gdkQHQKetYJkzVhh5p2tbIsYlBLe/XGH3IzAVbeFd6GUDK32EyFNrSSOtMEOwbejmH2wJysLFxGsJFySbnvyEdjDvTdGpNdqPRCU93K/BfRo1ycqSZiSObAh605Ddo3DMQlE0rk0im4BQXxwvaVuhbzszR8XIln8/QT5HysiDY5R2hgQq1yJtXtbL7yFGQSeNe1CG9Gb1JNnHCdAkf+n9RFVoisjsn8MGcSCxpTs0G16Oia41nQaKLByqh4++aithhUcd96ujYnIceljMycpkL0VMXzZ7xwHHU+aHPRkRQsDzbf0x0b7MwQD5XkE1rYPzw/TwJ6qhpPoxzlQ/H5hqEl9zdPpZyQLcfr5YluaRDSTAaR1QruqlWM2Zzy5iBthvx7hrYNQ/Re5pbp+b4M7h1I6zvohrMiCbtvMQrWYZWGtOHPsW+tUXTQyb2vIYIGIZMsLZzOijXn78/0IA07xuivqlQ/jmC6jNZAZCC5PRss9KQLWJWu9V3jEcK5dCstJv5eocZ6RWy8ShoL2fupp2jYCRota88mQ== diff --git a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerCrudTest.groovy b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerCrudTest.groovy index 2cc28a99290..35cb6b88898 100644 --- a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerCrudTest.groovy +++ b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerCrudTest.groovy @@ -19,7 +19,6 @@ import io.fabric8.mockwebserver.crud.CrudDispatcher import io.vertx.core.Future import io.vertx.core.Vertx import io.vertx.ext.web.client.WebClient -import okhttp3.mockwebserver.MockWebServer import spock.lang.Shared import spock.lang.Specification import spock.util.concurrent.AsyncConditions diff --git a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerHttpsTest.groovy b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerHttpsTest.groovy index 35cb5ff0a4e..ff6eba19294 100644 --- a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerHttpsTest.groovy +++ b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerHttpsTest.groovy @@ -15,24 +15,39 @@ */ package io.fabric8.mockwebserver -import okhttp3.OkHttpClient +import io.vertx.core.Vertx +import io.vertx.ext.web.client.WebClient +import io.vertx.ext.web.client.WebClientOptions import spock.lang.Shared import spock.lang.Specification +import spock.util.concurrent.AsyncConditions class DefaultMockServerHttpsTest extends Specification { + @Shared + static def vertx = Vertx.vertx() + DefaultMockServer server - @Shared - OkHttpClient client = new OkHttpClient() + WebClient client def setup() { server = new DefaultMockServer(true) server.start() + client = WebClient.create(vertx, new WebClientOptions() + .setSsl(true) + .setTrustOptions(server.getSelfSignedCertificate().trustOptions()) + .setKeyCertOptions(server.getSelfSignedCertificate().keyCertOptions())) + } def cleanup() { server.shutdown() + client.close() + } + + def cleanupSpec() { + vertx.close() } def "url, with path, returns URL with HTTPS protocol"() { @@ -42,4 +57,24 @@ class DefaultMockServerHttpsTest extends Specification { then: assert result.startsWith("https://") } + + + def "GET /, with empty store, should return 404"() { + given: "An HTTP request to /" + def request = client.get(server.port, server.getHostName(), "/").ssl(true) + and: "An instance of AsyncConditions" + def async = new AsyncConditions(2) + + when: "The request is sent and completed" + request.send().onComplete { res -> + if (res.failed()) { + res.cause().printStackTrace() + } + async.evaluate { assert res.result().statusCode() == 404 } + async.evaluate { assert res.result().body() == null} + } + + then: "Expect the result to be completed in the specified time" + async.await(10) + } } diff --git a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerTest.groovy b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerTest.groovy index 5ff230abda4..afb1808f758 100644 --- a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerTest.groovy +++ b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerTest.groovy @@ -15,14 +15,14 @@ */ package io.fabric8.mockwebserver +import io.fabric8.mockwebserver.http.Headers +import io.fabric8.mockwebserver.http.RecordedRequest import io.fabric8.mockwebserver.internal.WebSocketMessage import io.fabric8.mockwebserver.utils.ResponseProvider import io.vertx.core.Future import io.vertx.core.Vertx import io.vertx.core.http.WebSocketClient import io.vertx.ext.web.client.WebClient -import okhttp3.Headers -import okhttp3.mockwebserver.RecordedRequest import spock.lang.Specification import spock.util.concurrent.AsyncConditions diff --git a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/crud/CrudDispatcherTest.groovy b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/crud/CrudDispatcherTest.groovy index 5557b094cf2..e018ef5f87b 100644 --- a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/crud/CrudDispatcherTest.groovy +++ b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/crud/CrudDispatcherTest.groovy @@ -17,12 +17,12 @@ package io.fabric8.mockwebserver.crud import io.fabric8.mockwebserver.Context import io.fabric8.mockwebserver.DefaultMockServer +import io.fabric8.mockwebserver.MockWebServer import io.fabric8.mockwebserver.ServerRequest import io.fabric8.mockwebserver.ServerResponse import io.vertx.core.Vertx import io.vertx.core.buffer.Buffer import io.vertx.ext.web.client.WebClient -import okhttp3.mockwebserver.MockWebServer import spock.lang.Shared import spock.lang.Specification import spock.util.concurrent.AsyncConditions