def test_legacy(self): """ Test legacy operations of exceptionFromStanza. Given a realistic stanza with only legacy (pre-XMPP) error information, check if a sane exception is returned. Using this stanza:: <message type='error' to='[email protected]/Home' from='*****@*****.**'> <body>Are you there?</body> <error code='502'>Unable to resolve hostname.</error> </message> """ stanza = domish.Element((None, "stanza")) p = stanza.addElement("body", content="Are you there?") e = stanza.addElement("error", content="Unable to resolve hostname.") e["code"] = "502" result = error.exceptionFromStanza(stanza) self.assertIsInstance(result, error.StanzaError) self.assertEqual("service-unavailable", result.condition) self.assertEqual("wait", result.type) self.assertEqual("Unable to resolve hostname.", result.text) self.assertEqual([p], result.children)
def test_legacy(self): """ Test legacy operations of exceptionFromStanza. Given a realistic stanza with only legacy (pre-XMPP) error information, check if a sane exception is returned. Using this stanza:: <message type='error' to='[email protected]/Home' from='*****@*****.**'> <body>Are you there?</body> <error code='502'>Unable to resolve hostname.</error> </message> """ stanza = domish.Element((None, 'stanza')) p = stanza.addElement('body', content='Are you there?') e = stanza.addElement('error', content='Unable to resolve hostname.') e['code'] = '502' result = error.exceptionFromStanza(stanza) self.assert_(isinstance(result, error.StanzaError)) self.assertEqual('service-unavailable', result.condition) self.assertEqual('wait', result.type) self.assertEqual('Unable to resolve hostname.', result.text) self.assertEqual([p], result.children)
def testLegacy(self): """ Test legacy operations of exceptionFromStanza. Given a realistic stanza with only legacy (pre-XMPP) error information, check if a sane exception is returned. Using this stanza:: <message type='error' to='[email protected]/Home' from='*****@*****.**'> <body>Are you there?</body> <error code='502'>Unable to resolve hostname.</error> </message> """ stanza = domish.Element((None, 'stanza')) p = stanza.addElement('body', content='Are you there?') e = stanza.addElement('error', content='Unable to resolve hostname.') e['code'] = '502' result = error.exceptionFromStanza(stanza) self.assert_(isinstance(result, error.StanzaError)) self.assertEquals('service-unavailable', result.condition) self.assertEquals('wait', result.type) self.assertEquals('Unable to resolve hostname.', result.text) self.assertEquals([p], result.children)
def callback(iq): if getattr(iq, 'handled', False): return try: d = xs.queryDeferreds[iq["id"]] except KeyError: pass else: del xs.queryDeferreds[iq["id"]] iq.handled = True try : _ex = xpath.queryForNodes("/message/x", iq, )[0] except IndexError : d.errback(error.exceptionFromStanza(iq, ), ) else : if "type" in _ex.attributes and _ex['type'] == 'error': d.errback(error.exceptionFromStanza(_ex, ), ) else: d.callback(iq)
def _onIQResponse(self, iq): try: d = self._iqDeferreds[iq["id"]] except KeyError: return del self._iqDeferreds[iq["id"]] iq.handled = True if iq['type'] == 'error': d.errback(error.exceptionFromStanza(iq)) else: d.callback(iq)
def callback(iq): if getattr(iq, 'handled', False): return try: d = xmlstream.iqDeferreds[iq["id"]] except KeyError: pass else: del iq["id"] iq.handled = True if iq['type'] == 'error': d.errback(failure.Failure(error.exceptionFromStanza(iq))) else: d.callback(iq)
def test_noHandler(self): """ Test when the request is not recognised. """ iq = domish.Element((None, 'iq')) iq['type'] = 'set' iq['id'] = 'r1' handler = DummyIQHandler() handler.handleRequest(iq) response = handler.output[-1] self.assertEquals(None, response.uri) self.assertEquals('iq', response.name) self.assertEquals('error', response['type']) e = error.exceptionFromStanza(response) self.assertEquals('feature-not-implemented', e.condition)
def callback(iq): """ Handle iq response by firing associated deferred. """ if getattr(iq, 'handled', False): return try: d = xs.iqDeferreds[iq["id"]] except KeyError: pass else: del xs.iqDeferreds[iq["id"]] iq.handled = True if iq['type'] == 'error': d.errback(error.exceptionFromStanza(iq)) else: d.callback(iq)
def test_notImplemented(self): """ Test response when the request is recognised but not implemented. """ class Handler(DummyIQHandler): def onGet(self, iq): raise NotImplementedError() iq = domish.Element((None, 'iq')) iq['type'] = 'get' iq['id'] = 'r1' handler = Handler() handler.handleRequest(iq) response = handler.output[-1] self.assertEquals(None, response.uri) self.assertEquals('iq', response.name) self.assertEquals('error', response['type']) e = error.exceptionFromStanza(response) self.assertEquals('feature-not-implemented', e.condition)
def test_failure(self): """ Test response when the request is handled unsuccessfully. """ class Handler(DummyIQHandler): def onGet(self, iq): raise error.StanzaError('forbidden') iq = domish.Element((None, 'iq')) iq['type'] = 'get' iq['id'] = 'r1' handler = Handler() handler.handleRequest(iq) response = handler.output[-1] self.assertEquals(None, response.uri) self.assertEquals('iq', response.name) self.assertEquals('error', response['type']) e = error.exceptionFromStanza(response) self.assertEquals('forbidden', e.condition)
def testBasic(self): """ Test basic operations of exceptionFromStanza. Given a realistic stanza, check if a sane exception is returned. Using this stanza:: <iq type='error' from='pubsub.shakespeare.lit' to='[email protected]/barracks' id='subscriptions1'> <pubsub xmlns='http://jabber.org/protocol/pubsub'> <subscriptions/> </pubsub> <error type='cancel'> <feature-not-implemented xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/> <unsupported xmlns='http://jabber.org/protocol/pubsub#errors' feature='retrieve-subscriptions'/> </error> </iq> """ stanza = domish.Element((None, 'stanza')) p = stanza.addElement(('http://jabber.org/protocol/pubsub', 'pubsub')) p.addElement('subscriptions') e = stanza.addElement('error') e['type'] = 'cancel' e.addElement((NS_XMPP_STANZAS, 'feature-not-implemented')) uc = e.addElement(('http://jabber.org/protocol/pubsub#errors', 'unsupported')) uc['feature'] = 'retrieve-subscriptions' result = error.exceptionFromStanza(stanza) self.assert_(isinstance(result, error.StanzaError)) self.assertEquals('feature-not-implemented', result.condition) self.assertEquals('cancel', result.type) self.assertEquals(uc, result.appCondition) self.assertEquals([p], result.children)
def test_basic(self): """ Test basic operations of exceptionFromStanza. Given a realistic stanza, check if a sane exception is returned. Using this stanza:: <iq type='error' from='pubsub.shakespeare.lit' to='[email protected]/barracks' id='subscriptions1'> <pubsub xmlns='http://jabber.org/protocol/pubsub'> <subscriptions/> </pubsub> <error type='cancel'> <feature-not-implemented xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/> <unsupported xmlns='http://jabber.org/protocol/pubsub#errors' feature='retrieve-subscriptions'/> </error> </iq> """ stanza = domish.Element((None, 'stanza')) p = stanza.addElement(('http://jabber.org/protocol/pubsub', 'pubsub')) p.addElement('subscriptions') e = stanza.addElement('error') e['type'] = 'cancel' e.addElement((NS_XMPP_STANZAS, 'feature-not-implemented')) uc = e.addElement( ('http://jabber.org/protocol/pubsub#errors', 'unsupported')) uc['feature'] = 'retrieve-subscriptions' result = error.exceptionFromStanza(stanza) self.assert_(isinstance(result, error.StanzaError)) self.assertEqual('feature-not-implemented', result.condition) self.assertEqual('cancel', result.type) self.assertEqual(uc, result.appCondition) self.assertEqual([p], result.children)
def test_failureUnknown(self): """ Test response when the request handler raises a non-stanza-error. """ class TestError(Exception): pass class Handler(DummyIQHandler): def onGet(self, iq): raise TestError() iq = domish.Element((None, 'iq')) iq['type'] = 'get' iq['id'] = 'r1' handler = Handler() handler.handleRequest(iq) response = handler.output[-1] self.assertEquals(None, response.uri) self.assertEquals('iq', response.name) self.assertEquals('error', response['type']) e = error.exceptionFromStanza(response) self.assertEquals('internal-server-error', e.condition) self.assertEquals(1, len(self.flushLoggedErrors(TestError)))
def parseElement(self, element): Stanza.parseElement(self, element) self.exception = error.exceptionFromStanza(element)
def onResponse(element): if element.getAttribute("type") == "error": d.errback(error.exceptionFromStanza(element)) else: d.callback(UserPresence.fromElement(element))
def onResponse(element): if element.getAttribute('type') == 'error': d.errback(error.exceptionFromStanza(element)) else: d.callback(UserPresence.fromElement(element))