public static URL socketToUrl(InetSocketAddress socketAddress) {
        String hostString = socketAddress.getHostString();
        // If the hostString is an IPv6 address, it needs to be enclosed in square brackets
        // at the beginning and end.
        if (socketAddress.getAddress() != null
                && socketAddress.getAddress() instanceof Inet6Address
                && hostString.equals(socketAddress.getAddress().getHostAddress())) {
            hostString = "[" + hostString + "]";
        }
        String hostPort = hostString + ":" + socketAddress.getPort();

        return validateHostPortString(hostPort);
    }
@Test
    void testSocketToUrl() throws MalformedURLException {
        InetSocketAddress socketAddress = new InetSocketAddress("foo.com", 8080);
        URL expectedResult = new URL("http://foo.com:8080");

        assertThat(socketToUrl(socketAddress)).isEqualTo(expectedResult);
    }