source: wokkel/test/test_shim.py @ 27:d62d7ea14995

Last change on this file since 27:d62d7ea14995 was 27:d62d7ea14995, checked in by Ralph Meijer <ralphm@…>, 13 years ago

Implement SHIM support.

Author: ralphm.
Fixes #14.

This changes the signature of PubSubClient's itemsReceived and friends, to
have an object to represent the event, for easier addition of new information
from the event's message stanza, like these SHIM headers.

File size: 3.1 KB
Line 
1# -*- test-case-name: wokkel.test.test_shim -*-
2#
3# Copyright (c) 2003-2008 Ralph Meijer
4# See LICENSE for details.
5
6"""
7Tests for {wokkel.shim}.
8"""
9
10from twisted.trial import unittest
11from wokkel import shim
12from wokkel.generic import parseXml
13
14NS_SHIM = 'http://jabber.org/protocol/shim'
15
16class HeadersTest(unittest.TestCase):
17    """
18    Tests for L{wokkel.shim.headers}.
19    """
20
21    def test_noHeaders(self):
22        headers = shim.Headers([])
23        self.assertEquals(NS_SHIM, headers.uri)
24        self.assertEquals('headers', headers.name)
25        self.assertEquals([], headers.children)
26
27    def test_header(self):
28        headers = shim.Headers([('Urgency', 'high')])
29        elements = list(headers.elements())
30        self.assertEquals(1, len(elements))
31        header = elements[0]
32        self.assertEquals(NS_SHIM, header.uri)
33        self.assertEquals('header', header.name)
34        self.assertEquals('Urgency', header['name'])
35        self.assertEquals('high', unicode(header))
36
37
38    def test_headerRepeated(self):
39        """
40        Some headers can appear more than once with the same name.
41        """
42        headers = shim.Headers([('Collection', 'node1'),
43                           ('Collection', 'node2')])
44        elements = list(headers.elements())
45        self.assertEquals(2, len(elements))
46        collections = set((unicode(element) for element in elements
47                           if element['name'] == 'Collection'))
48        self.assertIn('node1', collections)
49        self.assertIn('node2', collections)
50
51
52
53class ExtractHeadersTest(unittest.TestCase):
54    """
55    Tests for L{wokkel.shim.extractHeaders}.
56    """
57
58    def test_noHeaders(self):
59        """
60        A stanza without headers results in an empty dictionary.
61        """
62        stanza = parseXml("""<message/>""")
63        headers = shim.extractHeaders(stanza)
64        self.assertEquals({}, headers)
65
66    def test_headers(self):
67        """
68        A stanza with headers results in a dictionary with those headers.
69        """
70        xml = """<message>
71                   <headers xmlns='http://jabber.org/protocol/shim'>
72                     <header name='Collection'>node1</header>
73                     <header name='Urgency'>high</header>
74                   </headers>
75                 </message>"""
76
77        stanza = parseXml(xml)
78        headers = shim.extractHeaders(stanza)
79        self.assertEquals({'Urgency': ['high'],
80                           'Collection': ['node1']}, headers)
81
82
83    def test_headersRepeated(self):
84        """
85        Some headers may appear repeatedly. Make sure all values are extracted.
86        """
87        xml = """<message>
88                   <headers xmlns='http://jabber.org/protocol/shim'>
89                     <header name='Collection'>node1</header>
90                     <header name='Urgency'>high</header>
91                     <header name='Collection'>node2</header>
92                   </headers>
93                 </message>"""
94
95        stanza = parseXml(xml)
96        headers = shim.extractHeaders(stanza)
97        self.assertEquals({'Urgency': ['high'],
98                           'Collection': ['node1', 'node2']}, headers)
Note: See TracBrowser for help on using the repository browser.