diff --git a/webclient/api/src/main/java/io/helidon/webclient/spi/WebClientService.java b/webclient/api/src/main/java/io/helidon/webclient/spi/WebClientService.java index a70c510208a..0425d794e50 100644 --- a/webclient/api/src/main/java/io/helidon/webclient/spi/WebClientService.java +++ b/webclient/api/src/main/java/io/helidon/webclient/spi/WebClientService.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Oracle and/or its affiliates. + * Copyright (c) 2023, 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. @@ -29,7 +29,7 @@ public interface WebClientService extends NamedService { @Override default String name() { - return "@default"; + return type(); } @Override diff --git a/webclient/metrics/src/main/java/io/helidon/webclient/metrics/WebClientMetric.java b/webclient/metrics/src/main/java/io/helidon/webclient/metrics/WebClientMetric.java index 3c445653459..b39bf5aae35 100644 --- a/webclient/metrics/src/main/java/io/helidon/webclient/metrics/WebClientMetric.java +++ b/webclient/metrics/src/main/java/io/helidon/webclient/metrics/WebClientMetric.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2023 Oracle and/or its affiliates. + * Copyright (c) 2020, 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. @@ -53,6 +53,11 @@ static Builder builder(WebClientMetricType clientMetricType) { return new Builder(clientMetricType); } + @Override + public String type() { + return "metrics"; + } + MeterRegistry meterRegistry() { return registry; } diff --git a/webclient/security/src/main/java/io/helidon/webclient/security/WebClientSecurity.java b/webclient/security/src/main/java/io/helidon/webclient/security/WebClientSecurity.java index ea43dc34a8f..6efe6ad3eac 100644 --- a/webclient/security/src/main/java/io/helidon/webclient/security/WebClientSecurity.java +++ b/webclient/security/src/main/java/io/helidon/webclient/security/WebClientSecurity.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Oracle and/or its affiliates. + * Copyright (c) 2023, 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. @@ -82,6 +82,11 @@ public static WebClientSecurity create(Security security) { return new WebClientSecurity(security); } + @Override + public String type() { + return "security"; + } + @Override public WebClientServiceResponse handle(Chain chain, WebClientServiceRequest request) { if ("true".equalsIgnoreCase(request.properties().get(OutboundConfig.PROPERTY_DISABLE_OUTBOUND))) { diff --git a/webclient/tests/webclient/src/test/java/io/helidon/webclient/tests/WebClientServiceTest.java b/webclient/tests/webclient/src/test/java/io/helidon/webclient/tests/WebClientServiceTest.java new file mode 100644 index 00000000000..410320b76ad --- /dev/null +++ b/webclient/tests/webclient/src/test/java/io/helidon/webclient/tests/WebClientServiceTest.java @@ -0,0 +1,92 @@ +/* + * 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 java.util.List; + +import io.helidon.security.Security; +import io.helidon.webclient.http1.Http1Client; +import io.helidon.webclient.http1.Http1ClientConfig; +import io.helidon.webclient.security.WebClientSecurity; + +import org.junit.jupiter.api.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; + +public class WebClientServiceTest { + + @Test + public void noServiceRegistered() { + Http1Client client = Http1Client.builder() + .servicesDiscoverServices(false) + .build(); + Http1ClientConfig config = client.prototype(); + + assertThat(config.services(), is(empty())); + } + + @Test + public void noServiceAutomaticallyDiscovered() { + Security security = Security.builder().build(); + WebClientSecurity webClientSecurity = WebClientSecurity.create(security); + + Http1Client client = Http1Client.builder() + .servicesDiscoverServices(false) + .addService(webClientSecurity) + .build(); + Http1ClientConfig config = client.prototype(); + + assertThat(config.services(), is(List.of(webClientSecurity))); + } + + @Test + public void servicesFound() { + Http1Client client = Http1Client.builder().build(); + Http1ClientConfig config = client.prototype(); + + assertThat(config.services(), is(not(empty()))); + } + + + @Test + public void servicesNotDuplicated() { + Http1Client client = Http1Client.builder().build(); + Http1ClientConfig config = client.prototype(); + + assertThat(config.services(), is(not(empty()))); + + int numberOfServicesFound = config.services().size(); + + Security security = Security.builder().build(); + WebClientSecurity webClientSecurity = WebClientSecurity.create(security); + + client = Http1Client.builder() + .addService(webClientSecurity) + .build(); + config = client.prototype(); + + //The number of services has to match. otherwise there has been duplication + assertThat(config.services().size(), is(numberOfServicesFound)); + assertThat(config.services(), hasItem(webClientSecurity)); + } + +} diff --git a/webclient/tracing/src/main/java/io/helidon/webclient/tracing/WebClientTracing.java b/webclient/tracing/src/main/java/io/helidon/webclient/tracing/WebClientTracing.java index c16b578c6b0..0f0d6a65f2c 100644 --- a/webclient/tracing/src/main/java/io/helidon/webclient/tracing/WebClientTracing.java +++ b/webclient/tracing/src/main/java/io/helidon/webclient/tracing/WebClientTracing.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Oracle and/or its affiliates. + * Copyright (c) 2023, 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. @@ -75,6 +75,11 @@ public static WebClientService create(Tracer tracer) { return new WebClientTracing(tracer); } + @Override + public String type() { + return "tracing"; + } + @Override public WebClientServiceResponse handle(Chain chain, WebClientServiceRequest request) { String method = request.method().text();