2018-05-08 14:32:07 +00:00
|
|
|
import json
|
|
|
|
import xml.etree.ElementTree as ET
|
|
|
|
|
|
|
|
from funkwhale_api.subsonic import renderers
|
|
|
|
|
|
|
|
|
|
|
|
def test_json_renderer():
|
2018-06-09 13:36:16 +00:00
|
|
|
data = {"hello": "world"}
|
2018-05-08 14:32:07 +00:00
|
|
|
expected = {
|
2018-06-09 13:36:16 +00:00
|
|
|
"subsonic-response": {"status": "ok", "version": "1.16.0", "hello": "world"}
|
2018-05-08 14:32:07 +00:00
|
|
|
}
|
|
|
|
renderer = renderers.SubsonicJSONRenderer()
|
|
|
|
assert json.loads(renderer.render(data)) == expected
|
|
|
|
|
|
|
|
|
|
|
|
def test_xml_renderer_dict_to_xml():
|
2018-06-09 13:36:16 +00:00
|
|
|
payload = {"hello": "world", "item": [{"this": 1}, {"some": "node"}]}
|
2018-05-08 14:32:07 +00:00
|
|
|
expected = """<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<key hello="world"><item this="1" /><item some="node" /></key>"""
|
2018-06-09 13:36:16 +00:00
|
|
|
result = renderers.dict_to_xml_tree("key", payload)
|
2018-05-08 14:32:07 +00:00
|
|
|
exp = ET.fromstring(expected)
|
|
|
|
assert ET.tostring(result) == ET.tostring(exp)
|
|
|
|
|
|
|
|
|
|
|
|
def test_xml_renderer():
|
2018-06-09 13:36:16 +00:00
|
|
|
payload = {"hello": "world"}
|
2018-05-08 14:32:07 +00:00
|
|
|
expected = b'<?xml version="1.0" encoding="UTF-8"?>\n<subsonic-response hello="world" status="ok" version="1.16.0" xmlns="http://subsonic.org/restapi" />'
|
|
|
|
|
|
|
|
renderer = renderers.SubsonicXMLRenderer()
|
|
|
|
rendered = renderer.render(payload)
|
|
|
|
|
|
|
|
assert rendered == expected
|