From 1c978e8414b5841c4caf856c80f385026600f54e Mon Sep 17 00:00:00 2001 From: Michael Gundlach Date: Mon, 27 Sep 2010 12:50:20 -0400 Subject: Support content type detection in serializer --- nova/api/rackspace/__init__.py | 2 +- nova/api/rackspace/servers.py | 5 ++--- nova/tests/api/wsgi_test.py | 33 ++++++++++++++++++++++++++++++--- nova/wsgi.py | 21 ++++++++++++++------- 4 files changed, 47 insertions(+), 14 deletions(-) diff --git a/nova/api/rackspace/__init__.py b/nova/api/rackspace/__init__.py index 5f4020837..736486733 100644 --- a/nova/api/rackspace/__init__.py +++ b/nova/api/rackspace/__init__.py @@ -140,7 +140,7 @@ class APIRouter(wsgi.Router): def __init__(self): mapper = routes.Mapper() - mapper.resource("server", "servers", controller=servers.Controller() + mapper.resource("server", "servers", controller=servers.Controller(), collection={'detail': 'GET'}) mapper.resource("image", "images", controller=images.Controller(), collection={'detail': 'GET'}) diff --git a/nova/api/rackspace/servers.py b/nova/api/rackspace/servers.py index 3ba5af8cf..761ce2895 100644 --- a/nova/api/rackspace/servers.py +++ b/nova/api/rackspace/servers.py @@ -29,7 +29,6 @@ FLAGS = flags.FLAGS class Controller(base.Controller): _serialization_metadata = { 'application/xml': { - "plurals": "servers", "attributes": { "server": [ "id", "imageId", "name", "flavorId", "hostId", "status", "progress", "addresses", "metadata", @@ -39,7 +38,7 @@ class Controller(base.Controller): } def __init__(self): - self.instdir = compute.InstanceDirectory() + self.instdir = None # TODO(cerberus): compute doesn't exist. compute.InstanceDirectory() def index(self, req): allowed_keys = [ 'id', 'name'] @@ -121,7 +120,7 @@ class Controller(base.Controller): 'reservation_id', 'project_id', 'launch_time', 'bridge_name', 'mac_address', 'user_id'] - for key in filtered_keys:: + for key in filtered_keys: del inst[key] if allowed_keys: 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 = '(2,3)' + 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 -- cgit