Software /
code /
prosody
Annotate
tests/test_util_xmppstream.lua @ 7844:316f5166eedb
mod_mam: Find out which party is the user and which is the 'with' earlier
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 19 Nov 2016 18:28:28 +0100 |
parent | 7241:5e7797822f19 |
rev | line source |
---|---|
7241
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 function new(new_stream, _M) |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 local function test(xml, expect_success, ex) |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 local stanzas = {}; |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 local session = { notopen = true }; |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 local callbacks = { |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 stream_ns = "streamns"; |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 stream_tag = "stream"; |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 default_ns = "stanzans"; |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 streamopened = function (_session) |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 assert_equal(session, _session); |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 assert_equal(session.notopen, true); |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 _session.notopen = nil; |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 return true; |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 end; |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 handlestanza = function (_session, stanza) |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 assert_equal(session, _session); |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 assert_equal(_session.notopen, nil); |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 table.insert(stanzas, stanza); |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 end; |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 streamclosed = function (_session) |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 assert_equal(session, _session); |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 assert_equal(_session.notopen, nil); |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 _session.notopen = nil; |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 end; |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 } |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 if type(ex) == "table" then |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 for k, v in pairs(ex) do |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 if k ~= "_size_limit" then |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 callbacks[k] = v; |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 end |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 end |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 end |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 local stream = new_stream(session, callbacks, size_limit); |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 local ok, err = pcall(function () |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 assert(stream:feed(xml)); |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 end); |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 if ok and type(expect_success) == "function" then |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 expect_success(stanzas); |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 end |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 assert_equal(not not ok, not not expect_success, "Expected "..(expect_success and ("success ("..tostring(err)..")") or "failure")); |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 end |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 local function test_stanza(stanza, expect_success, ex) |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 return test([[<stream:stream xmlns:stream="streamns" xmlns="stanzans">]]..stanza, expect_success, ex); |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 end |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 test([[<stream:stream xmlns:stream="streamns"/>]], true); |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 test([[<stream xmlns="streamns"/>]], true); |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 test([[<stream1 xmlns="streamns"/>]], false); |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 test([[<stream xmlns="streamns1"/>]], false); |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 test("<>", false); |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 test_stanza("<message/>", function (stanzas) |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 assert_equal(#stanzas, 1); |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 assert_equal(stanzas[1].name, "message"); |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 end); |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 test_stanza("< message>>>>/>\n", false); |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 test_stanza([[<x xmlns:a="b"> |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 <y xmlns:a="c"> |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 <a:z/> |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 </y> |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 <a:z/> |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 </x>]], function (stanzas) |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 assert_equal(#stanzas, 1); |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 local s = stanzas[1]; |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 assert_equal(s.name, "x"); |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 assert_equal(#s.tags, 2); |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 assert_equal(s.tags[1].name, "y"); |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 assert_equal(s.tags[1].attr.xmlns, nil); |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 assert_equal(s.tags[1].tags[1].name, "z"); |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 assert_equal(s.tags[1].tags[1].attr.xmlns, "c"); |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 assert_equal(s.tags[2].name, "z"); |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 assert_equal(s.tags[2].attr.xmlns, "b"); |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 assert_equal(s.namespaces, nil); |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 end); |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 end |