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

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

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

%exec-class Pop3ClientTest

public class Pop3ClientTest inherits QUnit::Test {
    constructor() : Test("Pop3ClientTest", "1.0") {
        addTestCase("Pop3Client tests", \pop3ClientTests());
        addTestCase("connection tests", \connectionTests());
        set_return_value(main());
    }

    pop3ClientTests() {
        Pop3Client p3c("pop3://user:pass@host.com");
        assertEq("host.com:110", p3c.getTarget());
        p3c = new Pop3Client("user:pass@x:23");
        assertEq("x:23", p3c.getTarget());
        p3c = new Pop3Client("Pop3://user:pass@x1:23");
        assertEq("x1:23", p3c.getTarget());
        p3c = new Pop3Client("Pop3://user:pass@x1");
        assertEq("x1:110", p3c.getTarget());
        p3c = new Pop3Client("Pop3://user:pass@socket=%2Ftmp%2Fsock");
        assertEq("/tmp/sock", p3c.getTarget());
        assertThrows("POP3-URL-ERROR", sub () { new Pop3Client("pop3x://host:23");});
        assertThrows("POP3-URL-ERROR", sub () { new Pop3Client("scheme://host");});
    }

    connectionTests() {
        string url = "pop3s://user:pass@localhost:8099";
        Pop3Connection conn("test", "test", url);
        Pop3Client client = conn.get(False);
        assertEq("localhost:8099", client.getTarget());

        # issue #3696: test connection serialization
        Pop3Connection conn2 = Serializable::deserialize(conn.serialize());
        assertEq(conn.url, conn2.url);

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