Merge 0.10->trunk
[prosody.git] / tests / test_util_xmppstream.lua
1 function new(new_stream, _M)
2         local function test(xml, expect_success, ex)
3                 local stanzas = {};
4                 local session = { notopen = true };
5                 local callbacks = {
6                         stream_ns = "streamns";
7                         stream_tag = "stream";
8                         default_ns = "stanzans";
9                         streamopened = function (_session)
10                                 assert_equal(session, _session);
11                                 assert_equal(session.notopen, true);
12                                 _session.notopen = nil;
13                                 return true;
14                         end;
15                         handlestanza = function (_session, stanza)
16                                 assert_equal(session, _session);
17                                 assert_equal(_session.notopen, nil);
18                                 table.insert(stanzas, stanza);
19                         end;
20                         streamclosed = function (_session)
21                                 assert_equal(session, _session);
22                                 assert_equal(_session.notopen, nil);
23                                 _session.notopen = nil;
24                         end;
25                 }
26                 if type(ex) == "table" then
27                         for k, v in pairs(ex) do
28                                 if k ~= "_size_limit" then
29                                         callbacks[k] = v;
30                                 end
31                         end
32                 end
33                 local stream = new_stream(session, callbacks, size_limit);
34                 local ok, err = pcall(function ()
35                         assert(stream:feed(xml));
36                 end);
37
38                 if ok and type(expect_success) == "function" then
39                         expect_success(stanzas);
40                 end
41                 assert_equal(not not ok, not not expect_success, "Expected "..(expect_success and ("success ("..tostring(err)..")") or "failure"));
42         end
43
44         local function test_stanza(stanza, expect_success, ex)
45                 return test([[<stream:stream xmlns:stream="streamns" xmlns="stanzans">]]..stanza, expect_success, ex);
46         end
47
48         test([[<stream:stream xmlns:stream="streamns"/>]], true);
49         test([[<stream xmlns="streamns"/>]], true);
50
51         test([[<stream1 xmlns="streamns"/>]], false);
52         test([[<stream xmlns="streamns1"/>]], false);
53         test("<>", false);
54
55         test_stanza("<message/>", function (stanzas)
56                 assert_equal(#stanzas, 1);
57                 assert_equal(stanzas[1].name, "message");
58         end);
59         test_stanza("< message>>>>/>\n", false);
60
61         test_stanza([[<x xmlns:a="b">
62                 <y xmlns:a="c">
63                         <a:z/>
64                 </y>
65                 <a:z/>
66         </x>]], function (stanzas)
67                 assert_equal(#stanzas, 1);
68                 local s = stanzas[1];
69                 assert_equal(s.name, "x");
70                 assert_equal(#s.tags, 2);
71
72                 assert_equal(s.tags[1].name, "y");
73                 assert_equal(s.tags[1].attr.xmlns, nil);
74
75                 assert_equal(s.tags[1].tags[1].name, "z");
76                 assert_equal(s.tags[1].tags[1].attr.xmlns, "c");
77
78                 assert_equal(s.tags[2].name, "z");
79                 assert_equal(s.tags[2].attr.xmlns, "b");
80
81                 assert_equal(s.namespaces, nil);
82         end);
83 end