Changeset

9732:51583ea2b4fd

util.stanza: Require a type attribute for iq stanzas
author Kim Alvefur <zash@zash.se>
date Fri, 28 Dec 2018 20:49:01 +0100
parents 9731:47121e8dc5b1
children 9733:9ab9aabafa80
files spec/util_stanza_spec.lua util/stanza.lua
diffstat 2 files changed, 22 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/spec/util_stanza_spec.lua	Mon Dec 24 03:00:27 2018 +0100
+++ b/spec/util_stanza_spec.lua	Fri Dec 28 20:49:01 2018 +0100
@@ -95,19 +95,30 @@
 
 	describe("#iq()", function()
 		it("should create an iq stanza", function()
-			local i = st.iq({ id = "foo" });
+			local i = st.iq({ type = "get", id = "foo" });
 			assert.are.equal("iq", i.name);
 			assert.are.equal("foo", i.attr.id);
+			assert.are.equal("get", i.attr.type);
 		end);
 
+		it("should reject stanzas with no attributes", function ()
+			assert.has.error_match(function ()
+				st.iq();
+			end, "attributes");
+		end);
+
+
 		it("should reject stanzas with no id", function ()
 			assert.has.error_match(function ()
-				st.iq();
+				st.iq({ type = "get" });
 			end, "id attribute");
+		end);
 
+		it("should reject stanzas with no type", function ()
 			assert.has.error_match(function ()
-				st.iq({ foo = "bar" });
-			end, "id attribute");
+				st.iq({ id = "foo" });
+			end, "type attribute");
+
 		end);
 	end);
 
--- a/util/stanza.lua	Mon Dec 24 03:00:27 2018 +0100
+++ b/util/stanza.lua	Fri Dec 28 20:49:01 2018 +0100
@@ -423,9 +423,15 @@
 	end
 end
 local function iq(attr)
-	if not (attr and attr.id) then
+	if not attr then
+		error("iq stanzas require id and type attributes");
+	end
+	if not attr.id then
 		error("iq stanzas require an id attribute");
 	end
+	if not attr.type then
+		error("iq stanzas require a type attribute");
+	end
 	return new_stanza("iq", attr);
 end