client: Support for "receives: nothing", to verify that the server does not respond to a given stanza, for example

Fri, 19 Feb 2016 11:48:31 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Fri, 19 Feb 2016 11:48:31 +0000
changeset 84
c9061cd9951b
parent 83
52f8fa7f999e
child 85
c928e514aa95

client: Support for "receives: nothing", to verify that the server does not respond to a given stanza, for example

scansion/objects/client.lua file | annotate | diff | comparison | revisions
--- a/scansion/objects/client.lua	Fri Feb 19 11:48:04 2016 +0000
+++ b/scansion/objects/client.lua	Fri Feb 19 11:48:31 2016 +0000
@@ -85,12 +85,21 @@
 
 	receives = function (client, data)
 		local wait, done = async.waiter();
-		local expected_stanza = fill_vars(client.script, assert(parse_xml((table.concat(data):gsub("\t", "  ")))));
+		local expected_stanza = false;
+		data = table.concat(data):gsub("\t", "    "):gsub("^%s+", ""):gsub("%s+$", "");
+		if data ~= "nothing" then
+			expected_stanza = fill_vars(client.script, assert(parse_xml(data)));
+		end
 		local function stanza_handler(received_stanza)
-			if not stanzacmp.stanzas_match(expected_stanza, received_stanza) then
-				client.log("NOT IT!")
-				client.log("Expected: %s", expected_stanza);
-				client.log("Received: %s", received_stanza);
+			if not expected_stanza then
+				error(new_error("Received unexpected stanza", { stanza = tostring(received_stanza) }));
+			elseif not expected_stanza or not stanzacmp.stanzas_match(expected_stanza, received_stanza) then
+				if not expected_stanza then
+					client.log("Received a stanza when none were expected: %s", received_stanza);
+				else
+					client.log("Expected: %s", expected_stanza);
+					client.log("Received: %s", received_stanza);
+				end
 				error(new_error("Received unexpected stanza", { stanza = tostring(received_stanza) }));
 			else
 				client.last_received_id = received_stanza.attr.id;

mercurial