test: errors should only ever trigger one handler

Phillip Davis created

Change summary

test/test_component.rb | 37 +++++++++++++++++++++++++++++++++++++
1 file changed, 37 insertions(+)

Detailed changes

test/test_component.rb 🔗

@@ -577,4 +577,41 @@ class ComponentTest < Minitest::Test
 		SGXbwmsgsv2.instance_variable_set(:@written, [])
 	end
 	em :test_sentry_captures_handler_exception
+
+	def test_error_handler
+		client = SGXbwmsgsv2.send(:client)
+		original_call_handler = client.method(:call_handler)
+		handlers_called = 0
+		with_stubs([
+			[
+				client,
+				:call_handler,
+				->(handler, guards, stanza) {
+					handlers_called += 1
+					original_call_handler.call(handler, guards, stanza)
+				}
+			]
+		]) do
+			stub_request(:post, "https://messaging.bandwidth.com/api/v2/users/account/messages")
+				.with(
+					body: "{\"from\":\"+15550000000\",\"to\":\"+15551234567\",\"text\":\"Hello world\",\"applicationId\":\"test-application-id\",\"tag\":\"stanza-123 resource\"}",
+					headers: {
+						'Accept-Encoding'=>'gzip, compressed',
+						'Authorization'=>'Basic dXNlcjpwYXNzd29yZA==',
+						'Connection'=>'close',
+						'Content-Type'=>'application/json',
+						'Host'=>'messaging.bandwidth.com',
+						'User-Agent'=>'EventMachine HttpClient'
+					}
+				)
+				.to_return(status: 200, body: "", headers: {})
+			m = Blather::Stanza::Message.new("+15551234567@component", "Hello world")
+			m.from = "test@example.com/resource"
+			m['id'] = "stanza-123"
+			m.type = :error
+			process_stanza(m)
+		end
+		assert_equal 1, handlers_called
+	end
+	em :test_error_handler
 end