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