Software / code / prosody
Comparison
spec/util_stanza_spec.lua @ 10563:e8db377a2983
Merge 0.11->trunk
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Tue, 24 Dec 2019 00:39:45 +0100 |
| parent | 10503:e25a1a9a6e7e |
| child | 10717:05e4645fc9b3 |
comparison
equal
deleted
inserted
replaced
| 10562:670afc079f68 | 10563:e8db377a2983 |
|---|---|
| 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); |
| 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 it("should accept util.error objects", function () | |
| 236 local s = st.message({ to = "touser", from = "fromuser", id = "123", type = "chat" }, "Hello"); | |
| 237 local e = errors.new({ type = "modify", condition = "not-acceptable", text = "Bork bork bork" }); | |
| 238 local r = st.error_reply(s, e); | |
| 239 | |
| 240 assert.are.equal(r.name, s.name); | |
| 241 assert.are.equal(r.id, s.id); | |
| 242 assert.are.equal(r.attr.to, s.attr.from); | |
| 243 assert.are.equal(r.attr.from, s.attr.to); | |
| 244 assert.are.equal(r.attr.type, "error"); | |
| 245 assert.are.equal(r.tags[1].name, "error"); | |
| 246 assert.are.equal(r.tags[1].attr.type, e.type); | |
| 247 assert.are.equal(r.tags[1].tags[1].name, e.condition); | |
| 248 assert.are.equal(r.tags[1].tags[2]:get_text(), e.text); | |
| 249 end); | |
| 250 | |
| 193 end); | 251 end); |
| 194 | 252 |
| 195 describe("should reject #invalid", function () | 253 describe("should reject #invalid", function () |
| 196 local invalid_names = { | 254 local invalid_names = { |
| 197 ["empty string"] = "", ["characters"] = "<>"; | 255 ["empty string"] = "", ["characters"] = "<>"; |
| 368 assert.has_error(function () | 426 assert.has_error(function () |
| 369 st.clone("this is not a stanza"); | 427 st.clone("this is not a stanza"); |
| 370 end); | 428 end); |
| 371 end); | 429 end); |
| 372 end); | 430 end); |
| 431 | |
| 432 describe("top_tag", function () | |
| 433 local xml_parse = require "util.xml".parse; | |
| 434 it("works", function () | |
| 435 local s = st.message({type="chat"}, "Hello"); | |
| 436 local top_tag = s:top_tag(); | |
| 437 assert.is_string(top_tag); | |
| 438 assert.not_equal("/>", top_tag:sub(-2, -1)); | |
| 439 assert.equal(">", top_tag:sub(-1, -1)); | |
| 440 local s2 = xml_parse(top_tag.."</message>"); | |
| 441 assert(st.is_stanza(s2)); | |
| 442 assert.equal("message", s2.name); | |
| 443 assert.equal(0, #s2); | |
| 444 assert.equal(0, #s2.tags); | |
| 445 assert.equal("chat", s2.attr.type); | |
| 446 end); | |
| 447 | |
| 448 it("works with namespaced attributes", function () | |
| 449 local s = xml_parse[[<message foo:bar='true' xmlns:foo='my-awesome-ns'/>]]; | |
| 450 local top_tag = s:top_tag(); | |
| 451 assert.is_string(top_tag); | |
| 452 assert.not_equal("/>", top_tag:sub(-2, -1)); | |
| 453 assert.equal(">", top_tag:sub(-1, -1)); | |
| 454 local s2 = xml_parse(top_tag.."</message>"); | |
| 455 assert(st.is_stanza(s2)); | |
| 456 assert.equal("message", s2.name); | |
| 457 assert.equal(0, #s2); | |
| 458 assert.equal(0, #s2.tags); | |
| 459 assert.equal("true", s2.attr["my-awesome-ns\1bar"]); | |
| 460 end); | |
| 461 end); | |
| 373 end); | 462 end); |