Skip to content

Commit

Permalink
Merge pull request #776 from Vlatombe/ipv6-follow-up
Browse files Browse the repository at this point in the history
IPV6 follow-up
  • Loading branch information
jglick committed Jun 3, 2024
2 parents dd51025 + 06fe714 commit 29bbb2e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/main/java/org/jvnet/hudson/test/RealJenkinsRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public final class RealJenkinsRule implements TestRule {

private int timeout = Integer.getInteger("jenkins.test.timeout", new DisableOnDebug(null).isDebugging() ? 0 : 600);

private String host = httpListenAddress;
private String host = "localhost";

Process proc;

Expand Down Expand Up @@ -292,13 +292,13 @@ public RealJenkinsRule withTimeout(int timeout) {

/**
* Sets a custom host name for the Jenkins root URL.
* <p>By default, this is the same as the {@link #httpListenAddress}.
* But you may wish to set it to something else that resolves to the loopback address,
* such as {@code some-id.127.0.0.1.nip.io}.
* <p>By default, this is just {@code localhost}.
* But you may wish to set it to something else that resolves to localhost,
* such as {@code some-id.localtest.me}.
* This is particularly useful when running multiple copies of Jenkins (and/or other services) in one test case,
* since browser cookies are sensitive to host but not port and so otherwise {@link HttpServletRequest#getSession}
* might accidentally be shared across otherwise distinct services.
* <p>Calling this method does <em>not</em> change the fact that Jenkins will be configured to listen only on the loopback address for security reasons
* <p>Calling this method does <em>not</em> change the fact that Jenkins will be configured to listen only on localhost for security reasons
* (so others in the same network cannot access your system under test, especially if it lacks authentication).
*/
public RealJenkinsRule withHost(String host) {
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/org/jvnet/hudson/test/RealJenkinsRuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.arrayContainingInAnyOrder;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertEquals;
Expand All @@ -53,6 +54,8 @@
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.HttpURLConnection;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.concurrent.atomic.AtomicReference;
Expand Down Expand Up @@ -92,6 +95,14 @@ private static void _smokes(JenkinsRule r) throws Throwable {
assertEquals(rr.getUrl().toExternalForm(), rr.runRemotely(RealJenkinsRuleTest::_getJenkinsUrlFromRemote));
}

@Test public void ipv6() throws Throwable {
// Use -Djava.net.preferIPv6Addresses=true if dualstack
assumeThat(InetAddress.getLoopbackAddress(), instanceOf(Inet6Address.class));
rr.withHost("::1").startJenkins();
var externalForm = rr.getUrl().toExternalForm();
assertEquals(externalForm, rr.runRemotely(RealJenkinsRuleTest::_getJenkinsUrlFromRemote));
}

@Test public void testThrowsException() {
assertThat(assertThrows(RealJenkinsRule.StepException.class, () -> rr.then(RealJenkinsRuleTest::throwsException)).getMessage(),
containsString("IllegalStateException: something is wrong"));
Expand Down

0 comments on commit 29bbb2e

Please sign in to comment.