#!/usr/bin/env qore
# -*- mode: qore; indent-tabs-mode: nil -*-

%new-style
%enable-all-warnings
%require-types
%strict-args

%requires ../../../../qlib/ConnectionProvider
%requires ../../../../qlib/TelnetClient.qm
%requires ../../../../qlib/QUnit.qm

%exec-class TelnetClientTest

public class TelnetClientTest inherits QUnit::Test {
    constructor() : Test("TelnetClientTest", "1.0") {
        addTestCase("TelnetClient tests", \telnetClientTests());
        addTestCase("connection tests", \connectionTests());
        set_return_value(main());
    }

    telnetClientTests() {
        TelnetClient tc("x");
        assertEq("x:23", tc.getTarget());
        tc = new TelnetClient("x:23");
        assertEq("x:23", tc.getTarget());
        tc = new TelnetClient("telnet://x1:23");
        assertEq("x1:23", tc.getTarget());
        tc = new TelnetClient("telnet://x1");
        assertEq("x1:23", tc.getTarget());
        tc = new TelnetClient("telnet://socket=%2Ftmp%2Fsock");
        assertEq("/tmp/sock", tc.getTarget());
        assertThrows("INVALID-TARGET", sub () { new TelnetClient("telnets://host:23");});
        assertThrows("INVALID-TARGET", sub () { new TelnetClient("scheme://host");});
    }

    connectionTests() {
        string url = "telnet://localhost:8099";
        TelnetConnection conn("test", "test", url);
        TelnetClient client = conn.get(False);
        assertEq("localhost:8099", client.getTarget());

        conn = new TelnetConnection("test", "test", url, {"monitor": False});
        client = conn.get(False);
        assertEq("localhost:8099", client.getTarget());
    }
}
