Skip to content

WebClientService duplication fix #8224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -29,7 +29,7 @@
public interface WebClientService extends NamedService {
@Override
default String name() {
return "@default";
return type();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -53,6 +53,11 @@ static Builder builder(WebClientMetricType clientMetricType) {
return new Builder(clientMetricType);
}

@Override
public String type() {
return "metrics";
}

MeterRegistry meterRegistry() {
return registry;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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))) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}

}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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();
Expand Down