2 function core_process_stanza(core_process_stanza)
3 local s2sout_session = { to_host = "remotehost", from_host = "localhost", type = "s2sout" }
4 local local_host_session = { host = "localhost", type = "local" }
5 local local_user_session = { username = "user", host = "localhost", resource = "resource", full_jid = "user@localhost/resource", type = "c2s" }
7 ["localhost"] = local_host_session;
10 -- Test message routing
11 local function test_message_full_jid()
12 local env = testlib_new_env();
13 local msg = stanza.stanza("message", { to = "user@localhost/resource", type = "chat" }):tag("body"):text("Hello world");
17 function env.core_route_stanza(p_origin, p_stanza)
18 assert_equal(p_origin, local_user_session, "origin of routed stanza is not correct");
19 assert_equal(p_stanza, msg, "routed stanza is not correct one: "..p_stanza:pretty_print());
23 setfenv(core_process_stanza, env);
24 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
25 assert_equal(target_routed, true, "stanza was not routed successfully");
28 local function test_message_bare_jid()
29 local env = testlib_new_env();
30 local msg = stanza.stanza("message", { to = "user@localhost", type = "chat" }):tag("body"):text("Hello world");
34 function env.core_route_stanza(p_origin, p_stanza)
35 assert_equal(p_origin, local_user_session, "origin of routed stanza is not correct");
36 assert_equal(p_stanza, msg, "routed stanza is not correct one: "..p_stanza:pretty_print());
40 setfenv(core_process_stanza, env);
41 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
42 assert_equal(target_routed, true, "stanza was not routed successfully");
45 local function test_message_no_to()
46 local env = testlib_new_env();
47 local msg = stanza.stanza("message", { type = "chat" }):tag("body"):text("Hello world");
51 function env.core_route_stanza(p_origin, p_stanza)
54 function env.core_handle_stanza(p_origin, p_stanza)
55 assert_equal(p_origin, local_user_session, "origin of handled stanza is not correct");
56 assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
57 target_handled = true;
60 setfenv(core_process_stanza, env);
61 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
62 assert_equal(target_handled, true, "stanza was not handled successfully");
65 local function test_message_to_remote_bare()
66 local env = testlib_new_env();
67 local msg = stanza.stanza("message", { to = "user@remotehost", type = "chat" }):tag("body"):text("Hello world");
71 function env.core_route_stanza(p_origin, p_stanza)
72 assert_equal(p_origin, local_user_session, "origin of handled stanza is not correct");
73 assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
78 setfenv(core_process_stanza, env);
79 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
80 assert_equal(target_routed, true, "stanza was not routed successfully");
83 local function test_message_to_remote_server()
84 local env = testlib_new_env();
85 local msg = stanza.stanza("message", { to = "remotehost", type = "chat" }):tag("body"):text("Hello world");
89 function env.core_route_stanza(p_origin, p_stanza)
90 assert_equal(p_origin, local_user_session, "origin of handled stanza is not correct");
91 assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
96 setfenv(core_process_stanza, env);
97 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
98 assert_equal(target_routed, true, "stanza was not routed successfully");
104 local function test_iq_to_remote_server()
105 local env = testlib_new_env();
106 local msg = stanza.stanza("iq", { to = "remotehost", type = "chat" }):tag("body"):text("Hello world");
110 function env.core_route_stanza(p_origin, p_stanza)
111 assert_equal(p_origin, local_user_session, "origin of handled stanza is not correct");
112 assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
113 target_routed = true;
116 function env.core_handle_stanza(p_origin, p_stanza)
121 setfenv(core_process_stanza, env);
122 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
123 assert_equal(target_routed, true, "stanza was not routed successfully");
126 runtest(test_message_full_jid, "Messages with full JID destinations get routed");
127 runtest(test_message_bare_jid, "Messages with bare JID destinations get routed");
128 runtest(test_message_no_to, "Messages with no destination are handled by the server");
129 runtest(test_message_to_remote_bare, "Messages to a remote user are routed by the server");
130 runtest(test_message_to_remote_server, "Messages to a remote server's JID are routed");
132 runtest(test_iq_to_remote_server, "iq to a remote server's JID are routed");