-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_upnp.py
69 lines (59 loc) · 2.44 KB
/
test_upnp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python
# encoding: utf-8
import io
import unittest
import upnp
class UpnpTests(unittest.TestCase):
soap_response_template = (
'<?xml version="1.0"?>'
'<s:Envelope '
'xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" '
's:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'
'<s:Body>'
'<u:{action}Response xmlns:u="urn:schemas-upnp-org:service:serviceType:v">'
'{args_xml}'
'</u:{action}Response>'
'</s:Body>'
'</s:Envelope>'
)
argument_temlate = '<{name}>{value}</{name}>'
def test_parse_response_no_arguments(self):
"""Passing a SOAP response with no arguments should give []"""
soap_xml = self.soap_response_template.format(
action='Pause', args_xml=''
)
arguments = upnp.parse_response('Pause', io.StringIO(soap_xml))
self.assertEqual(arguments, dict())
def test_parse_response_with_arguments(self):
"""Passing a SOAP response with some valid arguments should return them"""
args = [
dict(name='arg1', value='value1'),
dict(name='arg2', value='value2'),
]
args_xml = ''.join(
self.argument_temlate.format(**arg)
for arg in args
)
soap_xml = self.soap_response_template.format(
action='Pause', args_xml=args_xml
)
arguments = upnp.parse_response('Pause', io.StringIO(soap_xml))
self.assertEqual(arguments, dict([
(arg['name'], arg['value'])
for arg in args
]))
def test_parse_response_with_invalid_xml(self):
"""Passing some invalid badly formed XML should cause it to give up"""
with self.assertRaises(Exception):
upnp.parse_response('Pause', io.StringIO('<a>'))
def test_unescape_special_characters(self):
"""Argument values should have their special characters unescaped"""
arg = dict(name='arg1', value='<xml attr="with &apos; in it"></test>')
args_xml = self.argument_temlate.format(**arg)
soap_xml = self.soap_response_template.format(
action='NotReal', args_xml=args_xml
)
arguments = upnp.parse_response('NotReal', io.StringIO(soap_xml))
self.assertEqual(arguments, dict(arg1='<xml attr="with \' in it"></test>'))
if __name__ == '__main__':
unittest.main()