Changeset 97:f11253bd8b69


Ignore:
Timestamp:
Mar 7, 2011, 7:40:16 PM (9 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
Message:

Make sure item and subscription elements get the correct namespace.

This complements the changes in [f4aa0d507bc8] and [3fe44cf07366] for item and
subscription elements that are used in different namespaces. These changesets
set the namespace to None, which works nicely when the elements are
serialized. However, in situations where the DOM representation is passed
around (in-process communication) these elements are no longer matched by
namespace. This change makes sure the elements get the proper namespace just
before they are sent out.

Location:
wokkel
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • wokkel/pubsub.py

    r96 r97  
    139139
    140140
    141     def toElement(self):
     141    def toElement(self, defaultUri=None):
    142142        """
    143143        Return the DOM representation of this subscription.
     
    145145        @rtype: L{domish.Element}
    146146        """
    147         element = domish.Element((None, 'subscription'))
     147        element = domish.Element((defaultUri, 'subscription'))
    148148        if self.nodeIdentifier:
    149149            element['node'] = self.nodeIdentifier
     
    359359        if self.items:
    360360            for item in self.items:
     361                item.uri = NS_PUBSUB
    361362                verbElement.addChild(item)
    362363
     
    12141215    def _toResponse_subscribe(self, result, resource, request):
    12151216        response = domish.Element((NS_PUBSUB, "pubsub"))
    1216         response.addChild(result.toElement())
     1217        response.addChild(result.toElement(NS_PUBSUB))
    12171218        return response
    12181219
     
    12221223        subscriptions = response.addElement('subscriptions')
    12231224        for subscription in result:
    1224             subscriptions.addChild(subscription.toElement())
     1225            subscriptions.addChild(subscription.toElement(NS_PUBSUB))
    12251226        return response
    12261227
     
    13081309
    13091310        for item in result:
     1311            item.uri = NS_PUBSUB
    13101312            items.addChild(item)
    13111313
     
    13581360                                               nodeIdentifier, subscriber,
    13591361                                               subscriptions)
    1360             message.event.items.children = items
     1362            for item in items:
     1363                item.uri = NS_PUBSUB_EVENT
     1364                message.event.items.addChild(item)
    13611365            self.send(message)
    13621366
  • wokkel/test/test_pubsub.py

    r96 r97  
    463463        child = children[0]
    464464        self.assertEquals('test', child['node'])
    465 
    466         items = []
    467         for element in child.elements():
    468             if element.name == 'item' and element.uri in (NS_PUBSUB, None):
    469                 items.append(element)
    470 
     465        items = list(domish.generateElementsQNamed(child.children,
     466                                                   'item', NS_PUBSUB))
    471467        self.assertEquals(1, len(items))
    472468        self.assertIdentical(item, items[0])
     
    22182214            self.assertEqual(NS_PUBSUB, element.uri)
    22192215            subscription = element.subscription
    2220             self.assertIn(subscription.uri, (None, NS_PUBSUB))
     2216            self.assertEqual(NS_PUBSUB, subscription.uri)
    22212217            self.assertEqual('test', subscription['node'])
    22222218            self.assertEqual('user@example.org/Home', subscription['jid'])
     
    24312427            subscription = children[0]
    24322428            self.assertEqual('subscription', subscription.name)
    2433             self.assertIn(subscription.uri, (None, NS_PUBSUB))
     2429            self.assertEqual(NS_PUBSUB, subscription.uri, NS_PUBSUB)
    24342430            self.assertEqual('user@example.org', subscription['jid'])
    24352431            self.assertEqual('test', subscription['node'])
     
    29812977            self.assertTrue(domish.IElement.providedBy(item))
    29822978            self.assertEqual('item', item.name)
    2983             self.assertIn(item.uri, (NS_PUBSUB, None))
     2979            self.assertEqual(NS_PUBSUB, item.uri)
    29842980            self.assertEqual('current', item['id'])
    29852981
     
    30613057        verify.verifyObject(iwokkel.IPubSubResource, self.resource)
    30623058        return self.handleRequest(xml)
     3059
     3060
     3061    def test_notifyPublish(self):
     3062        """
     3063        Publish notifications are sent to the subscribers.
     3064        """
     3065        subscriber = JID('user@example.org')
     3066        subscriptions = [pubsub.Subscription('test', subscriber, 'subscribed')]
     3067        items = [pubsub.Item('current')]
     3068        notifications = [(subscriber, subscriptions, items)]
     3069        self.service.notifyPublish(JID('pubsub.example.org'), 'test',
     3070                                   notifications)
     3071        message = self.stub.output[-1]
     3072
     3073        self.assertEquals('message', message.name)
     3074        self.assertIdentical(None, message.uri)
     3075        self.assertEquals('user@example.org', message['to'])
     3076        self.assertEquals('pubsub.example.org', message['from'])
     3077        self.assertTrue(message.event)
     3078        self.assertEquals(NS_PUBSUB_EVENT, message.event.uri)
     3079        self.assertTrue(message.event.items)
     3080        self.assertEquals(NS_PUBSUB_EVENT, message.event.items.uri)
     3081        self.assertTrue(message.event.items.hasAttribute('node'))
     3082        self.assertEquals('test', message.event.items['node'])
     3083        itemElements = list(domish.generateElementsQNamed(
     3084            message.event.items.children, 'item', NS_PUBSUB_EVENT))
     3085        self.assertEquals(1, len(itemElements))
     3086        self.assertEquals('current', itemElements[0].getAttribute('id'))
     3087
     3088
     3089    def test_notifyPublishCollection(self):
     3090        """
     3091        Publish notifications are sent to the subscribers of collections.
     3092
     3093        The node the item was published to is on the C{items} element, while
     3094        the subscribed-to node is in the C{'Collections'} SHIM header.
     3095        """
     3096        subscriber = JID('user@example.org')
     3097        subscriptions = [pubsub.Subscription('', subscriber, 'subscribed')]
     3098        items = [pubsub.Item('current')]
     3099        notifications = [(subscriber, subscriptions, items)]
     3100        self.service.notifyPublish(JID('pubsub.example.org'), 'test',
     3101                                   notifications)
     3102        message = self.stub.output[-1]
     3103
     3104        self.assertTrue(message.event.items.hasAttribute('node'))
     3105        self.assertEquals('test', message.event.items['node'])
     3106        headers = shim.extractHeaders(message)
     3107        self.assertIn('Collection', headers)
     3108        self.assertIn('', headers['Collection'])
    30633109
    30643110
Note: See TracChangeset for help on using the changeset viewer.