1 | # -*- test-case-name: wokkel.test.test_shim -*- |
---|
2 | # |
---|
3 | # Copyright (c) 2003-2008 Ralph Meijer |
---|
4 | # See LICENSE for details. |
---|
5 | |
---|
6 | """ |
---|
7 | Tests for {wokkel.shim}. |
---|
8 | """ |
---|
9 | |
---|
10 | from twisted.trial import unittest |
---|
11 | from wokkel import shim |
---|
12 | from wokkel.generic import parseXml |
---|
13 | |
---|
14 | NS_SHIM = 'http://jabber.org/protocol/shim' |
---|
15 | |
---|
16 | class 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 | |
---|
53 | class 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) |
---|