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

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

%requires ../../../../qlib/Util.qm
%requires ../../../../qlib/QUnit.qm
%requires ../../../../qlib/Mime.qm
%requires ../../../../qlib/HttpServerUtil.qm

%exec-class HttpServerUtilTest

public class HttpServerUtilTest inherits QUnit::Test {
    constructor() : Test("HttpServerUtilTest", "1.0") {
        addTestCase("funcs", \funcTests());

        # Return for compatibility with test harness that checks the process's return value
        set_return_value(main());
    }

    funcTests() {
        assertEq("https://[fe80::468a:5bff:fe86:43ee]:8011",
            http_get_url_from_bind("https://fe80::468a:5bff:fe86:43ee:8011"));
        assertEq("http://[::1]", http_get_url_from_bind("::", "::1"));
        assertEq("https://192.168.55.1:8011",
            http_get_url_from_bind("https://0.0.0.0:8011", "192.168.55.1"));
        assertEq("https://my-host:8011",
            http_get_url_from_bind("https://[::]:8011", "my-host"));

        assertEq("passenger", http_mask_data("passenger"));
        assertEq("pass: <masked>...", http_mask_data("pass: 1"));
        assertEq("\"pass\": <masked>...", http_mask_data("\"pass\": 1"));
        assertEq("password: <masked>...", http_mask_data("password: 1"));
        assertEq("\"password\": <masked>...", http_mask_data("\"password\": 1"));
        assertEq("passwrd: 1", http_mask_data("passwrd: 1"));
        assertEq("\"passwrd\": 1", http_mask_data("\"passwrd\": 1"));
        assertEq("passwd: 1", http_mask_data("passwd: 1"));
        assertEq("\"passwd\": 1", http_mask_data("\"passwd\": 1"));
        assertEq("passenger: 1", http_mask_data("passenger: 1"));
        assertEq("\"passenger\": 1", http_mask_data("\"passenger\": 1"));

        assertEq("pass : <masked>...", http_mask_data("pass : 1"));
        assertEq("\"pass\" : <masked>...", http_mask_data("\"pass\" : 1"));
        assertEq("password : <masked>...", http_mask_data("password : 1"));
        assertEq("\"password\" : <masked>...", http_mask_data("\"password\" : 1"));
        assertEq("passwrd : 1", http_mask_data("passwrd : 1"));
        assertEq("\"passwrd\" : 1", http_mask_data("\"passwrd\" : 1"));
        assertEq("passwd : 1", http_mask_data("passwd : 1"));
        assertEq("\"passwd\" : 1", http_mask_data("\"passwd\" : 1"));
        assertEq("passenger : 1", http_mask_data("passenger : 1"));
        assertEq("\"passenger\" : 1", http_mask_data("\"passenger\" : 1"));

        assertEq("pass= <masked>...", http_mask_data("pass= 1"));
        assertEq("\"pass\"= <masked>...", http_mask_data("\"pass\"= 1"));
        assertEq("password= <masked>...", http_mask_data("password= 1"));
        assertEq("\"password\"= <masked>...", http_mask_data("\"password\"= 1"));
        assertEq("passwrd= 1", http_mask_data("passwrd= 1"));
        assertEq("\"passwrd\"= 1", http_mask_data("\"passwrd\"= 1"));
        assertEq("passwd= 1", http_mask_data("passwd= 1"));
        assertEq("\"passwd\"= 1", http_mask_data("\"passwd\"= 1"));
        assertEq("passenger= 1", http_mask_data("passenger= 1"));
        assertEq("\"passenger\"= 1", http_mask_data("\"passenger\"= 1"));

        assertEq("pass = <masked>...", http_mask_data("pass = 1"));
        assertEq("\"pass\" = <masked>...", http_mask_data("\"pass\" = 1"));
        assertEq("password = <masked>...", http_mask_data("password = 1"));
        assertEq("\"password\" = <masked>...", http_mask_data("\"password\" = 1"));
        assertEq("passwrd = 1", http_mask_data("passwrd = 1"));
        assertEq("\"passwrd\" = 1", http_mask_data("\"passwrd\" = 1"));
        assertEq("passwd = 1", http_mask_data("passwd = 1"));
        assertEq("\"passwd\" = 1", http_mask_data("\"passwd\" = 1"));
        assertEq("passenger = 1", http_mask_data("passenger = 1"));
        assertEq("\"passenger\" = 1", http_mask_data("\"passenger\" = 1"));
    }
}
