diff options
author | Matthew Wild <mwild1@gmail.com> | 2016-03-05 23:44:21 +0000 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2016-03-05 23:44:21 +0000 |
commit | 0d5b89d026386e62529faf8f264be3a43fb3c81e (patch) | |
tree | d6290d436593437443a2409fce2f36b64275a8c3 /tests/test_util_xmppstream.lua | |
parent | a1bcc0b591ed004d4414127aa314e550f04bfe7d (diff) | |
parent | 61a708fcd1a3c9a7a6f88787e47365ebd20fad60 (diff) | |
download | prosody-0d5b89d026386e62529faf8f264be3a43fb3c81e.tar.gz prosody-0d5b89d026386e62529faf8f264be3a43fb3c81e.zip |
Merge 0.10->trunk
Diffstat (limited to 'tests/test_util_xmppstream.lua')
-rw-r--r-- | tests/test_util_xmppstream.lua | 83 |
1 files changed, 83 insertions, 0 deletions
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([[<stream:stream xmlns:stream="streamns" xmlns="stanzans">]]..stanza, expect_success, ex); + end + + test([[<stream:stream xmlns:stream="streamns"/>]], true); + test([[<stream xmlns="streamns"/>]], true); + + test([[<stream1 xmlns="streamns"/>]], false); + test([[<stream xmlns="streamns1"/>]], false); + test("<>", false); + + test_stanza("<message/>", function (stanzas) + assert_equal(#stanzas, 1); + assert_equal(stanzas[1].name, "message"); + end); + test_stanza("< message>>>>/>\n", false); + + test_stanza([[<x xmlns:a="b"> + <y xmlns:a="c"> + <a:z/> + </y> + <a:z/> + </x>]], 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 |