Commit 1bc0da03 authored by Lukáš Lalinský's avatar Lukáš Lalinský

Include charset in the response Content-Type

parent b778d6f2
......@@ -39,12 +39,12 @@ def serialize_xml(data, **kwargs):
root = etree.Element('response')
_serialize_xml_node(root, data)
res = etree.tostring(root, encoding="UTF-8")
return Response(res, content_type='text/xml', **kwargs)
return Response(res, content_type='text/xml; charset=UTF-8', **kwargs)
def serialize_json(data, **kwargs):
res = json.dumps(data)
return Response(res, content_type='application/json', **kwargs)
return Response(res, content_type='application/json; charset=UTF-8', **kwargs)
def serialize_response(data, format, **kwargs):
......
......@@ -9,7 +9,7 @@ from acoustid.api import serialize_json, serialize_xml
def test_serialize_json():
data = {'status': 'ok', 'artists': [{'name': 'Jean Michel Jarre', 'year': 1948, 'cities': ['Paris', 'Lyon']}]}
resp = serialize_json(data)
assert_equals('application/json', resp.content_type)
assert_equals('application/json; charset=UTF-8', resp.content_type)
expected = '''{"status": "ok", "artists": [{"cities": ["Paris", "Lyon"], "name": "Jean Michel Jarre", "year": 1948}]}'''
assert_equals(expected, resp.data)
......@@ -17,7 +17,7 @@ def test_serialize_json():
def test_serialize_xml():
data = {'status': 'ok', 'artists': [{'name': 'Jean Michel Jarre', 'year': 1948, 'cities': ['Paris', 'Lyon']}]}
resp = serialize_xml(data)
assert_equals('text/xml', resp.content_type)
assert_equals('text/xml; charset=UTF-8', resp.content_type)
expected = '''<?xml version='1.0' encoding='UTF-8'?>\n<response><status>ok</status><artists><artist><cities><city>Paris</city><city>Lyon</city></cities><name>Jean Michel Jarre</name><year>1948</year></artist></artists></response>'''
assert_equals(expected, resp.data)
......@@ -25,7 +25,7 @@ def test_serialize_xml():
def test_serialize_xml_attribute():
data = {'@status': 'ok'}
resp = serialize_xml(data)
assert_equals('text/xml', resp.content_type)
assert_equals('text/xml; charset=UTF-8', resp.content_type)
expected = '''<?xml version='1.0' encoding='UTF-8'?>\n<response status="ok" />'''
assert_equals(expected, resp.data)
......@@ -28,7 +28,7 @@ from acoustid.utils import provider
def test_ok():
handler = APIHandler()
resp = handler._ok({'tracks': [{'id': 1, 'name': 'Track 1'}]})
assert_equals('text/xml', resp.content_type)
assert_equals('text/xml; charset=UTF-8', resp.content_type)
expected = '<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n<response status="ok"><tracks><track><id>1</id><name>Track 1</name></track></tracks></response>'
assert_equals(expected, resp.data)
assert_equals('200 OK', resp.status)
......@@ -37,12 +37,12 @@ def test_ok():
def test_error():
handler = APIHandler()
resp = handler._error(123, 'something is wrong')
assert_equals('text/xml', resp.content_type)
assert_equals('text/xml; charset=UTF-8', resp.content_type)
expected = '<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n<response status="error"><error>something is wrong</error></response>'
assert_equals(expected, resp.data)
assert_equals('400 BAD REQUEST', resp.status)
resp = handler._error(234, 'oops', status=500)
assert_equals('text/xml', resp.content_type)
assert_equals('text/xml; charset=UTF-8', resp.content_type)
expected = '<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n<response status="error"><error>oops</error></response>'
assert_equals(expected, resp.data)
assert_equals('500 INTERNAL SERVER ERROR', resp.status)
......@@ -87,7 +87,7 @@ def test_lookup_handler(conn):
# no matches
handler = LookupHandler(connect=provider(conn))
resp = handler.handle(Request(builder.get_environ()))
assert_equals('text/xml', resp.content_type)
assert_equals('text/xml; charset=UTF-8', resp.content_type)
expected = "<?xml version='1.0' encoding='UTF-8'?>\n<response><status>ok</status><results /></response>"
assert_equals(expected, resp.data)
assert_equals('200 OK', resp.status)
......@@ -98,7 +98,7 @@ INSERT INTO fingerprint (length, fingerprint, source_id, track_id)
""", (TEST_1_LENGTH, TEST_1_FP_RAW))
handler = LookupHandler(connect=provider(conn))
resp = handler.handle(Request(builder.get_environ()))
assert_equals('text/xml', resp.content_type)
assert_equals('text/xml; charset=UTF-8', resp.content_type)
expected = "<?xml version='1.0' encoding='UTF-8'?>\n<response><status>ok</status><results><result><score>1.0</score><id>1</id></result></results></response>"
assert_equals(expected, resp.data)
assert_equals('200 OK', resp.status)
......@@ -107,7 +107,7 @@ INSERT INTO fingerprint (length, fingerprint, source_id, track_id)
builder = EnvironBuilder(method='POST', data=values)
handler = LookupHandler(connect=provider(conn))
resp = handler.handle(Request(builder.get_environ()))
assert_equals('text/xml', resp.content_type)
assert_equals('text/xml; charset=UTF-8', resp.content_type)
expected = "<?xml version='1.0' encoding='UTF-8'?>\n<response><status>ok</status><results><result><tracks><track><id>b81f83ee-4da4-11e0-9ed8-0025225356f3</id></track></tracks><score>1.0</score><id>1</id></result></results></response>"
assert_equals(expected, resp.data)
assert_equals('200 OK', resp.status)
......@@ -116,7 +116,7 @@ INSERT INTO fingerprint (length, fingerprint, source_id, track_id)
builder = EnvironBuilder(method='POST', data=values)
handler = LookupHandler(connect=provider(conn))
resp = handler.handle(Request(builder.get_environ()))
assert_equals('text/xml', resp.content_type)
assert_equals('text/xml; charset=UTF-8', resp.content_type)
expected = "<?xml version='1.0' encoding='UTF-8'?>\n<response><status>ok</status><results><result><tracks><track><length>123</length><artist><id>a64796c0-4da4-11e0-bf81-0025225356f3</id><name>Artist A</name></artist><id>b81f83ee-4da4-11e0-9ed8-0025225356f3</id><releases><release><track_num>1</track_num><id>dd6c2cca-a0e9-4cc4-9a5f-7170bd098e23</id><track_count>2</track_count><name>Album A</name></release></releases><name>Track A</name></track></tracks><score>1.0</score><id>1</id></result></results></response>"
assert_equals(expected, resp.data)
assert_equals('200 OK', resp.status)
......@@ -212,7 +212,7 @@ def test_submit_handler(conn):
builder = EnvironBuilder(method='POST', data=values)
handler = SubmitHandler(connect=provider(conn))
resp = handler.handle(Request(builder.get_environ()))
assert_equals('text/xml', resp.content_type)
assert_equals('text/xml; charset=UTF-8', resp.content_type)
expected = "<?xml version='1.0' encoding='UTF-8'?>\n<response><status>ok</status></response>"
assert_equals(expected, resp.data)
assert_equals('200 OK', resp.status)
......
......@@ -28,7 +28,7 @@ from acoustid.utils import provider
def test_ok():
handler = APIHandler()
resp = handler._ok({'tracks': [{'id': 1, 'name': 'Track 1'}]}, 'json')
assert_equals('application/json', resp.content_type)
assert_equals('application/json; charset=UTF-8', resp.content_type)
expected = '{"status": "ok", "tracks": [{"id": 1, "name": "Track 1"}]}'
assert_equals(expected, resp.data)
assert_equals('200 OK', resp.status)
......@@ -37,12 +37,12 @@ def test_ok():
def test_error():
handler = APIHandler()
resp = handler._error(123, 'something is wrong', 'json')
assert_equals('application/json', resp.content_type)
assert_equals('application/json; charset=UTF-8', resp.content_type)
expected = '{"status": "error", "error": {"message": "something is wrong", "code": 123}}'
assert_equals(expected, resp.data)
assert_equals('400 BAD REQUEST', resp.status)
resp = handler._error(234, 'oops', 'json', status=500)
assert_equals('application/json', resp.content_type)
assert_equals('application/json; charset=UTF-8', resp.content_type)
expected = '{"status": "error", "error": {"message": "oops", "code": 234}}'
assert_equals(expected, resp.data)
assert_equals('500 INTERNAL SERVER ERROR', resp.status)
......@@ -106,7 +106,7 @@ def test_apihandler_ws_error(conn):
builder = EnvironBuilder(method='POST', data=values)
handler = WebServiceErrorHandler(connect=provider(conn))
resp = handler.handle(Request(builder.get_environ()))
assert_equals('application/json', resp.content_type)
assert_equals('application/json; charset=UTF-8', resp.content_type)
expected = {
"status": "error",
"error": {
......@@ -118,7 +118,7 @@ def test_apihandler_ws_error(conn):
assert_equals('400 BAD REQUEST', resp.status)
handler = InternalErrorHandler(connect=provider(conn))
resp = handler.handle(Request(builder.get_environ()))
assert_equals('application/json', resp.content_type)
assert_equals('application/json; charset=UTF-8', resp.content_type)
expected = {
"status": "error",
"error": {
......@@ -138,7 +138,7 @@ def test_lookup_handler(conn):
# no matches
handler = LookupHandler(connect=provider(conn))
resp = handler.handle(Request(builder.get_environ()))
assert_equals('application/json', resp.content_type)
assert_equals('application/json; charset=UTF-8', resp.content_type)
expected = {
"status": "ok",
"results": []
......@@ -152,7 +152,7 @@ INSERT INTO fingerprint (length, fingerprint, source_id, track_id)
""", (TEST_1_LENGTH, TEST_1_FP_RAW))
handler = LookupHandler(connect=provider(conn))
resp = handler.handle(Request(builder.get_environ()))
assert_equals('application/json', resp.content_type)
assert_equals('application/json; charset=UTF-8', resp.content_type)
expected = {
"status": "ok",
"results": [{
......@@ -167,7 +167,7 @@ INSERT INTO fingerprint (length, fingerprint, source_id, track_id)
builder = EnvironBuilder(method='POST', data=values)
handler = LookupHandler(connect=provider(conn))
resp = handler.handle(Request(builder.get_environ()))
assert_equals('application/json', resp.content_type)
assert_equals('application/json; charset=UTF-8', resp.content_type)
expected = {
"status": "ok",
"results": [{
......@@ -184,7 +184,7 @@ INSERT INTO fingerprint (length, fingerprint, source_id, track_id)
builder = EnvironBuilder(method='POST', data=values)
handler = LookupHandler(connect=provider(conn))
resp = handler.handle(Request(builder.get_environ()))
assert_equals('application/json', resp.content_type)
assert_equals('application/json; charset=UTF-8', resp.content_type)
expected = {
"status": "ok",
"results": [{
......@@ -220,7 +220,7 @@ INSERT INTO fingerprint (length, fingerprint, source_id, track_id)
builder = EnvironBuilder(method='POST', data=values)
handler = LookupHandler(connect=provider(conn))
resp = handler.handle(Request(builder.get_environ()))
assert_equals('application/json', resp.content_type)
assert_equals('application/json; charset=UTF-8', resp.content_type)
expected = {
"status": "ok",
"results": [{
......@@ -376,7 +376,7 @@ def test_submit_handler(conn):
builder = EnvironBuilder(method='POST', data=values)
handler = SubmitHandler(connect=provider(conn))
resp = handler.handle(Request(builder.get_environ()))
assert_equals('application/json', resp.content_type)
assert_equals('application/json; charset=UTF-8', resp.content_type)
expected = {"status": "ok"}
assert_json_equals(expected, resp.data)
assert_equals('200 OK', resp.status)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment