From: Matthew Wild Date: Sat, 5 Mar 2016 23:42:01 +0000 (+0000) Subject: tests: Add basic tests for util.xml and util.xmppstream X-Git-Url: https://git.enpas.org/?a=commitdiff_plain;h=36ada27cf0ed14431934faf78a514f46032de2e1;p=prosody.git tests: Add basic tests for util.xml and util.xmppstream --- diff --git a/tests/test.lua b/tests/test.lua index 4172b363..9ab2cad8 100644 --- a/tests/test.lua +++ b/tests/test.lua @@ -25,6 +25,8 @@ function run_all_tests() dotest "util.throttle" dotest "util.uuid" dotest "util.random" + dotest "util.xml" + dotest "util.xmppstream" dosingletest("test_sasl.lua", "latin1toutf8"); dosingletest("test_utf8.lua", "valid"); diff --git a/tests/test_util_xml.lua b/tests/test_util_xml.lua new file mode 100644 index 00000000..ba44da19 --- /dev/null +++ b/tests/test_util_xml.lua @@ -0,0 +1,12 @@ +function parse(parse) + local x = +[[ + + + + + +]] + local stanza = parse(x); + assert_equal(stanza.tags[2].attr.xmlns, "b"); +end diff --git a/tests/test_util_xmppstream.lua b/tests/test_util_xmppstream.lua new file mode 100644 index 00000000..791cf999 --- /dev/null +++ b/tests/test_util_xmppstream.lua @@ -0,0 +1,83 @@ +function new(new_stream, _M) + local function test(xml, expect_success, ex) + local stanzas = {}; + local session = { notopen = true }; + local callbacks = { + stream_ns = "streamns"; + stream_tag = "stream"; + default_ns = "stanzans"; + streamopened = function (_session) + assert_equal(session, _session); + assert_equal(session.notopen, true); + _session.notopen = nil; + return true; + end; + handlestanza = function (_session, stanza) + assert_equal(session, _session); + assert_equal(_session.notopen, nil); + table.insert(stanzas, stanza); + end; + streamclosed = function (_session) + assert_equal(session, _session); + assert_equal(_session.notopen, nil); + _session.notopen = nil; + end; + } + if type(ex) == "table" then + for k, v in pairs(ex) do + if k ~= "_size_limit" then + callbacks[k] = v; + end + end + end + local stream = new_stream(session, callbacks, size_limit); + local ok, err = pcall(function () + assert(stream:feed(xml)); + end); + + if ok and type(expect_success) == "function" then + expect_success(stanzas); + end + assert_equal(not not ok, not not expect_success, "Expected "..(expect_success and ("success ("..tostring(err)..")") or "failure")); + end + + local function test_stanza(stanza, expect_success, ex) + return test([[]]..stanza, expect_success, ex); + end + + test([[]], true); + test([[]], true); + + test([[]], false); + test([[]], false); + test("<>", false); + + test_stanza("", function (stanzas) + assert_equal(#stanzas, 1); + assert_equal(stanzas[1].name, "message"); + end); + test_stanza("< message>>>>/>\n", false); + + test_stanza([[ + + + + + ]], function (stanzas) + assert_equal(#stanzas, 1); + local s = stanzas[1]; + assert_equal(s.name, "x"); + assert_equal(#s.tags, 2); + + assert_equal(s.tags[1].name, "y"); + assert_equal(s.tags[1].attr.xmlns, nil); + + assert_equal(s.tags[1].tags[1].name, "z"); + assert_equal(s.tags[1].tags[1].attr.xmlns, "c"); + + assert_equal(s.tags[2].name, "z"); + assert_equal(s.tags[2].attr.xmlns, "b"); + + assert_equal(s.namespaces, nil); + end); +end