|
| 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