source: ralphm-patches/request-stanza.patch @ 53:fd2b3f70b221

Last change on this file since 53:fd2b3f70b221 was 53:fd2b3f70b221, checked in by Ralph Meijer <ralphm@…>, 9 years ago

Small cleanups for request handling.

File size: 4.3 KB
  • wokkel/generic.py

    # HG changeset patch
    # Parent 02050b16f4310d9875ae26fbe70ec31415a5d829
    Add base class for IQ get/set request stanzas: Request.
    
    diff -r 02050b16f431 wokkel/generic.py
    a b  
    236236        self.exception = error.exceptionFromStanza(element)
    237237
    238238
     239class Request(Stanza):
     240    """
     241    IQ request stanza.
     242
     243    This is a base class for IQ get or set stanzas, to be used with
     244    L{wokkel.subprotocols.StreamManager.request}.
     245    """
     246
     247    stanzaKind = 'iq'
     248    stanzaType = 'get'
     249    timeout = None
     250
     251    def __init__(self, recipient=None, sender=None, stanzaType='get'):
     252        Stanza.__init__(self, recipient=recipient, sender=sender)
     253        self.stanzaType = stanzaType
     254
     255
     256    def toElement(self):
     257        element = Stanza.toElement(self)
     258
     259        if not self.stanzaID:
     260            element.addUniqueId()
     261            self.stanzaID = element['id']
     262
     263        return element
     264
     265
     266
    239267class DeferredXmlStreamFactory(BootstrapMixin, protocol.ClientFactory):
    240268    protocol = xmlstream.XmlStream
    241269
  • wokkel/test/test_generic.py

    diff -r 02050b16f431 wokkel/test/test_generic.py
    a b  
    77
    88from twisted.trial import unittest
    99from twisted.words.xish import domish
     10from twisted.words.protocols.jabber.jid import JID
    1011
    1112from wokkel import generic
    1213from wokkel.test.helpers import XmlStreamStub
     
    8687        element = domish.Element(('testns', 'test'))
    8788        self.pipe.sink.send(element)
    8889        self.assertEquals([element], called)
     90
     91
     92
     93class RequestTest(unittest.TestCase):
     94    """
     95    Tests for L{generic.Request}.
     96    """
     97
     98    def setUp(self):
     99        self.request = generic.Request()
     100
     101
     102    def test_toElementStanzaKind(self):
     103        """
     104        A request is an iq stanza.
     105        """
     106        element = self.request.toElement()
     107        self.assertIdentical(None, element.uri)
     108        self.assertEquals('iq', element.name)
     109
     110
     111    def test_toElementStanzaType(self):
     112        """
     113        The request has type 'get'.
     114        """
     115        self.assertEquals('get', self.request.stanzaType)
     116        element = self.request.toElement()
     117        self.assertEquals('get', element.getAttribute('type'))
     118
     119
     120    def test_toElementStanzaTypeSet(self):
     121        """
     122        The request has type 'set'.
     123        """
     124        self.request.stanzaType = 'set'
     125        element = self.request.toElement()
     126        self.assertEquals('set', element.getAttribute('type'))
     127
     128
     129    def test_toElementStanzaID(self):
     130        """
     131        A request, when rendered, has an identifier.
     132        """
     133        element = self.request.toElement()
     134        self.assertNotIdentical(None, self.request.stanzaID)
     135        self.assertEquals(self.request.stanzaID, element.getAttribute('id'))
     136
     137
     138    def test_toElementRecipient(self):
     139        """
     140        A request without recipient, has no 'to' attribute.
     141        """
     142        self.request = generic.Request(recipient=JID('other@example.org'))
     143        self.assertEquals(JID('other@example.org'), self.request.recipient)
     144        element = self.request.toElement()
     145        self.assertEquals(u'other@example.org', element.getAttribute('to'))
     146
     147
     148    def test_toElementRecipientNone(self):
     149        """
     150        A request without recipient, has no 'to' attribute.
     151        """
     152        element = self.request.toElement()
     153        self.assertFalse(element.hasAttribute('to'))
     154
     155
     156    def test_toElementSender(self):
     157        """
     158        A request with sender, has a 'from' attribute.
     159        """
     160        self.request = generic.Request(sender=JID('user@example.org'))
     161        self.assertEquals(JID('user@example.org'), self.request.sender)
     162        element = self.request.toElement()
     163        self.assertEquals(u'user@example.org', element.getAttribute('from'))
     164
     165
     166    def test_toElementSenderNone(self):
     167        """
     168        A request without sender, has no 'from' attribute.
     169        """
     170        element = self.request.toElement()
     171        self.assertFalse(element.hasAttribute('from'))
     172
     173
     174    def test_timeoutDefault(self):
     175        """
     176        The default is no timeout.
     177        """
     178        self.assertIdentical(None, self.request.timeout)
Note: See TracBrowser for help on using the repository browser.