class ExtensionWebSocketClient {

    protected String validifyUrl(String url) {
        if (url == null) {
            throw new IllegalArgumentException("Must give a valid URL to connect to the websocket");
        }

        boolean usesSSL = true;
        // Ensure prepended by wss:// and not http:// or https://
        if (url.startsWith("http://")) {
            url = url.substring("http://".length());
            usesSSL = false;
        }
        else if (url.startsWith("https://")) {
            url = url.substring("https://".length());
        }
        if (!url.startsWith("ws://") && !url.startsWith("wss://")) {
            String prefix = "wss://";
            if (!usesSSL) {
                prefix = "ws://";
            }
            url = prefix + url;
        }
        
        // Ensure it ends with /api/v{version number}/wsock/websocket
        if (!url.matches(".*/api/v[0-9]+/wsock/websocket")) {
         // Sometimes generic urls end with a '/' already, so we only want to add one if it does not already exist
            if (!url.endsWith("/")) { 
                url = url + "/";
            }
            url = url + "api/v1/wsock/websocket";
        }
        
        return url;
    }

}

class TestExtensionWebSocketClient  {

     OpenExtensionWebSocketClient client;
     String srcName;
     String queryAddress;
     FalseWebSocket socket;

    @Test
    public void testValidifyUrl() {

        String url = "ws://cba.com/api/v1/wsock/websocket";
        url = client.validifyUrl(url);
        assert url.equals("ws://cba.com/api/v1/wsock/websocket");
        
        url = "http://prod.vantiq.com/api/v1/wsock/websocket";
        url = client.validifyUrl(url);
        assert url.equals("ws://prod.vantiq.com/api/v1/wsock/websocket");

        url = "http://prod.vantiq.com/";
        url = client.validifyUrl(url);
        assert url.equals("ws://prod.vantiq.com/api/v1/wsock/websocket");

        url = "http://prod.vantiq.com/api/v/wsock/websocket";
        url = client.validifyUrl(url);
        assert url.equals("ws://prod.vantiq.com/api/v/wsock/websocket/api/v1/wsock/websocket");

        url = "http://prod.vantiq.com/api/v47/wsock/websocket";
        url = client.validifyUrl(url);
        assert url.equals("ws://prod.vantiq.com/api/v47/wsock/websocket");
        
        url = "https://dev.vantiq.com";
        url = client.validifyUrl(url);
        assert url.equals("wss://dev.vantiq.com/api/v1/wsock/websocket");
        
        url = "https://dev.vantiq.com/";
        url = client.validifyUrl(url);
        assert url.equals("wss://dev.vantiq.com/api/v1/wsock/websocket");
        
        url = "dev.vantiq.com";
        url = client.validifyUrl(url);
        assert url.equals("wss://dev.vantiq.com/api/v1/wsock/websocket");

        url = "https://prod.vantiq.com/api/v1/wsock/websocket";
        url = client.validifyUrl(url);
        assert url.equals("wss://prod.vantiq.com/api/v1/wsock/websocket");

        url = "https://prod.vantiq.com/";
        url = client.validifyUrl(url);
        assert url.equals("wss://prod.vantiq.com/api/v1/wsock/websocket");

        url = "https://prod.vantiq.com/api/v/wsock/websocket";
        url = client.validifyUrl(url);
        assert url.equals("wss://prod.vantiq.com/api/v/wsock/websocket/api/v1/wsock/websocket");

        url = "https://prod.vantiq.com/api/v47/wsock/websocket";
        url = client.validifyUrl(url);
        assert url.equals("wss://prod.vantiq.com/api/v47/wsock/websocket");
    }

}