Software /
code /
prosody
Comparison
spec/util_stanza_spec.lua @ 11120:b2331f3dfeea
Merge 0.11->trunk
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 30 Sep 2020 09:50:33 +0100 |
parent | 11088:1f84d0e4d0c4 |
child | 11206:f051394762ff |
comparison
equal
deleted
inserted
replaced
11119:68df52bf08d5 | 11120:b2331f3dfeea |
---|---|
1 | 1 |
2 local st = require "util.stanza"; | 2 local st = require "util.stanza"; |
3 local errors = require "util.error"; | |
3 | 4 |
4 describe("util.stanza", function() | 5 describe("util.stanza", function() |
5 describe("#preserialize()", function() | 6 describe("#preserialize()", function() |
6 it("should work", function() | 7 it("should work", function() |
7 local stanza = st.stanza("message", { type = "chat" }):text_tag("body", "Hello"); | 8 local stanza = st.stanza("message", { type = "chat" }):text_tag("body", "Hello"); |
93 end); | 94 end); |
94 end); | 95 end); |
95 | 96 |
96 describe("#iq()", function() | 97 describe("#iq()", function() |
97 it("should create an iq stanza", function() | 98 it("should create an iq stanza", function() |
98 local i = st.iq({ id = "foo" }); | 99 local i = st.iq({ type = "get", id = "foo" }); |
99 assert.are.equal("iq", i.name); | 100 assert.are.equal("iq", i.name); |
100 assert.are.equal("foo", i.attr.id); | 101 assert.are.equal("foo", i.attr.id); |
101 end); | 102 assert.are.equal("get", i.attr.type); |
103 end); | |
104 | |
105 it("should reject stanzas with no attributes", function () | |
106 assert.has.error_match(function () | |
107 st.iq(); | |
108 end, "attributes"); | |
109 end); | |
110 | |
102 | 111 |
103 it("should reject stanzas with no id", function () | 112 it("should reject stanzas with no id", function () |
104 assert.has.error_match(function () | 113 assert.has.error_match(function () |
105 st.iq(); | 114 st.iq({ type = "get" }); |
106 end, "id attribute"); | 115 end, "id attribute"); |
107 | 116 end); |
108 assert.has.error_match(function () | 117 |
109 st.iq({ foo = "bar" }); | 118 it("should reject stanzas with no type", function () |
110 end, "id attribute"); | 119 assert.has.error_match(function () |
120 st.iq({ id = "foo" }); | |
121 end, "type attribute"); | |
122 | |
111 end); | 123 end); |
112 end); | 124 end); |
113 | 125 |
114 describe("#presence()", function () | 126 describe("#presence()", function () |
115 it("should work", function() | 127 it("should work", function() |
157 assert.are.equal(r.attr.to, s.attr.from); | 169 assert.are.equal(r.attr.to, s.attr.from); |
158 assert.are.equal(r.attr.from, s.attr.to); | 170 assert.are.equal(r.attr.from, s.attr.to); |
159 assert.are.equal(r.attr.type, "result"); | 171 assert.are.equal(r.attr.type, "result"); |
160 assert.are.equal(#r.tags, 0, "A reply should not include children of the original stanza"); | 172 assert.are.equal(#r.tags, 0, "A reply should not include children of the original stanza"); |
161 end); | 173 end); |
174 | |
175 it("should reject not-stanzas", function () | |
176 assert.has.error_match(function () | |
177 st.reply(not "a stanza"); | |
178 end, "expected stanza"); | |
179 end); | |
180 | |
181 it("should reject not-stanzas", function () | |
182 assert.has.error_match(function () | |
183 st.reply({name="x"}); | |
184 end, "expected stanza"); | |
185 end); | |
186 | |
162 end); | 187 end); |
163 | 188 |
164 describe("#error_reply()", function() | 189 describe("#error_reply()", function() |
165 it("should work for <s>", function() | 190 it("should work for <s>", function() |
166 -- Test stanza | 191 -- Test stanza |
167 local s = st.stanza("s", { to = "touser", from = "fromuser", id = "123" }) | 192 local s = st.stanza("s", { to = "touser", from = "fromuser", id = "123" }) |
168 :tag("child1"); | 193 :tag("child1"); |
169 -- Make reply stanza | 194 -- Make reply stanza |
170 local r = st.error_reply(s, "cancel", "service-unavailable"); | 195 local r = st.error_reply(s, "cancel", "service-unavailable", nil, "host"); |
171 assert.are.equal(r.name, s.name); | 196 assert.are.equal(r.name, s.name); |
172 assert.are.equal(r.id, s.id); | 197 assert.are.equal(r.id, s.id); |
173 assert.are.equal(r.attr.to, s.attr.from); | 198 assert.are.equal(r.attr.to, s.attr.from); |
174 assert.are.equal(r.attr.from, s.attr.to); | 199 assert.are.equal(r.attr.from, s.attr.to); |
175 assert.are.equal(#r.tags, 1); | 200 assert.are.equal(#r.tags, 1); |
176 assert.are.equal(r.tags[1].tags[1].name, "service-unavailable"); | 201 assert.are.equal(r.tags[1].tags[1].name, "service-unavailable"); |
202 assert.are.equal(r.tags[1].attr.by, "host"); | |
177 end); | 203 end); |
178 | 204 |
179 it("should work for <iq get>", function() | 205 it("should work for <iq get>", function() |
180 -- Test stanza | 206 -- Test stanza |
181 local s = st.stanza("iq", { to = "touser", from = "fromuser", id = "123", type = "get" }) | 207 local s = st.stanza("iq", { to = "touser", from = "fromuser", id = "123", type = "get" }) |
188 assert.are.equal(r.attr.from, s.attr.to); | 214 assert.are.equal(r.attr.from, s.attr.to); |
189 assert.are.equal(r.attr.type, "error"); | 215 assert.are.equal(r.attr.type, "error"); |
190 assert.are.equal(#r.tags, 1); | 216 assert.are.equal(#r.tags, 1); |
191 assert.are.equal(r.tags[1].tags[1].name, "service-unavailable"); | 217 assert.are.equal(r.tags[1].tags[1].name, "service-unavailable"); |
192 end); | 218 end); |
193 end); | 219 |
220 it("should reject not-stanzas", function () | |
221 assert.has.error_match(function () | |
222 st.error_reply(not "a stanza", "modify", "bad-request"); | |
223 end, "expected stanza"); | |
224 end); | |
225 | |
226 it("should reject stanzas of type error", function () | |
227 assert.has.error_match(function () | |
228 st.error_reply(st.message({type="error"}), "cancel", "conflict"); | |
229 end, "got stanza of type error"); | |
230 assert.has.error_match(function () | |
231 st.error_reply(st.error_reply(st.message({type="chat"}), "modify", "forbidden"), "cancel", "service-unavailable"); | |
232 end, "got stanza of type error"); | |
233 end); | |
234 | |
235 describe("util.error integration", function () | |
236 it("should accept util.error objects", function () | |
237 local s = st.message({ to = "touser", from = "fromuser", id = "123", type = "chat" }, "Hello"); | |
238 local e = errors.new({ type = "modify", condition = "not-acceptable", text = "Bork bork bork" }, { by = "this.test" }); | |
239 local r = st.error_reply(s, e); | |
240 | |
241 assert.are.equal(r.name, s.name); | |
242 assert.are.equal(r.id, s.id); | |
243 assert.are.equal(r.attr.to, s.attr.from); | |
244 assert.are.equal(r.attr.from, s.attr.to); | |
245 assert.are.equal(r.attr.type, "error"); | |
246 assert.are.equal(r.tags[1].name, "error"); | |
247 assert.are.equal(r.tags[1].attr.type, e.type); | |
248 assert.are.equal(r.tags[1].tags[1].name, e.condition); | |
249 assert.are.equal(r.tags[1].tags[2]:get_text(), e.text); | |
250 assert.are.equal("this.test", r.tags[1].attr.by); | |
251 end); | |
252 | |
253 it("should accept util.error objects with an URI", function () | |
254 local s = st.message({ to = "touser", from = "fromuser", id = "123", type = "chat" }, "Hello"); | |
255 local gone = errors.new({ condition = "gone", extra = { uri = "file:///dev/null" } }) | |
256 local gonner = st.error_reply(s, gone); | |
257 assert.are.equal("gone", gonner.tags[1].tags[1].name); | |
258 assert.are.equal("file:///dev/null", gonner.tags[1].tags[1][1]); | |
259 end); | |
260 | |
261 it("should accept util.error objects with application specific error", function () | |
262 local s = st.message({ to = "touser", from = "fromuser", id = "123", type = "chat" }, "Hello"); | |
263 local e = errors.new({ condition = "internal-server-error", text = "Namespaced thing happened", | |
264 extra = {namespace="xmpp:example.test", condition="this-happened"} }) | |
265 local r = st.error_reply(s, e); | |
266 assert.are.equal("xmpp:example.test", r.tags[1].tags[3].attr.xmlns); | |
267 assert.are.equal("this-happened", r.tags[1].tags[3].name); | |
268 | |
269 local e2 = errors.new({ condition = "internal-server-error", text = "Namespaced thing happened", | |
270 extra = {tag=st.stanza("that-happened", { xmlns = "xmpp:example.test", ["another-attribute"] = "here" })} }) | |
271 local r2 = st.error_reply(s, e2); | |
272 assert.are.equal("xmpp:example.test", r2.tags[1].tags[3].attr.xmlns); | |
273 assert.are.equal("that-happened", r2.tags[1].tags[3].name); | |
274 assert.are.equal("here", r2.tags[1].tags[3].attr["another-attribute"]); | |
275 end); | |
276 end); | |
277 end); | |
278 | |
279 describe("#get_error()", function () | |
280 describe("basics", function () | |
281 local s = st.message(); | |
282 local e = st.error_reply(s, "cancel", "not-acceptable", "UNACCEPTABLE!!!! ONE MILLION YEARS DUNGEON!") | |
283 :tag("dungeon", { xmlns = "urn:uuid:c9026187-5b05-4e70-b265-c3b6338a7d0f", period="1000000years"}); | |
284 local typ, cond, text, extra = e:get_error(); | |
285 assert.equal("cancel", typ); | |
286 assert.equal("not-acceptable", cond); | |
287 assert.equal("UNACCEPTABLE!!!! ONE MILLION YEARS DUNGEON!", text); | |
288 assert.not_nil(extra) | |
289 end) | |
290 end) | |
194 | 291 |
195 describe("should reject #invalid", function () | 292 describe("should reject #invalid", function () |
196 local invalid_names = { | 293 local invalid_names = { |
197 ["empty string"] = "", ["characters"] = "<>"; | 294 ["empty string"] = "", ["characters"] = "<>"; |
198 } | 295 } |
368 assert.has_error(function () | 465 assert.has_error(function () |
369 st.clone("this is not a stanza"); | 466 st.clone("this is not a stanza"); |
370 end); | 467 end); |
371 end); | 468 end); |
372 end); | 469 end); |
470 | |
471 describe("top_tag", function () | |
472 local xml_parse = require "util.xml".parse; | |
473 it("works", function () | |
474 local s = st.message({type="chat"}, "Hello"); | |
475 local top_tag = s:top_tag(); | |
476 assert.is_string(top_tag); | |
477 assert.not_equal("/>", top_tag:sub(-2, -1)); | |
478 assert.equal(">", top_tag:sub(-1, -1)); | |
479 local s2 = xml_parse(top_tag.."</message>"); | |
480 assert(st.is_stanza(s2)); | |
481 assert.equal("message", s2.name); | |
482 assert.equal(0, #s2); | |
483 assert.equal(0, #s2.tags); | |
484 assert.equal("chat", s2.attr.type); | |
485 end); | |
486 | |
487 it("works with namespaced attributes", function () | |
488 local s = xml_parse[[<message foo:bar='true' xmlns:foo='my-awesome-ns'/>]]; | |
489 local top_tag = s:top_tag(); | |
490 assert.is_string(top_tag); | |
491 assert.not_equal("/>", top_tag:sub(-2, -1)); | |
492 assert.equal(">", top_tag:sub(-1, -1)); | |
493 local s2 = xml_parse(top_tag.."</message>"); | |
494 assert(st.is_stanza(s2)); | |
495 assert.equal("message", s2.name); | |
496 assert.equal(0, #s2); | |
497 assert.equal(0, #s2.tags); | |
498 assert.equal("true", s2.attr["my-awesome-ns\1bar"]); | |
499 end); | |
500 end); | |
501 | |
502 describe("indent", function () | |
503 local s = st.stanza("foo"):text("\n"):tag("bar"):tag("baz"):up():text_tag("cow", "moo"); | |
504 assert.equal("<foo>\n\t<bar>\n\t\t<baz/>\n\t\t<cow>moo</cow>\n\t</bar>\n</foo>", tostring(s:indent())); | |
505 assert.equal("<foo>\n <bar>\n <baz/>\n <cow>moo</cow>\n </bar>\n</foo>", tostring(s:indent(1, " "))); | |
506 assert.equal("<foo>\n\t\t<bar>\n\t\t\t<baz/>\n\t\t\t<cow>moo</cow>\n\t\t</bar>\n\t</foo>", tostring(s:indent(2, "\t"))); | |
507 end); | |
508 | |
373 end); | 509 end); |