diff --git a/extensions/istio/tests/src/test/java/io/fabric8/istio/test/v1beta1/AuthorizationPolicyTest.java b/extensions/istio/tests/src/test/java/io/fabric8/istio/test/v1beta1/AuthorizationPolicyTest.java index f0d1ba1478e..d0a86707426 100644 --- a/extensions/istio/tests/src/test/java/io/fabric8/istio/test/v1beta1/AuthorizationPolicyTest.java +++ b/extensions/istio/tests/src/test/java/io/fabric8/istio/test/v1beta1/AuthorizationPolicyTest.java @@ -29,7 +29,7 @@ import io.fabric8.kubernetes.api.model.DeletionPropagation; import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; -import okhttp3.mockwebserver.RecordedRequest; +import io.fabric8.mockwebserver.http.RecordedRequest; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/extensions/istio/tests/src/test/java/io/fabric8/istio/test/v1beta1/DestinationRuleTest.java b/extensions/istio/tests/src/test/java/io/fabric8/istio/test/v1beta1/DestinationRuleTest.java index 44a4091b093..88735f032c1 100644 --- a/extensions/istio/tests/src/test/java/io/fabric8/istio/test/v1beta1/DestinationRuleTest.java +++ b/extensions/istio/tests/src/test/java/io/fabric8/istio/test/v1beta1/DestinationRuleTest.java @@ -27,7 +27,7 @@ import io.fabric8.kubernetes.api.model.DeletionPropagation; import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; -import okhttp3.mockwebserver.RecordedRequest; +import io.fabric8.mockwebserver.http.RecordedRequest; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/extensions/istio/tests/src/test/java/io/fabric8/istio/test/v1beta1/GatewayTest.java b/extensions/istio/tests/src/test/java/io/fabric8/istio/test/v1beta1/GatewayTest.java index 67df8eadd96..7d607c7c4d5 100644 --- a/extensions/istio/tests/src/test/java/io/fabric8/istio/test/v1beta1/GatewayTest.java +++ b/extensions/istio/tests/src/test/java/io/fabric8/istio/test/v1beta1/GatewayTest.java @@ -25,7 +25,7 @@ import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; import io.fabric8.kubernetes.client.utils.Serialization; -import okhttp3.mockwebserver.RecordedRequest; +import io.fabric8.mockwebserver.http.RecordedRequest; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.snakeyaml.engine.v2.api.Load; diff --git a/extensions/istio/tests/src/test/java/io/fabric8/istio/test/v1beta1/PeerAuthenticationTest.java b/extensions/istio/tests/src/test/java/io/fabric8/istio/test/v1beta1/PeerAuthenticationTest.java index 0eb665a497b..660b302d526 100644 --- a/extensions/istio/tests/src/test/java/io/fabric8/istio/test/v1beta1/PeerAuthenticationTest.java +++ b/extensions/istio/tests/src/test/java/io/fabric8/istio/test/v1beta1/PeerAuthenticationTest.java @@ -24,7 +24,7 @@ import io.fabric8.kubernetes.api.model.DeletionPropagation; import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; -import okhttp3.mockwebserver.RecordedRequest; +import io.fabric8.mockwebserver.http.RecordedRequest; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/extensions/istio/tests/src/test/java/io/fabric8/istio/test/v1beta1/ServiceEntryTest.java b/extensions/istio/tests/src/test/java/io/fabric8/istio/test/v1beta1/ServiceEntryTest.java index 49ff961e39e..c1c282f4a7a 100644 --- a/extensions/istio/tests/src/test/java/io/fabric8/istio/test/v1beta1/ServiceEntryTest.java +++ b/extensions/istio/tests/src/test/java/io/fabric8/istio/test/v1beta1/ServiceEntryTest.java @@ -23,7 +23,7 @@ import io.fabric8.kubernetes.api.model.DeletionPropagation; import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; -import okhttp3.mockwebserver.RecordedRequest; +import io.fabric8.mockwebserver.http.RecordedRequest; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/extensions/istio/tests/src/test/java/io/fabric8/istio/test/v1beta1/SidecarTest.java b/extensions/istio/tests/src/test/java/io/fabric8/istio/test/v1beta1/SidecarTest.java index c48e6d9ddf1..b121fc4ad08 100644 --- a/extensions/istio/tests/src/test/java/io/fabric8/istio/test/v1beta1/SidecarTest.java +++ b/extensions/istio/tests/src/test/java/io/fabric8/istio/test/v1beta1/SidecarTest.java @@ -22,7 +22,7 @@ import io.fabric8.kubernetes.api.model.DeletionPropagation; import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; -import okhttp3.mockwebserver.RecordedRequest; +import io.fabric8.mockwebserver.http.RecordedRequest; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/extensions/istio/tests/src/test/java/io/fabric8/istio/test/v1beta1/VirtualServiceTest.java b/extensions/istio/tests/src/test/java/io/fabric8/istio/test/v1beta1/VirtualServiceTest.java index b121b5940c6..c24d1f99ee2 100644 --- a/extensions/istio/tests/src/test/java/io/fabric8/istio/test/v1beta1/VirtualServiceTest.java +++ b/extensions/istio/tests/src/test/java/io/fabric8/istio/test/v1beta1/VirtualServiceTest.java @@ -35,7 +35,7 @@ import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; import io.fabric8.kubernetes.client.utils.Serialization; -import okhttp3.mockwebserver.RecordedRequest; +import io.fabric8.mockwebserver.http.RecordedRequest; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.snakeyaml.engine.v2.api.Load; diff --git a/extensions/istio/tests/src/test/java/io/fabric8/istio/test/v1beta1/WorkloadEntryTest.java b/extensions/istio/tests/src/test/java/io/fabric8/istio/test/v1beta1/WorkloadEntryTest.java index 576e153ea39..6a33cf52dd0 100644 --- a/extensions/istio/tests/src/test/java/io/fabric8/istio/test/v1beta1/WorkloadEntryTest.java +++ b/extensions/istio/tests/src/test/java/io/fabric8/istio/test/v1beta1/WorkloadEntryTest.java @@ -21,7 +21,7 @@ import io.fabric8.kubernetes.api.model.DeletionPropagation; import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; -import okhttp3.mockwebserver.RecordedRequest; +import io.fabric8.mockwebserver.http.RecordedRequest; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/extensions/knative/tests/src/test/java/io/fabric8/knative/test/ServiceTest.java b/extensions/knative/tests/src/test/java/io/fabric8/knative/test/ServiceTest.java index be21b7233db..a3ae73214a4 100644 --- a/extensions/knative/tests/src/test/java/io/fabric8/knative/test/ServiceTest.java +++ b/extensions/knative/tests/src/test/java/io/fabric8/knative/test/ServiceTest.java @@ -21,7 +21,7 @@ import io.fabric8.kubernetes.api.model.DeletionPropagation; import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; -import okhttp3.mockwebserver.RecordedRequest; +import io.fabric8.mockwebserver.http.RecordedRequest; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/extensions/tekton/tests/src/test/java/io/fabric8/tekton/v1beta1/PipelineTest.java b/extensions/tekton/tests/src/test/java/io/fabric8/tekton/v1beta1/PipelineTest.java index d73c004d561..38f4e5369d3 100644 --- a/extensions/tekton/tests/src/test/java/io/fabric8/tekton/v1beta1/PipelineTest.java +++ b/extensions/tekton/tests/src/test/java/io/fabric8/tekton/v1beta1/PipelineTest.java @@ -18,8 +18,8 @@ import io.fabric8.kubernetes.api.model.DeletionPropagation; import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; +import io.fabric8.mockwebserver.http.RecordedRequest; import io.fabric8.tekton.client.TektonClient; -import okhttp3.mockwebserver.RecordedRequest; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/httpclient-okhttp/src/test/java/io/fabric8/kubernetes/client/okhttp/ConnectionPoolLeakageTest.java b/httpclient-okhttp/src/test/java/io/fabric8/kubernetes/client/okhttp/ConnectionPoolLeakageTest.java index 906ed1981ba..38e7d61c8c9 100644 --- a/httpclient-okhttp/src/test/java/io/fabric8/kubernetes/client/okhttp/ConnectionPoolLeakageTest.java +++ b/httpclient-okhttp/src/test/java/io/fabric8/kubernetes/client/okhttp/ConnectionPoolLeakageTest.java @@ -18,10 +18,10 @@ import io.fabric8.kubernetes.client.http.AsyncBody; import io.fabric8.kubernetes.client.http.HttpClient; import io.fabric8.kubernetes.client.http.HttpResponse; +import io.fabric8.mockwebserver.MockWebServer; +import io.fabric8.mockwebserver.http.MockResponse; import okhttp3.ConnectionPool; import okhttp3.Protocol; -import okhttp3.mockwebserver.MockResponse; -import okhttp3.mockwebserver.MockWebServer; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -53,7 +53,7 @@ void setUp() { } @AfterEach - void tearDown() throws Exception { + void tearDown() { server.shutdown(); connectionPool.evictAll(); } @@ -63,7 +63,7 @@ void tearDown() throws Exception { @ValueSource(strings = { "h2_prior_knowledge", "http/1.1" }) void consumeBytes(String protocol) throws Exception { final Protocol p = Protocol.get(protocol); - server.setProtocols(Collections.singletonList(p)); + server.setProtocols(Collections.singletonList(io.fabric8.mockwebserver.vertx.Protocol.get(p.toString()))); server.start(); clientBuilder.getBuilder().protocols(Collections.singletonList(p)); try (HttpClient httpClient = clientBuilder.build()) { diff --git a/httpclient-okhttp/src/test/java/io/fabric8/kubernetes/client/okhttp/OkHttpClientProxyTest.java b/httpclient-okhttp/src/test/java/io/fabric8/kubernetes/client/okhttp/OkHttpClientProxyTest.java index 9972a2db619..aa1dadc8cb7 100644 --- a/httpclient-okhttp/src/test/java/io/fabric8/kubernetes/client/okhttp/OkHttpClientProxyTest.java +++ b/httpclient-okhttp/src/test/java/io/fabric8/kubernetes/client/okhttp/OkHttpClientProxyTest.java @@ -24,9 +24,4 @@ public class OkHttpClientProxyTest extends AbstractHttpClientProxyTest { protected HttpClient.Factory getHttpClientFactory() { return new OkHttpClientFactory(); } - - @Override - protected void proxyConfigurationOtherAuthAddsRequiredHeaders() throws Exception { - // OkHttp uses a response intercept to add the auth proxy headers in case the original response failed - } } diff --git a/junit/kubernetes-server-mock/pom.xml b/junit/kubernetes-server-mock/pom.xml index bb1d4de8e08..14a9853abe1 100644 --- a/junit/kubernetes-server-mock/pom.xml +++ b/junit/kubernetes-server-mock/pom.xml @@ -41,12 +41,21 @@ io.fabric8 mockwebserver - + + org.junit.jupiter + junit-jupiter-api + provided + org.junit.jupiter junit-jupiter-engine test + + junit + junit + provided + org.slf4j @@ -54,11 +63,6 @@ ${slf4j.version} test - - org.junit.jupiter - junit-jupiter-api - provided - org.junit.jupiter junit-jupiter-params diff --git a/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesAttributesExtractor.java b/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesAttributesExtractor.java index d35ad46e60e..5d1ff1e4309 100644 --- a/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesAttributesExtractor.java +++ b/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesAttributesExtractor.java @@ -24,7 +24,7 @@ import io.fabric8.mockwebserver.crud.Attribute; import io.fabric8.mockwebserver.crud.AttributeExtractor; import io.fabric8.mockwebserver.crud.AttributeSet; -import okhttp3.HttpUrl; +import io.fabric8.mockwebserver.http.HttpUrl; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesCrudDispatcher.java b/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesCrudDispatcher.java index b916969e7e9..5af8e8e4940 100644 --- a/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesCrudDispatcher.java +++ b/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesCrudDispatcher.java @@ -30,9 +30,8 @@ import io.fabric8.mockwebserver.crud.Attribute; import io.fabric8.mockwebserver.crud.AttributeSet; import io.fabric8.mockwebserver.crud.CrudDispatcher; -import okhttp3.mockwebserver.MockResponse; -import okhttp3.mockwebserver.RecordedRequest; -import okhttp3.mockwebserver.SocketPolicy; +import io.fabric8.mockwebserver.http.MockResponse; +import io.fabric8.mockwebserver.http.RecordedRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -292,7 +291,6 @@ public MockResponse handleWatch(String path) { .filter(entry -> watch.attributeMatches(entry.getKey())) .forEach(entry -> watch.sendWebSocketResponse(entry.getValue(), Action.ADDED)))); watchEventListeners.add(watchEventListener); - mockResponse.setSocketPolicy(SocketPolicy.KEEP_OPEN); return mockResponse.withWebSocketUpgrade(watchEventListener); } diff --git a/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesMixedDispatcher.java b/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesMixedDispatcher.java index 7daa72e0105..29f891ae13d 100644 --- a/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesMixedDispatcher.java +++ b/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesMixedDispatcher.java @@ -19,11 +19,11 @@ import io.fabric8.mockwebserver.ServerRequest; import io.fabric8.mockwebserver.ServerResponse; import io.fabric8.mockwebserver.dsl.HttpMethod; +import io.fabric8.mockwebserver.http.Dispatcher; +import io.fabric8.mockwebserver.http.MockResponse; +import io.fabric8.mockwebserver.http.RecordedRequest; import io.fabric8.mockwebserver.internal.MockDispatcher; import io.fabric8.mockwebserver.internal.SimpleRequest; -import okhttp3.mockwebserver.Dispatcher; -import okhttp3.mockwebserver.MockResponse; -import okhttp3.mockwebserver.RecordedRequest; import java.util.Collections; import java.util.List; @@ -58,7 +58,7 @@ public KubernetesMixedDispatcher( } @Override - public MockResponse dispatch(RecordedRequest request) throws InterruptedException { + public MockResponse dispatch(RecordedRequest request) { final Queue responseQueue = responses.get( new SimpleRequest(HttpMethod.valueOf(request.getMethod()), request.getPath())); if (responseQueue != null && !responseQueue.isEmpty()) { diff --git a/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesMockServer.java b/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesMockServer.java index db94c7113d0..2297f9de441 100644 --- a/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesMockServer.java +++ b/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesMockServer.java @@ -37,11 +37,11 @@ import io.fabric8.kubernetes.client.utils.Serialization; 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.fabric8.mockwebserver.http.Dispatcher; import io.fabric8.mockwebserver.internal.MockDispatcher; -import okhttp3.mockwebserver.Dispatcher; -import okhttp3.mockwebserver.MockWebServer; import java.net.HttpURLConnection; import java.net.InetAddress; diff --git a/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesMockServerExtension.java b/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesMockServerExtension.java index 7ad84905817..c8c82359082 100644 --- a/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesMockServerExtension.java +++ b/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesMockServerExtension.java @@ -16,15 +16,14 @@ package io.fabric8.kubernetes.client.server.mock; import io.fabric8.kubernetes.client.Client; -import io.fabric8.kubernetes.client.KubernetesClient; import io.fabric8.kubernetes.client.NamespacedKubernetesClient; import io.fabric8.kubernetes.client.utils.Serialization; import io.fabric8.mockwebserver.Context; +import io.fabric8.mockwebserver.MockWebServer; import io.fabric8.mockwebserver.ServerRequest; import io.fabric8.mockwebserver.ServerResponse; +import io.fabric8.mockwebserver.http.Dispatcher; import io.fabric8.mockwebserver.internal.MockDispatcher; -import okhttp3.mockwebserver.Dispatcher; -import okhttp3.mockwebserver.MockWebServer; import org.junit.jupiter.api.extension.AfterAllCallback; import org.junit.jupiter.api.extension.AfterEachCallback; import org.junit.jupiter.api.extension.BeforeAllCallback; @@ -148,14 +147,6 @@ protected void destroyStatic() { staticClient.close(); } - /** - * @deprecated no longer used - */ - @Deprecated - protected Class getClientType() { - return KubernetesClient.class; - } - protected Class getKubernetesMockServerType() { return KubernetesMockServer.class; } diff --git a/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesServer.java b/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesServer.java index ea3b39a14d7..5a0de1a2f7e 100644 --- a/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesServer.java +++ b/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesServer.java @@ -18,11 +18,11 @@ import io.fabric8.kubernetes.client.NamespacedKubernetesClient; import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext; import io.fabric8.mockwebserver.Context; +import io.fabric8.mockwebserver.MockWebServer; import io.fabric8.mockwebserver.ServerRequest; import io.fabric8.mockwebserver.ServerResponse; import io.fabric8.mockwebserver.dsl.MockServerExpectation; -import okhttp3.mockwebserver.MockWebServer; -import okhttp3.mockwebserver.RecordedRequest; +import io.fabric8.mockwebserver.http.RecordedRequest; import org.junit.rules.ExternalResource; import java.net.InetAddress; @@ -95,26 +95,6 @@ public MockServerExpectation expect() { return mock.expect(); } - @Deprecated - public void expectAndReturnAsJson(String path, int code, T body) { - expect().withPath(path).andReturn(code, body).always(); - } - - @Deprecated - public void expectAndReturnAsString(String path, int code, String body) { - expect().withPath(path).andReturn(code, body).always(); - } - - @Deprecated - public void expectAndReturnAsJson(String method, String path, int code, T body) { - expect().withPath(path).andReturn(code, body).always(); - } - - @Deprecated - public void expectAndReturnAsString(String method, String path, int code, String body) { - expect().withPath(path).andReturn(code, body).always(); - } - public KubernetesMockServer getKubernetesMockServer() { return mock; } diff --git a/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/WatchEventsListener.java b/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/WatchEventsListener.java index ab11a0f61f3..ba81bbd4dff 100644 --- a/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/WatchEventsListener.java +++ b/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/WatchEventsListener.java @@ -22,10 +22,10 @@ import io.fabric8.kubernetes.client.utils.Serialization; import io.fabric8.mockwebserver.Context; import io.fabric8.mockwebserver.crud.AttributeSet; +import io.fabric8.mockwebserver.http.Response; +import io.fabric8.mockwebserver.http.WebSocket; +import io.fabric8.mockwebserver.http.WebSocketListener; import io.fabric8.mockwebserver.internal.WebSocketMessage; -import okhttp3.Response; -import okhttp3.WebSocket; -import okhttp3.WebSocketListener; import org.slf4j.Logger; import java.util.Set; diff --git a/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/crud/KubernetesCrudDispatcherHandler.java b/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/crud/KubernetesCrudDispatcherHandler.java index a82e349ec0a..459014f5ff5 100644 --- a/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/crud/KubernetesCrudDispatcherHandler.java +++ b/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/crud/KubernetesCrudDispatcherHandler.java @@ -21,8 +21,8 @@ import io.fabric8.kubernetes.client.utils.Serialization; import io.fabric8.kubernetes.client.utils.Utils; import io.fabric8.mockwebserver.crud.AttributeSet; -import okhttp3.mockwebserver.MockResponse; -import okhttp3.mockwebserver.RecordedRequest; +import io.fabric8.mockwebserver.http.MockResponse; +import io.fabric8.mockwebserver.http.RecordedRequest; import java.net.HttpURLConnection; diff --git a/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/crud/PatchHandler.java b/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/crud/PatchHandler.java index d6518043375..cc1cfc94594 100644 --- a/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/crud/PatchHandler.java +++ b/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/crud/PatchHandler.java @@ -22,9 +22,9 @@ import io.fabric8.kubernetes.client.utils.Serialization; import io.fabric8.kubernetes.client.utils.Utils; import io.fabric8.mockwebserver.crud.AttributeSet; +import io.fabric8.mockwebserver.http.MediaType; +import io.fabric8.mockwebserver.http.MockResponse; import io.fabric8.zjsonpatch.JsonPatch; -import okhttp3.MediaType; -import okhttp3.mockwebserver.MockResponse; import java.net.HttpURLConnection; import java.util.Iterator; diff --git a/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/crud/PostHandler.java b/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/crud/PostHandler.java index 9980caaa97b..a3c016a263d 100644 --- a/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/crud/PostHandler.java +++ b/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/crud/PostHandler.java @@ -23,7 +23,7 @@ import io.fabric8.kubernetes.client.utils.Utils; import io.fabric8.mockwebserver.crud.Attribute; import io.fabric8.mockwebserver.crud.AttributeSet; -import okhttp3.mockwebserver.MockResponse; +import io.fabric8.mockwebserver.http.MockResponse; import java.net.HttpURLConnection; import java.time.ZoneOffset; diff --git a/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/crud/PutHandler.java b/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/crud/PutHandler.java index 81ebd0f02e8..75ebd3d255b 100644 --- a/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/crud/PutHandler.java +++ b/junit/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/crud/PutHandler.java @@ -19,7 +19,7 @@ import io.fabric8.kubernetes.api.model.GenericKubernetesResource; import io.fabric8.kubernetes.client.utils.Serialization; import io.fabric8.mockwebserver.crud.AttributeSet; -import okhttp3.mockwebserver.MockResponse; +import io.fabric8.mockwebserver.http.MockResponse; import java.net.HttpURLConnection; import java.util.Map; diff --git a/junit/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/KubernetesMixedDispatcherTest.java b/junit/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/KubernetesMixedDispatcherTest.java index 1c78461ad62..e13c86c53cd 100644 --- a/junit/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/KubernetesMixedDispatcherTest.java +++ b/junit/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/KubernetesMixedDispatcherTest.java @@ -20,21 +20,18 @@ import io.fabric8.mockwebserver.ServerRequest; import io.fabric8.mockwebserver.ServerResponse; import io.fabric8.mockwebserver.dsl.HttpMethod; +import io.fabric8.mockwebserver.http.Buffer; +import io.fabric8.mockwebserver.http.Headers; +import io.fabric8.mockwebserver.http.MockResponse; +import io.fabric8.mockwebserver.http.RecordedRequest; import io.fabric8.mockwebserver.internal.SimpleRequest; import io.fabric8.mockwebserver.internal.SimpleResponse; -import okhttp3.Headers; -import okhttp3.mockwebserver.MockResponse; -import okhttp3.mockwebserver.RecordedRequest; -import okio.Buffer; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import org.mockito.Mockito; -import java.net.Socket; import java.nio.charset.StandardCharsets; import java.util.ArrayDeque; -import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Queue; @@ -48,25 +45,21 @@ class KubernetesMixedDispatcherTest { private Map> responses; private KubernetesMixedDispatcher dispatcher; - private Socket socket; - @BeforeEach void setUp() { responses = new HashMap<>(); dispatcher = new KubernetesMixedDispatcher(responses); - socket = Mockito.mock(Socket.class, Mockito.RETURNS_DEEP_STUBS); } @Test @DisplayName("dispatch, with matching expectation, returns expectation") - void dispatchWithMatchingExpectation() throws Exception { + void dispatchWithMatchingExpectation() { // Given responses.compute(new SimpleRequest(HttpMethod.GET, "/api/v1/resources/my-resource"), (k, v) -> new ArrayDeque<>()) .add(new SimpleResponse(true, 200, "resourceBody", null)); // When - final MockResponse result = dispatcher.dispatch(new RecordedRequest( - "GET /api/v1/resources/my-resource HTTP/1.1", EMPTY_HEADERS, Collections.emptyList(), - 0, new Buffer(), 0, socket)); + final MockResponse result = dispatcher.dispatch(new RecordedRequest("HTTP/1.1", HttpMethod.GET, + "/api/v1/resources/my-resource", EMPTY_HEADERS, null)); // Then assertThat(result) .hasFieldOrPropertyWithValue("status", "HTTP/1.1 200 OK") @@ -76,19 +69,17 @@ void dispatchWithMatchingExpectation() throws Exception { @Test @DisplayName("dispatch, with existing CRUD resource, returns CRUD resource") - void dispatchWithCrudExistentResource() throws Exception { + void dispatchWithCrudExistentResource() { // Given final Buffer requestBody = new Buffer(); requestBody.writeString("{\"kind\": \"Resource\", \"apiVersion\": \"v1\",\"metadata\": {\"name\": \"my-resource\"}}", StandardCharsets.UTF_8); requestBody.flush(); - dispatcher.dispatch(new RecordedRequest( - "POST /api/v1/resources HTTP/1.1", EMPTY_HEADERS, Collections.emptyList(), - requestBody.size(), requestBody, 0, socket)); + dispatcher.dispatch(new RecordedRequest("HTTP/1.1", HttpMethod.POST, + "/api/v1/resources", EMPTY_HEADERS, requestBody)); // When - final MockResponse result = dispatcher.dispatch(new RecordedRequest( - "GET /api/v1/resources/my-resource HTTP/1.1", EMPTY_HEADERS, Collections.emptyList(), - 0, new Buffer(), 0, socket)); + final MockResponse result = dispatcher.dispatch(new RecordedRequest("HTTP/1.1", HttpMethod.GET, + "/api/v1/resources/my-resource", EMPTY_HEADERS, null)); // Then assertThat(result) .hasFieldOrPropertyWithValue("status", "HTTP/1.1 200 OK") diff --git a/junit/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/crud/KubernetesCrudDispatcherTestBase.java b/junit/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/crud/KubernetesCrudDispatcherTestBase.java index feaa3aec369..60c23af07d8 100644 --- a/junit/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/crud/KubernetesCrudDispatcherTestBase.java +++ b/junit/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/crud/KubernetesCrudDispatcherTestBase.java @@ -19,7 +19,7 @@ import io.fabric8.kubernetes.client.server.mock.KubernetesCrudDispatcher; import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; import io.fabric8.mockwebserver.Context; -import okhttp3.mockwebserver.MockWebServer; +import io.fabric8.mockwebserver.MockWebServer; import org.assertj.core.api.Condition; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/MediaType.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/MediaType.java index 05a49ce013a..bc292fc0726 100644 --- a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/MediaType.java +++ b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/MediaType.java @@ -20,11 +20,11 @@ import java.util.regex.Pattern; /** - * Compatibility layer for OkHttp. + * Provides tooling to parse and manipulate media types. * - * @deprecated should not be needed anymore + *

+ * Compatibility layer for OkHttp. */ -@Deprecated public class MediaType { private static final String TOKEN = "([a-zA-Z0-9-!#$%&'*+.^_`{|}~]+)"; private static final String QUOTED = "\"([^\"]*)\""; diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/WebSocketListener.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/WebSocketListener.java index d652c015485..7d9a3700077 100644 --- a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/WebSocketListener.java +++ b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/WebSocketListener.java @@ -15,6 +15,8 @@ */ package io.fabric8.mockwebserver.http; +import java.nio.charset.StandardCharsets; + public abstract class WebSocketListener { /** @@ -40,10 +42,11 @@ public void onMessage(WebSocket webSocket, String text) { */ @Deprecated public void onMessage(WebSocket webSocket, ByteString bytes) { + onMessage(webSocket, bytes.utf8()); } public void onMessage(WebSocket webSocket, byte[] bytes) { - onMessage(webSocket, ByteString.of(bytes)); + onMessage(webSocket, new String(bytes, StandardCharsets.UTF_8)); } /** 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 83ad81b1148..c9ee9cd0309 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 @@ -230,7 +230,7 @@ private ServerResponse createResponse(boolean repeatable, long delay, TimeUnit d } private ResponseProvider toString(final ResponseProvider provider) { - return new ResponseProvider() { + return new ResponseProvider<>() { @Override public String getBody(RecordedRequest request) { Object object = provider.getBody(request); @@ -255,7 +255,7 @@ public void setHeaders(Headers headers) { } private ResponseProvider> listToString(final ResponseProvider> provider) { - return new ResponseProvider>() { + return new ResponseProvider<>() { @Override public List getBody(RecordedRequest request) { List objects = provider.getBody(request); 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 c13384a909e..67f5349b023 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 @@ -16,12 +16,12 @@ package io.fabric8.mockwebserver.internal; import io.fabric8.mockwebserver.MockServerException; -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.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -79,11 +79,6 @@ public void onOpen(WebSocket webSocket, Response response) { closeActiveSocketsIfApplicable(); } - @Override - public void onMessage(WebSocket webSocket, ByteString bytes) { - onMessage(webSocket, bytes.utf8()); - } - @Override public void onMessage(WebSocket webSocket, String in) { Queue queue = requestEvents.get(in); @@ -109,7 +104,7 @@ private void send(WebSocket ws, Queue queue, String in) { } private void checkIfShouldSendAgain(WebSocket ws, WebSocketMessage msg) { - String text = msg.isBinary() ? ByteString.of(msg.getBytes()).utf8() : msg.getBody(); + String text = msg.isBinary() ? new String(msg.getBytes(), StandardCharsets.UTF_8) : msg.getBody(); if (sentWebSocketMessagesRequestEvents.containsKey(text)) { Queue queue = sentWebSocketMessagesRequestEvents.get(text); send(ws, queue, text); @@ -163,7 +158,7 @@ private void send(final WebSocket ws, final WebSocketMessage message) { executor.schedule(() -> { if (ws != null) { if (message.isBinary()) { - ws.send(ByteString.of(message.getBytes())); + ws.send(message.getBytes()); } else { ws.send(message.getBody()); } 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 fe9dba39862..c838e494a59 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 @@ -54,7 +54,7 @@ public static ResponseProvider> ofAll(int statusCode, R... elements) public static ResponseProvider of(final int statusCode, final BodyProvider bodyProvider) { if (bodyProvider != null) { - return new ResponseProvider() { + return new ResponseProvider<>() { private Headers headers = new Headers.Builder().build(); @Override 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 index 75cfdd1f48f..53c760ac1d3 100644 --- a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/vertx/HttpServerRequestHandler.java +++ b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/vertx/HttpServerRequestHandler.java @@ -42,8 +42,11 @@ protected HttpServerRequestHandler(Vertx vertx) { @Override public final void handle(HttpServerRequest event) { - final Handler exceptionHandler = err -> event.response().setStatusCode(500).setStatusMessage(err.getMessage()) - .send(); + final Handler exceptionHandler = err -> { + if (!event.response().headWritten()) { + event.response().setStatusCode(500).setStatusMessage(err.getMessage()).send(); + } + }; event.resume(); final Future body; if (hasBody(event)) { 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 9dc85c6ca9c..1f0ad639356 100644 --- a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerTest.groovy +++ b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerTest.groovy @@ -194,7 +194,7 @@ class DefaultMockServerTest extends Specification { } } - def "takeRequest, with timeout and no requests, should return null and don't block (after timeout)"() { + def "takeRequest, with timeout and no requests, should return null and don't block after timeout"() { when: def result = server.takeRequest(1, TimeUnit.MICROSECONDS) @@ -651,4 +651,25 @@ class DefaultMockServerTest extends Specification { receivedMessages.poll() == "CREATED" receivedMessages.poll() == "WS-CREATED" } + + def "when setting a chunked response it should be sent in chunks"() { + given: "An expectation with chunked response" + server.expect().get().withPath("/api/v1/chunked-response") + .andReturnChunked(200, "A response that should be sent in chunks") + .always() + and: "A request" + def request = client.get(server.port, server.getHostName(), "/api/v1/chunked-response").send() + and: "An instance of PollingConditions" + def conditions = new PollingConditions(timeout: 10) + + when: "The request is completed" + conditions.eventually { + assert request.isComplete() + } + + then: "Expect the response to match the expectation" + request.result().statusCode() == 200 + request.result().body().toString() == "28\r\nA response that should be sent in chunks\r\n0\r\n" + request.result().headers().get("Transfer-Encoding") == "chunked" + } } diff --git a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerWebSocketTest.groovy b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerWebSocketTest.groovy index a0a31958e42..63257f329ea 100644 --- a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerWebSocketTest.groovy +++ b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerWebSocketTest.groovy @@ -18,9 +18,12 @@ package io.fabric8.mockwebserver import io.vertx.core.AsyncResult import io.vertx.core.Handler import io.vertx.core.Vertx +import io.vertx.core.http.HttpClientOptions +import io.vertx.core.http.HttpMethod +import io.vertx.core.http.HttpVersion +import io.vertx.core.http.RequestOptions import io.vertx.core.http.WebSocket import io.vertx.core.http.WebSocketClient -import io.vertx.core.http.WebSocketConnectOptions import java.util.concurrent.ConcurrentLinkedQueue import java.util.stream.IntStream import spock.lang.Shared @@ -171,37 +174,81 @@ class DefaultMockServerWebSocketTest extends Specification { } // https://github.com/fabric8io/mockwebserver/issues/77 + // n.b. Uses an HTTP client in its simplest form (emulating a WebSocket protocol negotiation) + // since it's the only reliable way to get the protocol negotiation headers (in this case 'sec-websocket-protocol') def "andUpgradeToWebSocket, with request header 'sec-websocket-protocol', should create response with matching header"() { given: "A WebSocket expectation" server.expect() .withPath("/websocket") .andUpgradeToWebSocket().open().waitFor(10L).andEmit("done").done().always() - and: "A WebSocket request" - def wsReq = wsClient.webSocket().connect(new WebSocketConnectOptions() - .setPort(server.port) + and: "An HTTP client" + def httpClient = vertx.createHttpClient(new HttpClientOptions() + .setProtocolVersion(HttpVersion.HTTP_1_1)) + and: "A simple HTTP request to retrieve the headers and status" + // Emulates an HTTP Request as a WebSocket client would perform it + // This is mimicking what jdk.internal.net.http.websocket.OpeningHandshake does + def request = httpClient.request(new RequestOptions() + .setPort(server.getPort()) .setHost(server.getHostName()) .setURI("/websocket") - .addSubProtocol("v4.channel.k8s.io")) - and: "A WebSocket listener" - String secWebSocketProtocol - wsReq.onComplete { ws -> - if (ws.result() != null && ws.result().headers() != null) { - secWebSocketProtocol = ws.result().headers().get("sec-websocket-protocol") - } - } - def currentResult = wsReq.result() - if (currentResult != null && currentResult.headers() != null) { - secWebSocketProtocol = currentResult.headers().get("sec-websocket-protocol") - } + .setMethod(HttpMethod.GET) + .putHeader("sec-websocket-protocol", "v4.channel.k8s.io") + .putHeader("sec-websocket-version", "13") + .putHeader("sec-websocket-key", Base64.getEncoder().encodeToString(new byte[16])) + .putHeader("connection", "upgrade") + .putHeader("upgrade", "websocket") + .putHeader("origin", "http://localhost")) + .compose { req -> + req.send() + } and: "An instance of PollingConditions" def conditions = new PollingConditions(timeout: 10) - when: "The request is sent and completed" + when: "The request is completed" conditions.eventually { - assert secWebSocketProtocol != null + assert request.isComplete() } then: "Expect the response to contain a matching header" - secWebSocketProtocol == "v4.channel.k8s.io" + request.result().statusCode() == 101 + request.result().headers().get("sec-websocket-protocol") == "v4.channel.k8s.io" + + cleanup: + httpClient.close() + } + + def "andUpgradeToWebSocket, with invalid request should respond with 400 status message"() { + given: "A WebSocket expectation" + server.expect() + .withPath("/websocket") + .andUpgradeToWebSocket().open().waitFor(10L).andEmit("done").done().always() + and: "An HTTP client" + def httpClient = vertx.createHttpClient(new HttpClientOptions() + .setProtocolVersion(HttpVersion.HTTP_1_1)) + and: "A simple HTTP request to retrieve the headers and status" + // Emulates an HTTP Request as a WebSocket client would perform it + // This is mimicking what jdk.internal.net.http.websocket.OpeningHandshake does + def request = httpClient.request(new RequestOptions() + .setPort(server.getPort()) + .setHost(server.getHostName()) + .setURI("/websocket") + .setMethod(HttpMethod.GET) + .putHeader("connection", "upgrade")) + .compose { req -> + req.send() + } + and: "An instance of PollingConditions" + def conditions = new PollingConditions(timeout: 10) + + when: "The request is completed" + conditions.eventually { + assert request.isComplete() + } + + then: "Expect the response to have a client error status code" + request.result().statusCode() == 400 + + cleanup: + httpClient.close() } } diff --git a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/MockWebServerTest.groovy b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/MockWebServerTest.groovy index 40d6a5237ee..beb9182edb0 100644 --- a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/MockWebServerTest.groovy +++ b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/MockWebServerTest.groovy @@ -20,7 +20,6 @@ import io.fabric8.mockwebserver.http.RecordedRequest import io.vertx.core.Future import io.vertx.core.Vertx import io.vertx.ext.web.client.WebClient -import io.vertx.ext.web.client.WebClientOptions import java.util.concurrent.TimeUnit import spock.lang.Shared import spock.lang.Specification diff --git a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/http/WebSocketListenerTest.groovy b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/http/WebSocketListenerTest.groovy index 9b877a10e92..b1df8bc3a4c 100644 --- a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/http/WebSocketListenerTest.groovy +++ b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/http/WebSocketListenerTest.groovy @@ -20,13 +20,13 @@ import spock.lang.Specification class WebSocketListenerTest extends Specification { - def "onMessage(WebSocket,byte[]) calls deprecated onMessage(WebSocket,ByteString) method"() { + def "onMessage(WebSocket,byte[]) calls onMessage(WebSocket,String) method by default"() { given: String message def listener = new WebSocketListener(){ @Override - void onMessage(WebSocket webSocket, ByteString bytes) { - message = bytes.utf8() + void onMessage(WebSocket webSocket, String msg) { + message = msg } } when: @@ -34,4 +34,20 @@ class WebSocketListenerTest extends Specification { then: message == "Hello, world!" } + + @SuppressWarnings('GrDeprecatedAPIUsage') + def "onMessage(WebSocket,ByteString) calls onMessage(WebSocket,String) method by default"() { + given: + String message + def listener = new WebSocketListener(){ + @Override + void onMessage(WebSocket webSocket, String msg) { + message = msg + } + } + when: + listener.onMessage(null, ByteString.of("Hello, world!".getBytes(StandardCharsets.UTF_8))) + then: + message == "Hello, world!" + } } diff --git a/junit/openshift-server-mock/pom.xml b/junit/openshift-server-mock/pom.xml index 44f93f1b38a..4f98748e8ba 100644 --- a/junit/openshift-server-mock/pom.xml +++ b/junit/openshift-server-mock/pom.xml @@ -44,6 +44,13 @@ assertj-core test + + + junit + junit + provided + + org.junit.jupiter junit-jupiter-api diff --git a/junit/openshift-server-mock/src/main/java/io/fabric8/openshift/client/server/mock/OpenShiftMockServer.java b/junit/openshift-server-mock/src/main/java/io/fabric8/openshift/client/server/mock/OpenShiftMockServer.java index 454137c78f2..3f31342618a 100644 --- a/junit/openshift-server-mock/src/main/java/io/fabric8/openshift/client/server/mock/OpenShiftMockServer.java +++ b/junit/openshift-server-mock/src/main/java/io/fabric8/openshift/client/server/mock/OpenShiftMockServer.java @@ -17,12 +17,12 @@ import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; import io.fabric8.mockwebserver.Context; +import io.fabric8.mockwebserver.MockWebServer; import io.fabric8.mockwebserver.ServerRequest; import io.fabric8.mockwebserver.ServerResponse; +import io.fabric8.mockwebserver.http.Dispatcher; import io.fabric8.openshift.client.NamespacedOpenShiftClient; import io.fabric8.openshift.client.OpenShiftConfig; -import okhttp3.mockwebserver.Dispatcher; -import okhttp3.mockwebserver.MockWebServer; import java.util.Map; import java.util.Queue; diff --git a/junit/openshift-server-mock/src/main/java/io/fabric8/openshift/client/server/mock/OpenShiftMockServerExtension.java b/junit/openshift-server-mock/src/main/java/io/fabric8/openshift/client/server/mock/OpenShiftMockServerExtension.java index 7cb0a6eba84..dd775f655a6 100644 --- a/junit/openshift-server-mock/src/main/java/io/fabric8/openshift/client/server/mock/OpenShiftMockServerExtension.java +++ b/junit/openshift-server-mock/src/main/java/io/fabric8/openshift/client/server/mock/OpenShiftMockServerExtension.java @@ -18,11 +18,11 @@ import io.fabric8.kubernetes.client.server.mock.KubernetesMixedDispatcher; import io.fabric8.kubernetes.client.server.mock.KubernetesMockServerExtension; import io.fabric8.mockwebserver.Context; +import io.fabric8.mockwebserver.MockWebServer; import io.fabric8.mockwebserver.ServerRequest; import io.fabric8.mockwebserver.ServerResponse; import io.fabric8.openshift.client.NamespacedOpenShiftClient; import io.fabric8.openshift.client.OpenShiftClient; -import okhttp3.mockwebserver.MockWebServer; import org.junit.jupiter.api.extension.ExtensionContext; import java.lang.reflect.Field; @@ -58,11 +58,6 @@ protected void destroy() { } } - @Override - protected Class getClientType() { - return OpenShiftClient.class; - } - @Override protected Class getKubernetesMockServerType() { return OpenShiftMockServer.class; @@ -100,7 +95,7 @@ protected void setFieldIfKubernetesClientOrMockServer(ExtensionContext context, openShiftClient = instantOpenShiftClient; openShiftMockServer = instantOpenShiftMockServer; } - setFieldIfEqualsToProvidedType(context, isStatic, field, getClientType(), (i, f) -> f.set(i, openShiftClient)); + setFieldIfEqualsToProvidedType(context, isStatic, field, OpenShiftClient.class, (i, f) -> f.set(i, openShiftClient)); setFieldIfEqualsToProvidedType(context, isStatic, field, getKubernetesMockServerType(), (i, f) -> f.set(i, openShiftMockServer)); } diff --git a/junit/openshift-server-mock/src/main/java/io/fabric8/openshift/client/server/mock/OpenShiftServer.java b/junit/openshift-server-mock/src/main/java/io/fabric8/openshift/client/server/mock/OpenShiftServer.java index b670b7c51c9..c2a1bdd1d35 100644 --- a/junit/openshift-server-mock/src/main/java/io/fabric8/openshift/client/server/mock/OpenShiftServer.java +++ b/junit/openshift-server-mock/src/main/java/io/fabric8/openshift/client/server/mock/OpenShiftServer.java @@ -18,12 +18,12 @@ import io.fabric8.kubernetes.client.KubernetesClient; import io.fabric8.kubernetes.client.server.mock.KubernetesMixedDispatcher; import io.fabric8.mockwebserver.Context; +import io.fabric8.mockwebserver.MockWebServer; import io.fabric8.mockwebserver.ServerRequest; import io.fabric8.mockwebserver.ServerResponse; import io.fabric8.mockwebserver.dsl.MockServerExpectation; +import io.fabric8.mockwebserver.http.RecordedRequest; import io.fabric8.openshift.client.NamespacedOpenShiftClient; -import okhttp3.mockwebserver.MockWebServer; -import okhttp3.mockwebserver.RecordedRequest; import org.junit.rules.ExternalResource; import java.util.HashMap; diff --git a/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/AbstractHttpClientProxyHttpsTest.java b/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/AbstractHttpClientProxyHttpsTest.java index fe7b8c059f7..2c21a9ff2a1 100644 --- a/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/AbstractHttpClientProxyHttpsTest.java +++ b/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/AbstractHttpClientProxyHttpsTest.java @@ -15,22 +15,20 @@ */ package io.fabric8.kubernetes.client.http; +import io.fabric8.kubernetes.client.RequestConfigBuilder; import io.fabric8.kubernetes.client.internal.SSLUtils; 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.fabric8.mockwebserver.dsl.HttpMethod; +import io.fabric8.mockwebserver.http.Headers; +import io.fabric8.mockwebserver.http.RecordedRequest; import io.fabric8.mockwebserver.internal.MockDispatcher; -import io.fabric8.mockwebserver.internal.MockSSLContextFactory; import io.fabric8.mockwebserver.internal.SimpleRequest; import io.fabric8.mockwebserver.internal.SimpleResponse; import io.fabric8.mockwebserver.utils.ResponseProvider; -import okhttp3.Headers; -import okhttp3.mockwebserver.MockResponse; -import okhttp3.mockwebserver.MockWebServer; -import okhttp3.mockwebserver.RecordedRequest; -import okhttp3.mockwebserver.SocketPolicy; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; @@ -49,42 +47,32 @@ public abstract class AbstractHttpClientProxyHttpsTest { - private static SocketPolicy defaultResponseSocketPolicy; private static Map> responses; - private static DefaultMockServer server; + private static DefaultMockServer proxyServer; @BeforeAll static void beforeAll() { - defaultResponseSocketPolicy = SocketPolicy.KEEP_OPEN; responses = new HashMap<>(); - final MockWebServer okHttpMockWebServer = new MockWebServer(); - final MockDispatcher dispatcher = new MockDispatcher(responses) { - @Override - public MockResponse peek() { - return new MockResponse().setSocketPolicy(defaultResponseSocketPolicy); - } - }; - server = new DefaultMockServer(new Context(), okHttpMockWebServer, responses, dispatcher, true); - server.start(); - okHttpMockWebServer.useHttps(MockSSLContextFactory.create().getSocketFactory(), true); + proxyServer = new DefaultMockServer(new Context(), new MockWebServer(), responses, new MockDispatcher(responses), false); + proxyServer.start(); } @AfterAll static void afterAll() { - server.shutdown(); + proxyServer.shutdown(); } protected abstract HttpClient.Factory getHttpClientFactory(); @Test - @DisplayName("Proxied HttpClient adds required headers to the request") + @DisplayName("Proxied HttpClient with basic authorization adds required headers to the request") protected void proxyConfigurationAddsRequiredHeadersForHttps() throws Exception { final AtomicReference initialConnectRequest = new AtomicReference<>(); final ResponseProvider bodyProvider = new ResponseProvider() { @Override public String getBody(RecordedRequest request) { - return ""; + return "\n"; } @Override @@ -93,7 +81,6 @@ public void setHeaders(Headers headers) { @Override public int getStatusCode(RecordedRequest request) { - defaultResponseSocketPolicy = SocketPolicy.UPGRADE_TO_SSL_AT_END; // for jetty to upgrade after the challenge if (request.getHeader(StandardHttpHeaders.PROXY_AUTHORIZATION) != null) { initialConnectRequest.compareAndSet(null, request); return 200; @@ -107,20 +94,20 @@ public Headers getHeaders() { } }; - responses.computeIfAbsent(new SimpleRequest(HttpMethod.CONNECT, "/"), k -> new ArrayDeque<>()) + responses.computeIfAbsent(new SimpleRequest(HttpMethod.CONNECT, "example.com:443"), k -> new ArrayDeque<>()) .add(new SimpleResponse(true, bodyProvider, null, 0, TimeUnit.SECONDS)); // Given final HttpClient.Builder builder = getHttpClientFactory().newBuilder() .sslContext(null, SSLUtils.trustManagers(null, null, true, null, null)) - .proxyAddress(new InetSocketAddress("localhost", server.getPort())) + .proxyAddress(new InetSocketAddress("localhost", proxyServer.getPort())) .proxyAuthorization(basicCredentials("auth", "cred")); + builder.tag(new RequestConfigBuilder().withRequestRetryBackoffInterval(1).build()); try (HttpClient client = builder.build()) { - // When - client.sendAsync(client.newHttpRequestBuilder() - .uri(String.format("https://0.0.0.0:%s/not-found", server.getPort() + 1)).build(), String.class) + // When (just send and ignore response, we only care about the CONNECT request headers) + client.sendAsync(client.newHttpRequestBuilder().uri("https://example.com/proxied").build(), String.class) + .exceptionally(t -> null) .get(30, TimeUnit.SECONDS); - - // if it fails, then authorization was not set + // Then assertThat(initialConnectRequest) .doesNotHaveNullValue() .hasValueMatching(r -> r.getHeader("Proxy-Authorization").equals("Basic YXV0aDpjcmVk")); diff --git a/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/AbstractHttpClientProxyTest.java b/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/AbstractHttpClientProxyTest.java index c04397ad8aa..92e6878c95b 100644 --- a/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/AbstractHttpClientProxyTest.java +++ b/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/AbstractHttpClientProxyTest.java @@ -16,9 +16,9 @@ package io.fabric8.kubernetes.client.http; import io.fabric8.mockwebserver.DefaultMockServer; +import io.fabric8.mockwebserver.http.Headers; +import io.fabric8.mockwebserver.http.RecordedRequest; import io.fabric8.mockwebserver.utils.ResponseProvider; -import okhttp3.Headers; -import okhttp3.mockwebserver.RecordedRequest; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; @@ -30,19 +30,20 @@ import static io.fabric8.kubernetes.client.utils.HttpClientUtils.basicCredentials; import static org.assertj.core.api.Assertions.assertThat; +@SuppressWarnings("HttpUrlsUsage") public abstract class AbstractHttpClientProxyTest { - private static DefaultMockServer server; + private static DefaultMockServer proxyServer; @BeforeAll static void beforeAll() { - server = new DefaultMockServer(false); - server.start(); + proxyServer = new DefaultMockServer(false); + proxyServer.start(); } @AfterAll static void afterAll() { - server.shutdown(); + proxyServer.shutdown(); } protected abstract HttpClient.Factory getHttpClientFactory(); @@ -50,7 +51,7 @@ static void afterAll() { @Test @DisplayName("Proxied HttpClient with basic authorization adds required headers to the request") protected void proxyConfigurationBasicAuthAddsRequiredHeaders() throws Exception { - server.expect().get().withPath("/").andReply(new ResponseProvider() { + proxyServer.expect().get().withPath("http://example.com/proxied").andReply(new ResponseProvider() { @Override public String getBody(RecordedRequest request) { @@ -74,17 +75,16 @@ public Headers getHeaders() { }).always(); // Given final HttpClient.Builder builder = getHttpClientFactory().newBuilder() - .proxyAddress(new InetSocketAddress("localhost", server.getPort())) + .proxyAddress(new InetSocketAddress("localhost", proxyServer.getPort())) .proxyAuthorization(basicCredentials("auth", "cred")); try (HttpClient client = builder.build()) { // When - client.sendAsync(client.newHttpRequestBuilder() - .uri(String.format("http://0.0.0.0:%s/not-found", server.getPort())).build(), String.class) + client.sendAsync(client.newHttpRequestBuilder().uri("http://example.com/proxied").build(), String.class) .get(10L, TimeUnit.SECONDS); // Then - assertThat(server.getLastRequest()) + assertThat(proxyServer.getLastRequest()) .extracting(RecordedRequest::getHeaders) - .returns("0.0.0.0:" + server.getPort(), h -> h.get("Host")) + .returns("example.com", h -> h.get("Host")) .returns("Basic YXV0aDpjcmVk", h -> h.get("Proxy-Authorization")); } } @@ -94,17 +94,16 @@ public Headers getHeaders() { protected void proxyConfigurationOtherAuthAddsRequiredHeaders() throws Exception { // Given final HttpClient.Builder builder = getHttpClientFactory().newBuilder() - .proxyAddress(new InetSocketAddress("localhost", server.getPort())) + .proxyAddress(new InetSocketAddress("localhost", proxyServer.getPort())) .proxyAuthorization("Other kind of auth"); try (HttpClient client = builder.build()) { // When - client.sendAsync(client.newHttpRequestBuilder() - .uri(String.format("http://0.0.0.0:%s/not-found", server.getPort())).build(), String.class) + client.sendAsync(client.newHttpRequestBuilder().uri("http://example.com/proxied").build(), String.class) .get(10L, TimeUnit.SECONDS); // Then - assertThat(server.getLastRequest()) + assertThat(proxyServer.getLastRequest()) .extracting(RecordedRequest::getHeaders) - .returns("0.0.0.0:" + server.getPort(), h -> h.get("Host")) + .returns("example.com", h -> h.get("Host")) .returns("Other kind of auth", h -> h.get("Proxy-Authorization")); } } diff --git a/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/AbstractHttpLoggingInterceptorTest.java b/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/AbstractHttpLoggingInterceptorTest.java index e95d3004e1f..6cdd5b33604 100644 --- a/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/AbstractHttpLoggingInterceptorTest.java +++ b/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/AbstractHttpLoggingInterceptorTest.java @@ -17,14 +17,14 @@ 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.fabric8.mockwebserver.http.Buffer; +import io.fabric8.mockwebserver.http.MockResponse; +import io.fabric8.mockwebserver.http.RecordedRequest; import io.fabric8.mockwebserver.internal.SimpleRequest; import io.fabric8.mockwebserver.utils.ResponseProviders; -import okhttp3.mockwebserver.MockResponse; -import okhttp3.mockwebserver.MockWebServer; -import okhttp3.mockwebserver.RecordedRequest; -import okio.Buffer; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; diff --git a/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/AbstractHttpPostTest.java b/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/AbstractHttpPostTest.java index 47a406f0314..50866bc85ce 100644 --- a/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/AbstractHttpPostTest.java +++ b/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/AbstractHttpPostTest.java @@ -16,7 +16,7 @@ package io.fabric8.kubernetes.client.http; import io.fabric8.mockwebserver.DefaultMockServer; -import okhttp3.mockwebserver.RecordedRequest; +import io.fabric8.mockwebserver.http.RecordedRequest; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; diff --git a/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/AbstractHttpPutTest.java b/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/AbstractHttpPutTest.java index e24dc11a4ca..9bcbae71877 100644 --- a/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/AbstractHttpPutTest.java +++ b/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/AbstractHttpPutTest.java @@ -16,7 +16,7 @@ package io.fabric8.kubernetes.client.http; import io.fabric8.mockwebserver.DefaultMockServer; -import okhttp3.mockwebserver.RecordedRequest; +import io.fabric8.mockwebserver.http.RecordedRequest; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; diff --git a/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/AbstractSimultaneousConnectionsTest.java b/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/AbstractSimultaneousConnectionsTest.java index 6b940fa4a63..06d3cb71854 100644 --- a/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/AbstractSimultaneousConnectionsTest.java +++ b/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/AbstractSimultaneousConnectionsTest.java @@ -18,11 +18,13 @@ import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; -import okhttp3.Protocol; -import okhttp3.Response; -import okhttp3.WebSocketListener; -import okhttp3.mockwebserver.MockResponse; -import okhttp3.mockwebserver.MockWebServer; +import io.fabric8.mockwebserver.MockWebServer; +import io.fabric8.mockwebserver.MockWebServerListener; +import io.fabric8.mockwebserver.http.MockResponse; +import io.fabric8.mockwebserver.http.RecordedHttpConnection; +import io.fabric8.mockwebserver.http.Response; +import io.fabric8.mockwebserver.http.WebSocketListener; +import io.fabric8.mockwebserver.vertx.Protocol; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -30,17 +32,11 @@ import org.junit.jupiter.api.condition.DisabledOnOs; import org.junit.jupiter.api.condition.OS; -import java.io.Closeable; import java.io.IOException; -import java.net.InetAddress; import java.net.InetSocketAddress; -import java.net.ServerSocket; -import java.net.Socket; -import java.net.SocketException; import java.net.URI; import java.util.Collection; import java.util.Collections; -import java.util.HashSet; import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; @@ -51,8 +47,6 @@ import java.util.concurrent.TimeUnit; import java.util.stream.IntStream; -import javax.net.ServerSocketFactory; - import static org.assertj.core.api.Assertions.assertThat; public abstract class AbstractSimultaneousConnectionsTest { @@ -63,7 +57,7 @@ public abstract class AbstractSimultaneousConnectionsTest { private static final int MAX_HTTP_1_CONNECTIONS = 2048; // Should be able to at least make 2048 private static final int MAX_HTTP_1_WS_CONNECTIONS = 1024; // Should be able to at least make 1024 - private RegisteredServerSocketFactory serverSocketFactory; + private RegisteredConnections registeredConnections; private MockWebServer mockWebServer; private ExecutorService httpExecutor; private HttpServer httpServer; @@ -72,9 +66,9 @@ public abstract class AbstractSimultaneousConnectionsTest { @BeforeEach void prepareServerAndBuilder() throws IOException { - serverSocketFactory = new RegisteredServerSocketFactory(); + registeredConnections = new RegisteredConnections(); mockWebServer = new MockWebServer(); - mockWebServer.setServerSocketFactory(serverSocketFactory); + mockWebServer.addListener(registeredConnections); httpExecutor = Executors.newCachedThreadPool(); httpServer = HttpServer.create(new InetSocketAddress(0), 0); httpServer.setExecutor(httpExecutor); @@ -84,8 +78,7 @@ void prepareServerAndBuilder() throws IOException { } @AfterEach - void stopServer() throws IOException { - serverSocketFactory.close(); + void stopServer() { mockWebServer.shutdown(); httpServer.stop(0); httpExecutor.shutdownNow(); @@ -93,7 +86,7 @@ void stopServer() throws IOException { protected abstract HttpClient.Factory getHttpClientFactory(); - private void withHttp1() throws IOException { + private void withHttp1() { mockWebServer.setProtocols(Collections.singletonList(Protocol.HTTP_1_1)); mockWebServer.start(); } @@ -150,17 +143,18 @@ public void http1WebSocketConnectionsBeforeUpgrade() throws Exception { @DisabledOnOs(OS.WINDOWS) public void http1WebSocketConnections() throws Exception { withHttp1(); - final Collection serverSockets = ConcurrentHashMap.newKeySet(); + final Collection serverSockets = ConcurrentHashMap.newKeySet(); final Collection clientSockets = ConcurrentHashMap.newKeySet(); final CyclicBarrier cyclicBarrier = new CyclicBarrier(2); final CountDownLatch latch = new CountDownLatch(MAX_HTTP_1_WS_CONNECTIONS); final MockResponse response = new MockResponse() .withWebSocketUpgrade(new WebSocketListener() { @Override - public void onOpen(okhttp3.WebSocket webSocket, Response response) { + public void onOpen(io.fabric8.mockwebserver.http.WebSocket webSocket, Response response) { try { cyclicBarrier.await(1, TimeUnit.SECONDS); } catch (Exception ignore) { + // Ignored } serverSockets.add(webSocket); webSocket.send("go on"); @@ -185,11 +179,11 @@ public void onMessage(WebSocket webSocket, String text) { assertThat(latch.await(60L, TimeUnit.SECONDS)).isTrue(); assertThat(serverSockets.size()) .isEqualTo(MAX_HTTP_1_WS_CONNECTIONS) - .isLessThanOrEqualTo((int) serverSocketFactory.activeConnections()); + .isLessThanOrEqualTo(registeredConnections.activeConnections()); // assertThat(clientSockets) // .hasSize(MAX_HTTP_1_WS_CONNECTIONS); } finally { - for (okhttp3.WebSocket socket : serverSockets) { + for (io.fabric8.mockwebserver.http.WebSocket socket : serverSockets) { socket.close(1000, "done"); } } @@ -222,7 +216,7 @@ private DelayedResponseHandler(int requestCount, HttpHandler handler) { } @Override - public void handle(HttpExchange exchange) throws IOException { + public void handle(HttpExchange exchange) { exchanges.add(exchange); await(); if (exchanges.size() == requestCount) { @@ -237,54 +231,26 @@ public final void await() { } catch (Exception ex) { throw new RuntimeException("Failed to await the barrier"); } - ; } } - private static class RegisteredServerSocketFactory extends ServerSocketFactory implements Closeable { - - private final Set connections = new HashSet<>(); + private static class RegisteredConnections implements MockWebServerListener { - final long activeConnections() { - return connections.stream().filter(Socket::isConnected).filter(s -> !s.isClosed()).count(); - } + private final Set connections = ConcurrentHashMap.newKeySet(); - @Override - public final void close() { - for (Socket socket : connections) { - try { - socket.close(); - } catch (IOException ignored) { - // ignored - } - } - } - - @Override - public ServerSocket createServerSocket() throws IOException { - return new ServerSocket() { - @Override - public Socket accept() throws IOException { - final Socket socket = super.accept(); - connections.add(socket); - return socket; - } - }; - } - - @Override - public ServerSocket createServerSocket(int port) throws IOException { - throw new SocketException("not implemented"); + final int activeConnections() { + return connections.size(); } @Override - public ServerSocket createServerSocket(int port, int backlog) throws IOException { - throw new SocketException("not implemented"); + public void onConnection(RecordedHttpConnection connection) { + connections.add(connection); + MockWebServerListener.super.onConnection(connection); } @Override - public ServerSocket createServerSocket(int port, int backlog, InetAddress ifAddress) throws IOException { - throw new SocketException("not implemented"); + public void onConnectionClosed(RecordedHttpConnection connection) { + connections.remove(connection); } } } diff --git a/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/internal/CertUtilsTest.java b/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/internal/CertUtilsTest.java index e296f287ead..06e00cda1ef 100644 --- a/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/internal/CertUtilsTest.java +++ b/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/internal/CertUtilsTest.java @@ -40,9 +40,9 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.junit.Assert.assertNotSame; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotSame; import static org.junit.jupiter.api.Assertions.assertTrue; class CertUtilsTest { diff --git a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/CreateOrReplaceResourceTest.java b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/CreateOrReplaceResourceTest.java index 6e6b4b69964..6d2aad04ac4 100644 --- a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/CreateOrReplaceResourceTest.java +++ b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/CreateOrReplaceResourceTest.java @@ -32,7 +32,7 @@ import io.fabric8.kubernetes.client.dsl.Resource; import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; -import okhttp3.mockwebserver.RecordedRequest; +import io.fabric8.mockwebserver.http.RecordedRequest; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/CustomResourceTest.java b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/CustomResourceTest.java index 4bb7cdd7e30..5e914c7ba8f 100644 --- a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/CustomResourceTest.java +++ b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/CustomResourceTest.java @@ -37,7 +37,7 @@ import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; import io.fabric8.kubernetes.client.utils.Serialization; import io.fabric8.kubernetes.client.utils.Utils; -import okhttp3.mockwebserver.RecordedRequest; +import io.fabric8.mockwebserver.http.RecordedRequest; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -385,6 +385,7 @@ public void eventReceived(Action action, GenericKubernetesResource resource) { @Override public void onClose(WatcherException cause) { + // NO OP } }); @@ -416,6 +417,7 @@ public void eventReceived(Action action, GenericKubernetesResource resource) { @Override public void onClose(WatcherException cause) { + // NO OP } }); @@ -450,6 +452,7 @@ public void eventReceived(Action action, GenericKubernetesResource resource) { @Override public void onClose(WatcherException cause) { + // NO OP } }); @@ -483,6 +486,7 @@ public void eventReceived(Action action, GenericKubernetesResource resource) { @Override public void onClose(WatcherException cause) { + // NO OP } }); @@ -520,6 +524,7 @@ public void eventReceived(Action action, GenericKubernetesResource resource) { @Override public void onClose(WatcherException cause) { + // NO OP } }); @@ -557,6 +562,7 @@ public void eventReceived(Action action, GenericKubernetesResource resource) { @Override public void onClose(WatcherException cause) { + // NO OP } }); @@ -567,7 +573,7 @@ public void onClose(WatcherException cause) { @Test @DisplayName("Should be able to test watch with Namespace and ListOptions provided") - void testWatchWithNamespaceAndListOptions() throws IOException, InterruptedException { + void testWatchWithNamespaceAndListOptions() throws InterruptedException { // Given server.expect().withPath( "/apis/test.fabric8.io/v1alpha1/namespaces/ns1/hellos?allowWatchBookmarks=true&resourceVersion=1003&timeoutSeconds=30&watch=true") @@ -594,6 +600,7 @@ public void eventReceived(Action action, GenericKubernetesResource resource) { @Override public void onClose(WatcherException cause) { + // NO OP } }); diff --git a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/DeploymentTest.java b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/DeploymentTest.java index c5bc30acc07..c2fc47e1fd5 100644 --- a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/DeploymentTest.java +++ b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/DeploymentTest.java @@ -38,7 +38,7 @@ import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; import io.fabric8.kubernetes.client.utils.Utils; -import okhttp3.mockwebserver.RecordedRequest; +import io.fabric8.mockwebserver.http.RecordedRequest; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/MixedCrudTest.java b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/MixedCrudTest.java index dedec302736..815c569f90b 100644 --- a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/MixedCrudTest.java +++ b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/MixedCrudTest.java @@ -24,11 +24,10 @@ import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; import io.fabric8.kubernetes.client.utils.Serialization; import io.fabric8.mockwebserver.Context; +import io.fabric8.mockwebserver.MockWebServer; import io.fabric8.mockwebserver.ServerRequest; import io.fabric8.mockwebserver.ServerResponse; -import okhttp3.mockwebserver.MockWebServer; import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -45,10 +44,6 @@ class MixedCrudTest { private KubernetesMockServer server; private KubernetesClient client; - @BeforeAll - static void beforeAll() { - } - @BeforeEach void setUp() { final Map> responses = new HashMap<>(); diff --git a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/PodCrudTest.java b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/PodCrudTest.java index d8e03086a59..a4451109dfa 100644 --- a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/PodCrudTest.java +++ b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/PodCrudTest.java @@ -26,7 +26,6 @@ import io.fabric8.kubernetes.client.WatcherException; import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; -import junit.framework.AssertionFailedError; import org.awaitility.Awaitility; import org.junit.jupiter.api.Test; @@ -34,6 +33,7 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; +import static org.junit.jupiter.api.AssertionFailureBuilder.assertionFailure; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -289,7 +289,7 @@ public void eventReceived(Action action, Pod resource) { addLatch.countDown(); break; default: - throw new AssertionFailedError(action.toString().concat(" isn't recognised.")); + throw assertionFailure().message(action.toString().concat(" isn't recognised.")).build(); } } diff --git a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/PropagationPolicyTest.java b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/PropagationPolicyTest.java index 1e8ed43c7ce..5edd83db5db 100644 --- a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/PropagationPolicyTest.java +++ b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/PropagationPolicyTest.java @@ -37,7 +37,7 @@ import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; import io.fabric8.kubernetes.client.utils.Utils; -import okhttp3.mockwebserver.RecordedRequest; +import io.fabric8.mockwebserver.http.RecordedRequest; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/RequestConfigTest.java b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/RequestConfigTest.java index 5b762884808..563c5175e95 100644 --- a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/RequestConfigTest.java +++ b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/RequestConfigTest.java @@ -21,7 +21,7 @@ import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; import io.fabric8.kubernetes.client.utils.ImpersonatorInterceptor; -import okhttp3.mockwebserver.RecordedRequest; +import io.fabric8.mockwebserver.http.RecordedRequest; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; diff --git a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/ResourceListTest.java b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/ResourceListTest.java index 3ef8f175765..68628f2ccce 100644 --- a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/ResourceListTest.java +++ b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/ResourceListTest.java @@ -37,7 +37,7 @@ import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; import io.fabric8.kubernetes.client.utils.Serialization; -import okhttp3.mockwebserver.RecordedRequest; +import io.fabric8.mockwebserver.http.RecordedRequest; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; diff --git a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/ResourceTest.java b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/ResourceTest.java index bbb3a7ef307..f069a1f8bbf 100644 --- a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/ResourceTest.java +++ b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/ResourceTest.java @@ -39,7 +39,7 @@ import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; import io.fabric8.kubernetes.client.utils.Serialization; import io.fabric8.kubernetes.client.utils.Utils; -import okhttp3.mockwebserver.RecordedRequest; +import io.fabric8.mockwebserver.http.RecordedRequest; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -285,7 +285,7 @@ public void eventReceived(Action action, Pod resource) { @Override public void onClose(WatcherException cause) { - + // NO OP } }); assertTrue(latch.await(5000, MILLISECONDS)); @@ -293,7 +293,7 @@ public void onClose(WatcherException cause) { } @Test - void testWaitUntilReady() throws InterruptedException { + void testWaitUntilReady() { Pod pod1 = new PodBuilder().withNewMetadata() .withName("pod1") .withResourceVersion("1") diff --git a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/ServiceTest.java b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/ServiceTest.java index a96e14c7d9d..5fb64b931d4 100644 --- a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/ServiceTest.java +++ b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/ServiceTest.java @@ -31,7 +31,7 @@ import io.fabric8.kubernetes.client.dsl.Resource; import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; -import okhttp3.mockwebserver.RecordedRequest; +import io.fabric8.mockwebserver.http.RecordedRequest; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -39,9 +39,9 @@ import java.util.Collections; import java.util.concurrent.TimeUnit; -import static junit.framework.TestCase.assertNotNull; -import static junit.framework.TestCase.assertTrue; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; @EnableKubernetesMockClient class ServiceTest { diff --git a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/StatefulSetTest.java b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/StatefulSetTest.java index 65704494d86..7f5b0c4d4fa 100644 --- a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/StatefulSetTest.java +++ b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/StatefulSetTest.java @@ -34,7 +34,7 @@ import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; import io.fabric8.kubernetes.client.utils.Utils; -import okhttp3.mockwebserver.RecordedRequest; +import io.fabric8.mockwebserver.http.RecordedRequest; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/TypedClusterScopeCustomResourceApiTest.java b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/TypedClusterScopeCustomResourceApiTest.java index 6a26d24ae8f..b1f1cdf9363 100644 --- a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/TypedClusterScopeCustomResourceApiTest.java +++ b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/TypedClusterScopeCustomResourceApiTest.java @@ -27,7 +27,7 @@ import io.fabric8.kubernetes.client.mock.crd.StarStatus; import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; -import okhttp3.mockwebserver.RecordedRequest; +import io.fabric8.mockwebserver.http.RecordedRequest; import org.junit.jupiter.api.Test; import java.net.HttpURLConnection; diff --git a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/TypedCustomResourceApiTest.java b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/TypedCustomResourceApiTest.java index addfaeb4b5d..cb2717f5c47 100644 --- a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/TypedCustomResourceApiTest.java +++ b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/TypedCustomResourceApiTest.java @@ -27,7 +27,7 @@ import io.fabric8.kubernetes.client.mock.crd.PodSetStatus; import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; -import okhttp3.mockwebserver.RecordedRequest; +import io.fabric8.mockwebserver.http.RecordedRequest; import org.junit.jupiter.api.Test; import java.net.HttpURLConnection; diff --git a/kubernetes-tests/src/test/java/io/fabric8/openshift/client/server/mock/AdaptTest.java b/kubernetes-tests/src/test/java/io/fabric8/openshift/client/server/mock/AdaptTest.java index d89746c7137..77de954ba0f 100644 --- a/kubernetes-tests/src/test/java/io/fabric8/openshift/client/server/mock/AdaptTest.java +++ b/kubernetes-tests/src/test/java/io/fabric8/openshift/client/server/mock/AdaptTest.java @@ -20,10 +20,10 @@ import io.fabric8.kubernetes.client.KubernetesClient; import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; +import io.fabric8.mockwebserver.http.Headers; +import io.fabric8.mockwebserver.http.RecordedRequest; import io.fabric8.mockwebserver.utils.ResponseProvider; import io.fabric8.openshift.client.OpenShiftClient; -import okhttp3.Headers; -import okhttp3.mockwebserver.RecordedRequest; import org.junit.jupiter.api.Test; import java.net.HttpURLConnection; @@ -64,7 +64,7 @@ void testCheckIfAvailableAPIGroupsContainOpenShiftOpenShift4() { .andReturn(HttpURLConnection.HTTP_OK, "{\"authorization_endpoint\":\"" + authorizationEndpoint + "\"}") .once(); server.expect().get().withPath("/oauth/authorize?response_type=token&client_id=openshift-challenging-client") - .andReply(new ResponseProvider() { + .andReply(new ResponseProvider<>() { @Override public Object getBody(RecordedRequest recordedRequest) { return null; @@ -85,6 +85,7 @@ public Headers getHeaders() { @Override public void setHeaders(Headers headers) { + // NO OP } }).once(); server.expect().withPath("/apis").andReturn(HttpURLConnection.HTTP_OK, new APIGroupListBuilder() diff --git a/log4j/src/test/java/io/fabric8/kubernetes/log4j/lookup/KubernetesLookupTest.java b/log4j/src/test/java/io/fabric8/kubernetes/log4j/lookup/KubernetesLookupTest.java index 0e734fa4108..6b4a816ec98 100644 --- a/log4j/src/test/java/io/fabric8/kubernetes/log4j/lookup/KubernetesLookupTest.java +++ b/log4j/src/test/java/io/fabric8/kubernetes/log4j/lookup/KubernetesLookupTest.java @@ -173,6 +173,7 @@ void containers_smoke_test(String containerName) throws Exception { @Test void initialize_works_with_mock_client() { + KubernetesLookup.clear(); final Pod pod = mockClient.pods().resource(createPod()).create(); final Namespace namespace = mockClient.namespaces().resource(createNamespace()).create(); final StrLookup lookup = new KubernetesLookup() { diff --git a/pom.xml b/pom.xml index 91955815c11..6acea8a0eb4 100644 --- a/pom.xml +++ b/pom.xml @@ -108,6 +108,7 @@ 4.0.23 + 4.13.2 5.11.2 3.26.3 4.2.2 @@ -761,11 +762,6 @@ okhttp ${okhttp.version} - - com.squareup.okhttp3 - mockwebserver - ${okhttp.version} - org.apache.commons commons-compress @@ -909,6 +905,12 @@ test + + junit + junit + ${junit4.version} + test + org.junit.jupiter junit-jupiter-api