diff options
author | Waqas Hussain <waqas20@gmail.com> | 2017-09-15 17:07:57 -0400 |
---|---|---|
committer | Waqas Hussain <waqas20@gmail.com> | 2017-09-15 17:07:57 -0400 |
commit | f9ead37d008e58daf1e3263298cfd4a1ab54d09c (patch) | |
tree | b5cd2d09f0ee7e1e9e13feec26defe4f7d9f5003 /spec/util_xmppstream_spec.lua | |
parent | 2547ccfe3d26fbf57e0ecddf5cfd5d21377c3633 (diff) | |
download | prosody-f9ead37d008e58daf1e3263298cfd4a1ab54d09c.tar.gz prosody-f9ead37d008e58daf1e3263298cfd4a1ab54d09c.zip |
Port tests to the `busted` test runner
Diffstat (limited to 'spec/util_xmppstream_spec.lua')
-rw-r--r-- | spec/util_xmppstream_spec.lua | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/spec/util_xmppstream_spec.lua b/spec/util_xmppstream_spec.lua new file mode 100644 index 00000000..f03a806e --- /dev/null +++ b/spec/util_xmppstream_spec.lua @@ -0,0 +1,90 @@ + +local xmppstream = require "util.xmppstream"; + +describe("util.xmppstream", function() + describe("#new()", function() + it("should work", function() + 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.are.equal(session, _session); + assert.are.equal(session.notopen, true); + _session.notopen = nil; + return true; + end; + handlestanza = function (_session, stanza) + assert.are.equal(session, _session); + assert.are.equal(_session.notopen, nil); + table.insert(stanzas, stanza); + end; + streamclosed = function (_session) + assert.are.equal(session, _session); + assert.are.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 = xmppstream.new(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.are.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.are.equal(#stanzas, 1); + assert.are.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.are.equal(#stanzas, 1); + local s = stanzas[1]; + assert.are.equal(s.name, "x"); + assert.are.equal(#s.tags, 2); + + assert.are.equal(s.tags[1].name, "y"); + assert.are.equal(s.tags[1].attr.xmlns, nil); + + assert.are.equal(s.tags[1].tags[1].name, "z"); + assert.are.equal(s.tags[1].tags[1].attr.xmlns, "c"); + + assert.are.equal(s.tags[2].name, "z"); + assert.are.equal(s.tags[2].attr.xmlns, "b"); + + assert.are.equal(s.namespaces, nil); + end); + end); + end); +end); |