Repository: jenkinsci/jenkins
File: core/src/test/java/hudson/util/FormValidationTest.java (Java)

Code before:
        assertTrue(ok_error.renderHtml().contains(error.getMessage()));

        final FormValidation warning_error = aggregate(warning, error);
        assertEquals(FormValidation.Kind.ERROR, warning_error.kind);
        assertTrue(warning_error.renderHtml().contains(error.getMessage()));
        assertTrue(warning_error.renderHtml().contains(warning.getMessage()));
    }

    private FormValidation aggregate(FormValidation... fvs) {
        return FormValidation.aggregate(Arrays.asList(fvs));
    }

    @Test
    void formValidationException() {
        FormValidation fv = FormValidation.error(new Exception("<html"), "Message<html");
        assertThat(fv.renderHtml(), not(containsString("<html")));
    }

    @Test
    void testUrlCheck() throws IOException {
        HttpServer server = createAndStartMockServer();

        try {
            String uri = "http://localhost:" + server.getAddress().getPort() + "/";

            FormValidation.URLCheck urlCheck = new FormValidation.URLCheck() {
                @Override
                protected FormValidation check() throws IOException {
                    try {
                        if (findText(open(URI.create(uri)), "Jenkins")) {
                            return FormValidation.ok();
                        } else {
                            return FormValidation.error("This is a valid URI but it does not look like Jenkins");
                        }
                    } catch (IOException e) {
                        return handleIOException(uri, e);
                    }
                }
            };
            assertThat(urlCheck.check(), is(FormValidation.ok()));

        } finally {
            server.stop(0);
        }
    }


    private static HttpServer createAndStartMockServer() throws IOException {
        HttpServer server = HttpServer.create(new InetSocketAddress("localhost", 0), 0);

        server.createContext("/", exchange -> {

Review comment (bug):
The test builds the URI using the literal host `localhost` even though the server is bound via name resolution as well. On some systems (IPv4/IPv6 preference differences), this can cause the server to bind to one loopback address while the client resolves `localhost` to another, leading to flaky connection failures. Bind the server explicitly to `InetAddress.getLoopbackAddress()` (or `new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)`) and/or construct the URI host from `server.getAddress().getHostString()` to ensure the client connects to the bound address.

Code after:
        assertTrue(ok_error.renderHtml().contains(ok.getMessage()));
        assertTrue(ok_error.renderHtml().contains(error.getMessage()));

        final FormValidation warning_error = aggregate(warning, error);
        assertEquals(FormValidation.Kind.ERROR, warning_error.kind);
        assertTrue(warning_error.renderHtml().contains(error.getMessage()));
        assertTrue(warning_error.renderHtml().contains(warning.getMessage()));
    }

    private FormValidation aggregate(FormValidation... fvs) {
        return FormValidation.aggregate(Arrays.asList(fvs));
    }

    @Test
    void formValidationException() {
        FormValidation fv = FormValidation.error(new Exception("<html"), "Message<html");
        assertThat(fv.renderHtml(), not(containsString("<html")));
    }

    @Test
    void testUrlCheck() throws IOException {
        HttpServer server = createAndStartMockServer();

        try {
            String uri = "http://localhost:" + server.getAddress().getPort() + "/";

            FormValidation.URLCheck urlCheck = new FormValidation.URLCheck() {
                @Override
                protected FormValidation check() throws IOException {
                    try {
                        if (findText(open(URI.create(uri)), "Jenkins")) {
                            return FormValidation.ok();
                        } else {
                            return FormValidation.error("This is a valid URI but it does not look like Jenkins");
                        }
                    } catch (IOException e) {
                        return handleIOException(uri, e);
                    }
                }
            };
            assertThat(urlCheck.check(), is(FormValidation.ok()));

        } finally {
            server.stop(0);
        }
    }


    private static HttpServer createAndStartMockServer() throws IOException {
        HttpServer server = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);