| 1 |
# -*- test-case-name: tx_xmpp.test.test.test_shim -*- |
| 2 |
# |
| 3 |
# Copyright (c) Ralph Meijer. |
| 4 |
# See LICENSE for details. |
| 5 |
|
| 6 |
""" |
| 7 |
Tests for {tx_xmpp.shim}. |
| 8 |
""" |
| 9 |
|
| 10 |
from twisted.trial import unittest |
| 11 |
|
| 12 |
from .. import shim |
| 13 |
from ..generic import parseXml |
| 14 |
|
| 15 |
NS_SHIM = "http://jabber.org/protocol/shim" |
| 16 |
|
| 17 |
|
| 18 |
class HeadersTest(unittest.TestCase): |
| 19 |
""" |
| 20 |
Tests for L{tx_xmpp.shim.headers}. |
| 21 |
""" |
| 22 |
|
| 23 |
def test_noHeaders(self): |
| 24 |
headers = shim.Headers([]) |
| 25 |
self.assertEquals(NS_SHIM, headers.uri) |
| 26 |
self.assertEquals("headers", headers.name) |
| 27 |
self.assertEquals([], headers.children) |
| 28 |
|
| 29 |
def test_header(self): |
| 30 |
headers = shim.Headers([("Urgency", "high")]) |
| 31 |
elements = list(headers.elements()) |
| 32 |
self.assertEquals(1, len(elements)) |
| 33 |
header = elements[0] |
| 34 |
self.assertEquals(NS_SHIM, header.uri) |
| 35 |
self.assertEquals("header", header.name) |
| 36 |
self.assertEquals("Urgency", header["name"]) |
| 37 |
self.assertEquals("high", str(header)) |
| 38 |
|
| 39 |
def test_headerRepeated(self): |
| 40 |
""" |
| 41 |
Some headers can appear more than once with the same name. |
| 42 |
""" |
| 43 |
headers = shim.Headers([("Collection", "node1"), ("Collection", "node2")]) |
| 44 |
elements = list(headers.elements()) |
| 45 |
self.assertEquals(2, len(elements)) |
| 46 |
collections = set( |
| 47 |
(str(element) for element in elements if element["name"] == "Collection") |
| 48 |
) |
| 49 |
self.assertIn("node1", collections) |
| 50 |
self.assertIn("node2", collections) |
| 51 |
|
| 52 |
|
| 53 |
class ExtractHeadersTest(unittest.TestCase): |
| 54 |
""" |
| 55 |
Tests for L{tx_xmpp.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"], "Collection": ["node1"]}, headers) |
| 80 |
|
| 81 |
def test_headersRepeated(self): |
| 82 |
""" |
| 83 |
Some headers may appear repeatedly. Make sure all values are extracted. |
| 84 |
""" |
| 85 |
xml = """<message> |
| 86 |
<headers xmlns='http://jabber.org/protocol/shim'> |
| 87 |
<header name='Collection'>node1</header> |
| 88 |
<header name='Urgency'>high</header> |
| 89 |
<header name='Collection'>node2</header> |
| 90 |
</headers> |
| 91 |
</message>""" |
| 92 |
|
| 93 |
stanza = parseXml(xml) |
| 94 |
headers = shim.extractHeaders(stanza) |
| 95 |
self.assertEquals( |
| 96 |
{"Urgency": ["high"], "Collection": ["node1", "node2"]}, headers |
| 97 |
) |