Skip to content

Commit abe92eb

Browse files
committed
Protect Inet test against "helpful" DNS resolvers
Update `InetAddressFormatterTests` to ensure that DNS resolvers that return a "help" page for missing domains don't cause the build to fail. Closes gh-11897
1 parent 70c0d6a commit abe92eb

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/convert/InetAddressFormatterTests.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,32 +52,43 @@ public InetAddressFormatterTests(String name, ConversionService conversionServic
5252
@Test
5353
public void convertFromInetAddressToStringShouldConvert()
5454
throws UnknownHostException {
55-
assumeResolves("example.com");
55+
assumeResolves("example.com", true);
5656
InetAddress address = InetAddress.getByName("example.com");
5757
String converted = this.conversionService.convert(address, String.class);
5858
assertThat(converted).isEqualTo(address.getHostAddress());
5959
}
6060

6161
@Test
6262
public void convertFromStringToInetAddressShouldConvert() {
63-
assumeResolves("example.com");
63+
assumeResolves("example.com", true);
6464
InetAddress converted = this.conversionService.convert("example.com",
6565
InetAddress.class);
6666
assertThat(converted.toString()).startsWith("example.com");
6767
}
6868

6969
@Test
7070
public void convertFromStringToInetAddressWhenHostDoesNotExistShouldThrowException() {
71+
String missingDomain = "ireallydontexist.example.com";
72+
assumeResolves(missingDomain, false);
7173
this.thrown.expect(ConversionFailedException.class);
72-
this.conversionService.convert("ireallydontexist.example.com", InetAddress.class);
74+
this.conversionService.convert(missingDomain, InetAddress.class);
7375
}
7476

75-
private void assumeResolves(String host) {
77+
private void assumeResolves(String host, boolean expectedToResolve) {
78+
boolean resolved = isResolvable(host);
79+
if (resolved != expectedToResolve) {
80+
throw new AssumptionViolatedException(
81+
"Host " + host + " resolved " + resolved);
82+
}
83+
}
84+
85+
private boolean isResolvable(String host) {
7686
try {
7787
InetAddress.getByName(host);
88+
return true;
7889
}
7990
catch (UnknownHostException ex) {
80-
throw new AssumptionViolatedException("Host " + host + " not resolvable", ex);
91+
return false;
8192
}
8293
}
8394

0 commit comments

Comments
 (0)