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

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

%requires ../../../../qlib/Util.qm
%requires ../../../../qlib/QUnit.qm
%requires ../../../../qlib/Logger.qm
%requires ../../../../qlib/DataProvider
%requires ../../../../qlib/HttpClientDataProvider
%requires ../../../../qlib/HttpServerUtil.qm
%requires ../../../../qlib/HttpServer.qm

%exec-class HttpClientDataProviderTest

public class HttpClientDataProviderTest inherits QUnit::Test {
    constructor() : Test("HttpClientDataProvider Test", "1.0") {
        addTestCase("test", \httpClientDataProviderTest());

        # Return for compatibility with test harness that checks return value.
        set_return_value(main());
    }

    httpClientDataProviderTest() {
        HttpClientDataProvider parent({"url": "localhost", "headers": {"Test": "x"}});

        AbstractDataProvider prov = parent.getChildProviderEx("call");
        hash<DataProviderSummaryInfo> sinfo = prov.getSummaryInfo();
        assertEq("call", sinfo.name);
        assertEq(Type::String, sinfo.desc.type());
        assertEq("call", prov.getName());

        prov = parent.getChildProviderEx("delete");
        sinfo = prov.getSummaryInfo();
        assertEq("delete", sinfo.name);
        assertEq(Type::String, sinfo.desc.type());
        assertEq("delete", prov.getName());

        prov = parent.getChildProviderEx("get");
        sinfo = prov.getSummaryInfo();
        assertEq("get", sinfo.name);
        assertEq(Type::String, sinfo.desc.type());
        assertEq("get", prov.getName());

        prov = parent.getChildProviderEx("head");
        sinfo = prov.getSummaryInfo();
        assertEq("head", sinfo.name);
        assertEq(Type::String, sinfo.desc.type());
        assertEq("head", prov.getName());

        prov = parent.getChildProviderEx("options");
        sinfo = prov.getSummaryInfo();
        assertEq("options", sinfo.name);
        assertEq(Type::String, sinfo.desc.type());
        assertEq("options", prov.getName());

        prov = parent.getChildProviderEx("post");
        sinfo = prov.getSummaryInfo();
        assertEq("post", sinfo.name);
        assertEq(Type::String, sinfo.desc.type());
        assertEq("post", prov.getName());

        prov = parent.getChildProviderEx("put");
        sinfo = prov.getSummaryInfo();
        assertEq("put", sinfo.name);
        assertEq(Type::String, sinfo.desc.type());
        assertEq("put", prov.getName());

        prov = parent.getChildProviderEx("patch");
        sinfo = prov.getSummaryInfo();
        assertEq("patch", sinfo.name);
        assertEq(Type::String, sinfo.desc.type());
        assertEq("patch", prov.getName());

        parent = new HttpClientDataProvider({
            "url": "localhost",
            "pre_encoded_urls": True,
        });
        prov = parent.getChildProviderEx("call");
        sinfo = prov.getSummaryInfo();
        assertEq("call", sinfo.name);
        assertEq(Type::String, sinfo.desc.type());
        assertEq("call", prov.getName());
    }
}
