summaryrefslogtreecommitdiffstats
path: root/nova
diff options
context:
space:
mode:
authorCerberus <matt.dietz@rackspace.com>2010-09-27 17:01:59 -0500
committerCerberus <matt.dietz@rackspace.com>2010-09-27 17:01:59 -0500
commitf32e7054052f9daca7dcb92b0fb0ff0525c073be (patch)
tree02b0b2e4bf7d2cb5921d21b4627d5729094b288d /nova
parent0e6c3b6034ef4927e381b231bf120a4980512c4e (diff)
parent70516be4ff02cd82dce82ac1950fc55e87bab9ec (diff)
downloadnova-f32e7054052f9daca7dcb92b0fb0ff0525c073be.tar.gz
nova-f32e7054052f9daca7dcb92b0fb0ff0525c073be.tar.xz
nova-f32e7054052f9daca7dcb92b0fb0ff0525c073be.zip
Merge from lp:~gundlach/nova/servers_api
Diffstat (limited to 'nova')
-rw-r--r--nova/tests/api/wsgi_test.py33
-rw-r--r--nova/wsgi.py21
2 files changed, 44 insertions, 10 deletions
diff --git a/nova/tests/api/wsgi_test.py b/nova/tests/api/wsgi_test.py
index 786dc1bce..145b1bfee 100644
--- a/nova/tests/api/wsgi_test.py
+++ b/nova/tests/api/wsgi_test.py
@@ -91,6 +91,33 @@ class Test(unittest.TestCase):
result = webob.Request.blank('/test/123').get_response(Router())
self.assertNotEqual(result.body, "123")
- def test_serializer(self):
- # TODO(eday): Placeholder for serializer testing.
- pass
+
+class SerializerTest(unittest.TestCase):
+
+ def match(self, url, accept, expect):
+ input_dict = dict(servers=dict(a=(2,3)))
+ expected_xml = '<servers><a>(2,3)</a></servers>'
+ expected_json = '{"servers":{"a":[2,3]}}'
+ req = webob.Request.blank(url, headers=dict(Accept=accept))
+ result = wsgi.Serializer(req.environ).to_content_type(input_dict)
+ result = result.replace('\n', '').replace(' ', '')
+ if expect == 'xml':
+ self.assertEqual(result, expected_xml)
+ elif expect == 'json':
+ self.assertEqual(result, expected_json)
+ else:
+ raise "Bad expect value"
+
+ def test_basic(self):
+ self.match('/servers/4.json', None, expect='json')
+ self.match('/servers/4', 'application/json', expect='json')
+ self.match('/servers/4', 'application/xml', expect='xml')
+ self.match('/servers/4.xml', None, expect='xml')
+
+ def test_defaults_to_json(self):
+ self.match('/servers/4', None, expect='json')
+ self.match('/servers/4', 'text/html', expect='json')
+
+ def test_suffix_takes_precedence_over_accept_header(self):
+ self.match('/servers/4.xml', 'application/json', expect='xml')
+ self.match('/servers/4.xml.', 'application/json', expect='json')
diff --git a/nova/wsgi.py b/nova/wsgi.py
index 8a4e2a9f4..ac319db40 100644
--- a/nova/wsgi.py
+++ b/nova/wsgi.py
@@ -29,6 +29,7 @@ import eventlet.wsgi
eventlet.patcher.monkey_patch(all=False, socket=True)
import routes
import routes.middleware
+import webob
import webob.dec
import webob.exc
@@ -239,11 +240,19 @@ class Serializer(object):
'metadata' is an optional dict mapping MIME types to information
needed to serialize a dictionary to that type.
"""
- self.environ = environ
self.metadata = metadata or {}
- self._methods = {
- 'application/json': self._to_json,
- 'application/xml': self._to_xml}
+ req = webob.Request(environ)
+ suffix = req.path_info.split('.')[-1].lower()
+ if suffix == 'json':
+ self.handler = self._to_json
+ elif suffix == 'xml':
+ self.handler = self._to_xml
+ elif 'application/json' in req.accept:
+ self.handler = self._to_json
+ elif 'application/xml' in req.accept:
+ self.handler = self._to_xml
+ else:
+ self.handler = self._to_json # default
def to_content_type(self, data):
"""
@@ -251,9 +260,7 @@ class Serializer(object):
will be decided based on the Content Type requested in self.environ:
by Accept: header, or by URL suffix.
"""
- mimetype = 'application/xml'
- # TODO(gundlach): determine mimetype from request
- return self._methods.get(mimetype, repr)(data)
+ return self.handler(data)
def _to_json(self, data):
import json