Changeset 102:039149921acf


Ignore:
Timestamp:
Aug 3, 2011, 9:49:22 AM (9 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
Message:

Add base class for IQ get/set request stanzas: Request.

Location:
wokkel
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • wokkel/generic.py

    r96 r102  
    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
  • wokkel/test/test_generic.py

    r96 r102  
    88from twisted.trial import unittest
    99from twisted.words.xish import domish
     10from twisted.words.protocols.jabber.jid import JID
    1011
    1112from wokkel import generic
     
    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 TracChangeset for help on using the changeset viewer.