Skip to content

Commit 908eb0e

Browse files
committed
test: use only 127.0.0.1 for loopback (microsoft#978)
1 parent 0608243 commit 908eb0e

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

driver-bundle/src/test/java/com/microsoft/playwright/TestInstall.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import org.junit.jupiter.api.*;
2222
import org.junit.jupiter.api.io.TempDir;
2323

24+
import java.io.IOException;
25+
import java.net.ServerSocket;
2426
import java.nio.file.Files;
2527
import java.nio.file.Path;
2628
import java.util.Collections;
@@ -34,6 +36,23 @@
3436
import static org.junit.jupiter.api.Assertions.assertTrue;
3537

3638
public class TestInstall {
39+
private static boolean isPortAvailable(int port) {
40+
try (ServerSocket ignored = new ServerSocket(port)) {
41+
return true;
42+
} catch (IOException ignored) {
43+
return false;
44+
}
45+
}
46+
47+
private static int unusedPort() {
48+
for (int i = 10000; i < 11000; i++) {
49+
if (isPortAvailable(i)) {
50+
return i;
51+
}
52+
}
53+
throw new RuntimeException("Cannot find unused local port");
54+
}
55+
3756
@BeforeEach
3857
void clearSystemProperties() {
3958
// Clear system property to ensure that the driver is loaded from jar.
@@ -47,7 +66,10 @@ void clearSystemProperties() {
4766
@Tags({@Tag("isolated"), @Tag("driverThrowTest")})
4867
void shouldThrowWhenBrowserPathIsInvalid(@TempDir Path tmpDir) {
4968
Map<String,String> env = new HashMap<>();
50-
env.put("PLAYWRIGHT_DOWNLOAD_HOST", "https://127.0.0.127");
69+
70+
// On macOS we can only use 127.0.0.1, so pick unused port instead.
71+
// https://superuser.com/questions/458875/how-do-you-get-loopback-addresses-other-than-127-0-0-1-to-work-on-os-x
72+
env.put("PLAYWRIGHT_DOWNLOAD_HOST", "https://127.0.0.1:" + unusedPort());
5173
// Make sure the browsers are not installed yet by pointing at an empty dir.
5274
env.put("PLAYWRIGHT_BROWSERS_PATH", tmpDir.toString());
5375
env.put("PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD", "false");

0 commit comments

Comments
 (0)