Skip to content

Commit f8fcaf3

Browse files
author
Keith Lustria
authored
Add option to disable DNS Resolver (helidon-io#6492)
1 parent aaf3264 commit f8fcaf3

4 files changed

Lines changed: 94 additions & 6 deletions

File tree

webclient/webclient/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@
9595
<artifactId>junit-jupiter-api</artifactId>
9696
<scope>test</scope>
9797
</dependency>
98+
<dependency>
99+
<artifactId>junit-jupiter-params</artifactId>
100+
<groupId>org.junit.jupiter</groupId>
101+
<scope>test</scope>
102+
</dependency>
98103
<dependency>
99104
<groupId>org.hamcrest</groupId>
100105
<artifactId>hamcrest-all</artifactId>

webclient/webclient/src/main/java/io/helidon/webclient/DnsResolverType.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022 Oracle and/or its affiliates.
2+
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,6 +29,11 @@ public enum DnsResolverType {
2929
/**
3030
* Round Robin DNS resolver.
3131
*/
32-
ROUND_ROBIN
32+
ROUND_ROBIN,
33+
34+
/**
35+
* No DNS resolver.
36+
*/
37+
NONE
3338

3439
}

webclient/webclient/src/main/java/io/helidon/webclient/WebClientRequestBuilderImpl.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2022 Oracle and/or its affiliates.
2+
* Copyright (c) 2020, 2023 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -76,6 +76,7 @@
7676
import io.netty.handler.codec.http.HttpMethod;
7777
import io.netty.handler.codec.http.HttpUtil;
7878
import io.netty.handler.codec.http.HttpVersion;
79+
import io.netty.resolver.NoopAddressResolverGroup;
7980
import io.netty.resolver.dns.DnsServerAddressStreamProviders;
8081
import io.netty.resolver.dns.RoundRobinDnsAddressResolverGroup;
8182
import io.netty.util.AsciiString;
@@ -586,9 +587,16 @@ private Single<WebClientResponse> invoke(Flow.Publisher<DataChunk> requestEntity
586587
.option(ChannelOption.SO_KEEPALIVE, keepAlive)
587588
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, (int) connectTimeout.toMillis());
588589

589-
if (dnsResolverType == DnsResolverType.ROUND_ROBIN) {
590-
bootstrap.resolver(new RoundRobinDnsAddressResolverGroup(NioDatagramChannel.class,
591-
DnsServerAddressStreamProviders.platformDefault()));
590+
switch (dnsResolverType) {
591+
case ROUND_ROBIN:
592+
bootstrap.resolver(new RoundRobinDnsAddressResolverGroup(NioDatagramChannel.class,
593+
DnsServerAddressStreamProviders.platformDefault()));
594+
break;
595+
case NONE:
596+
bootstrap.resolver(NoopAddressResolverGroup.INSTANCE);
597+
break;
598+
default:
599+
// Do nothing and default bootstrap resolver will be used
592600
}
593601

594602
ChannelFuture channelFuture = keepAlive
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2023 Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.helidon.webclient;
17+
18+
import java.util.concurrent.ExecutionException;
19+
import java.util.stream.Stream;
20+
21+
import org.junit.jupiter.params.ParameterizedTest;
22+
import org.junit.jupiter.params.provider.Arguments;
23+
import org.junit.jupiter.params.provider.EnumSource;
24+
import org.junit.jupiter.params.provider.MethodSource;
25+
26+
import static org.hamcrest.CoreMatchers.endsWith;
27+
import static org.hamcrest.MatcherAssert.assertThat;
28+
import static org.hamcrest.Matchers.is;
29+
import static org.junit.jupiter.api.Assertions.assertThrows;
30+
import static org.junit.jupiter.params.provider.Arguments.arguments;
31+
32+
/**
33+
* Unit test for {@link io.helidon.webclient.DnsResolverType}.
34+
*/
35+
class DnsResolverTest {
36+
@ParameterizedTest
37+
@EnumSource(DnsResolverType.class)
38+
void testConfig(DnsResolverType dnsResolverType) {
39+
WebClientConfiguration webConfig = WebClient.builder()
40+
.baseUri("http://localhost:80")
41+
.dnsResolverType(dnsResolverType)
42+
.configuration();
43+
assertThat(webConfig.dnsResolverType(), is(dnsResolverType));
44+
}
45+
46+
@ParameterizedTest
47+
@MethodSource("expectedExceptions")
48+
void testExpectedException(DnsResolverType dnsResolverType, String exeptionClass) {
49+
WebClient webClient = WebClient.builder()
50+
.baseUri("http://invalid.site.com")
51+
.dnsResolverType(dnsResolverType)
52+
.build();
53+
WebClientRequestBuilder webClientRequestBuilder = webClient.method("GET");
54+
ExecutionException exe = assertThrows(ExecutionException.class, () -> {
55+
webClientRequestBuilder.submit().get();
56+
});
57+
String dnsResolverExceptionClass = exe.getCause().getCause().getClass().getName();
58+
assertThat(dnsResolverExceptionClass, endsWith(exeptionClass));
59+
}
60+
61+
private static Stream<Arguments> expectedExceptions() {
62+
return Stream.of(
63+
arguments(DnsResolverType.NONE, "java.nio.channels.UnresolvedAddressException"),
64+
// This can either be io.netty.resolver.dns.DnsResolveContext$SearchDomainUnknownHostException
65+
// or java.net.UnknownHostException
66+
arguments(DnsResolverType.ROUND_ROBIN, "UnknownHostException"),
67+
arguments(DnsResolverType.DEFAULT, "java.net.UnknownHostException")
68+
);
69+
}
70+
}

0 commit comments

Comments
 (0)