From 0f6d2d3ed07db60d3667580738e915f28345340d Mon Sep 17 00:00:00 2001 From: Jorge Bescos Gascon Date: Tue, 24 Sep 2024 12:03:58 +0200 Subject: [PATCH 1/2] Make ClientRequestBase mock friendly Signed-off-by: Jorge Bescos Gascon --- .../java/io/helidon/webclient/api/ClientRequestBase.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/webclient/api/src/main/java/io/helidon/webclient/api/ClientRequestBase.java b/webclient/api/src/main/java/io/helidon/webclient/api/ClientRequestBase.java index bdd3f053203..d1cec37df6e 100644 --- a/webclient/api/src/main/java/io/helidon/webclient/api/ClientRequestBase.java +++ b/webclient/api/src/main/java/io/helidon/webclient/api/ClientRequestBase.java @@ -262,7 +262,7 @@ public R request() { } @Override - public final R submit(Object entity) { + public R submit(Object entity) { if (!(entity instanceof byte[] bytes && bytes.length == 0)) { rejectHeadWithEntity(); } @@ -271,7 +271,7 @@ public final R submit(Object entity) { } @Override - public final R outputStream(OutputStreamHandler outputStreamConsumer) { + public R outputStream(OutputStreamHandler outputStreamConsumer) { rejectHeadWithEntity(); additionalHeaders(); return doOutputStream(outputStreamConsumer); @@ -481,7 +481,7 @@ private String resolvePathParams(String path) { } private void rejectHeadWithEntity() { - if (this.method.equals(Method.HEAD)) { + if (Method.HEAD.equals(this.method)) { throw new IllegalArgumentException("Payload in method '" + Method.HEAD + "' has no defined semantics"); } } From 0e9e9a26203a1b833b6344cb06b3a397f0fb9266 Mon Sep 17 00:00:00 2001 From: Jorge Bescos Gascon Date: Wed, 25 Sep 2024 07:27:12 +0200 Subject: [PATCH 2/2] Add test Signed-off-by: Jorge Bescos Gascon --- webclient/tests/webclient/pom.xml | 5 ++ .../webclient/tests/WebClientMockTest.java | 59 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 webclient/tests/webclient/src/test/java/io/helidon/webclient/tests/WebClientMockTest.java diff --git a/webclient/tests/webclient/pom.xml b/webclient/tests/webclient/pom.xml index 855dc1e41b4..7010f4cdd2d 100644 --- a/webclient/tests/webclient/pom.xml +++ b/webclient/tests/webclient/pom.xml @@ -124,5 +124,10 @@ helidon-logging-jul test + + org.mockito + mockito-core + test + diff --git a/webclient/tests/webclient/src/test/java/io/helidon/webclient/tests/WebClientMockTest.java b/webclient/tests/webclient/src/test/java/io/helidon/webclient/tests/WebClientMockTest.java new file mode 100644 index 00000000000..a560ff22b55 --- /dev/null +++ b/webclient/tests/webclient/src/test/java/io/helidon/webclient/tests/WebClientMockTest.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. + * + * 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.helidon.webclient.tests; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import io.helidon.http.HeaderName; +import io.helidon.http.HeaderNames; +import io.helidon.http.Status; +import io.helidon.webclient.api.HttpClientRequest; +import io.helidon.webclient.api.HttpClientResponse; +import io.helidon.webclient.api.WebClient; + +import org.junit.jupiter.api.Test; + +/** + * Tests WebClientMockTest. + */ +class WebClientMockTest { + + @Test + public void useMock() { + WebClient webClient = mockWebClient(Status.OK_200); + HttpClientResponse response = webClient.post("http://test.com") + .header(HeaderNames.AUTHORIZATION, "Bearer asdsadsad") + .submit(new Object()); + assertEquals(Status.OK_200, response.status()); + } + + private WebClient mockWebClient(Status status) { + WebClient webClient = mock(WebClient.class); + HttpClientRequest request = mock(HttpClientRequest.class); + HttpClientResponse response = mock(HttpClientResponse.class); + doReturn(request).when(webClient).post(anyString()); + doReturn(request).when(request).header(any(HeaderName.class), anyString()); + doReturn(response).when(request).submit(any(Object.class)); + when(response.status()).thenReturn(status); + return webClient; + } + +}