-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP Introduce custom host name resolver
to avoid localhost URLs with WireMock/TestContainers. CAUTION: It still runs into test problems, and perhaps causes problems elsewhere.
- Loading branch information
Showing
2 changed files
with
76 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...tyCheck-core/src/test/java/org/aim42/htmlsanitycheck/test/dns/CustomHostNameResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.aim42.htmlsanitycheck.test.dns; | ||
|
||
import java.lang.reflect.Field; | ||
import java.net.InetAddress; | ||
import java.net.UnknownHostException; | ||
|
||
public class CustomHostNameResolver { | ||
|
||
public static final String WIREMOCK_HOST = "my.custom.mocked.host"; | ||
private static Object originalResolver; | ||
|
||
static { | ||
try { | ||
Field implField = InetAddress.class.getDeclaredField("impl"); | ||
implField.setAccessible(true); | ||
originalResolver = implField.get(null); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Failed to setup fallback DNS resolver", e); | ||
} | ||
} | ||
|
||
public InetAddress[] resolve(String hostname) throws UnknownHostException { | ||
// Custom DNS resolution logic | ||
if (WIREMOCK_HOST.equals(hostname)) { | ||
return new InetAddress[]{InetAddress.getByAddress("localhost", new byte[]{127, 0, 0, 1})}; | ||
} | ||
// Fallback to original resolver using reflection | ||
try { | ||
return (InetAddress[]) originalResolver.getClass() | ||
.getMethod("lookupAllHostAddr", String.class) | ||
.invoke(originalResolver, hostname); | ||
} catch (Exception e) { | ||
throw new UnknownHostException("Failed to resolve hostname: " + hostname); | ||
} | ||
} | ||
} |