Skip to content

Commit 984e79f

Browse files
committed
Make NetworkAddressRulesAdheringDnsResolver testable
By using composition rather than inheritance
1 parent 92d7793 commit 984e79f

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/main/java/com/github/tomakehurst/wiremock/http/NetworkAddressRulesAdheringDnsResolver.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,21 @@
2020
import java.net.InetAddress;
2121
import java.net.UnknownHostException;
2222
import java.util.stream.Stream;
23+
import org.apache.hc.client5.http.DnsResolver;
2324
import org.apache.hc.client5.http.SystemDefaultDnsResolver;
2425

25-
public class NetworkAddressRulesAdheringDnsResolver extends SystemDefaultDnsResolver {
26+
public class NetworkAddressRulesAdheringDnsResolver implements DnsResolver {
2627

28+
private final DnsResolver delegate;
2729
private final NetworkAddressRules networkAddressRules;
2830

2931
public NetworkAddressRulesAdheringDnsResolver(NetworkAddressRules networkAddressRules) {
32+
this(SystemDefaultDnsResolver.INSTANCE, networkAddressRules);
33+
}
34+
35+
public NetworkAddressRulesAdheringDnsResolver(
36+
DnsResolver delegate, NetworkAddressRules networkAddressRules) {
37+
this.delegate = delegate;
3038
this.networkAddressRules = networkAddressRules;
3139
}
3240

@@ -36,12 +44,17 @@ public InetAddress[] resolve(String host) throws UnknownHostException {
3644
throw new ProhibitedNetworkAddressException();
3745
}
3846

39-
final InetAddress[] resolved = super.resolve(host);
47+
final InetAddress[] resolved = delegate.resolve(host);
4048
if (Stream.of(resolved)
4149
.anyMatch(address -> !networkAddressRules.isAllowed(address.getHostAddress()))) {
4250
throw new ProhibitedNetworkAddressException();
4351
}
4452

4553
return resolved;
4654
}
55+
56+
@Override
57+
public String resolveCanonicalHostname(String host) throws UnknownHostException {
58+
return delegate.resolveCanonicalHostname(host);
59+
}
4760
}

0 commit comments

Comments
 (0)