diff --git a/swift/account/server.py b/swift/account/server.py index 18bd3f212b..a51bf64054 100644 --- a/swift/account/server.py +++ b/swift/account/server.py @@ -199,12 +199,12 @@ class AccountController(object): except UnicodeDecodeError, err: return HTTPBadRequest(body='parameters not utf8', content_type='text/plain', request=req) - header_format = req.accept.first_match(['text/plain', - 'application/json', - 'application/xml']) - format = query_format if query_format else header_format - if format.startswith('application/'): - format = format[12:] + header_format = req.accept.best_match(['text/plain', + 'application/json', + 'application/xml']) + format = query_format or header_format or 'text/plain' + if '/' in format: + format = format.split('/')[-1] account_list = broker.list_containers_iter(limit, marker, prefix, delimiter) if format == 'json': diff --git a/swift/container/server.py b/swift/container/server.py index ff8dc76684..8ec5573213 100644 --- a/swift/container/server.py +++ b/swift/container/server.py @@ -278,14 +278,15 @@ class ContainerController(object): except UnicodeDecodeError, err: return HTTPBadRequest(body='parameters not utf8', content_type='text/plain', request=req) - header_format = req.accept.first_match(['text/plain', - 'application/json', - 'application/xml']) - format = query_format if query_format else header_format - if format.startswith('application/'): - format = format[12:] + header_format = req.accept.best_match(['text/plain', + 'application/json', + 'application/xml']) + format = query_format or header_format or 'text/plain' + if '/' in format: + format = format.split('/')[-1] container_list = broker.list_objects_iter(limit, marker, prefix, delimiter, path) + print "format is %s" % format if format == 'json': out_content_type = 'application/json' json_pattern = ['"name":%s', '"hash":"%s"', '"bytes":%s', diff --git a/test/unit/container/test_server.py b/test/unit/container/test_server.py index 7aaee688e8..8a2d79c926 100644 --- a/test/unit/container/test_server.py +++ b/test/unit/container/test_server.py @@ -479,7 +479,7 @@ class TestContainerController(unittest.TestCase): resp = self.controller.GET(req) self.assertEquals(resp.status_int, 412) - def test_GET_format(self): + def test_GET_json(self): # make a container req = Request.blank('/sda1/p/a/c', environ={'REQUEST_METHOD': 'PUT', 'HTTP_X_TIMESTAMP': '0'}) @@ -514,8 +514,80 @@ class TestContainerController(unittest.TestCase): "bytes":0, "content_type":"text/plain", "last_modified":"1970-01-01T00:00:01"}] + req = Request.blank('/sda1/p/a/c?format=json', environ={'REQUEST_METHOD': 'GET'}) + resp = self.controller.GET(req) + self.assertEquals(resp.content_type, 'application/json') + result = eval(resp.body) + self.assertEquals(result, json_body) + req = Request.blank('/sda1/p/a/c', environ={'REQUEST_METHOD': 'GET'}) + req.accept = 'application/json' + resp = self.controller.GET(req) + self.assertEquals(resp.content_type, 'application/json') + result = eval(resp.body) + self.assertEquals(result, json_body) + req = Request.blank('/sda1/p/a/c', environ={'REQUEST_METHOD': 'GET'}) + req.accept = 'application/*' + resp = self.controller.GET(req) + result = eval(resp.body) + self.assertEquals(result, json_body) + + def test_GET_plain(self): + # make a container + req = Request.blank('/sda1/p/a/plainc', environ={'REQUEST_METHOD': 'PUT', + 'HTTP_X_TIMESTAMP': '0'}) + resp = self.controller.PUT(req) + # fill the container + for i in range(3): + req = Request.blank('/sda1/p/a/plainc/%s'%i, environ= + {'REQUEST_METHOD': 'PUT', + 'HTTP_X_TIMESTAMP': '1', + 'HTTP_X_CONTENT_TYPE': 'text/plain', + 'HTTP_X_ETAG': 'x', + 'HTTP_X_SIZE': 0}) + resp = self.controller.PUT(req) + self.assertEquals(resp.status_int, 201) + plain_body = '0\n1\n2\n' + + req = Request.blank('/sda1/p/a/plainc', + environ={'REQUEST_METHOD': 'GET'}) + resp = self.controller.GET(req) + self.assertEquals(resp.content_type, 'text/plain') + self.assertEquals(resp.body, plain_body) + + for accept in ('', 'text/plain', 'application/xml;q=0.8,*/*;q=0.9', + '*/*;q=0.9,application/xml;q=0.8', '*/*'): + req = Request.blank('/sda1/p/a/plainc', + environ={'REQUEST_METHOD': 'GET'}) + req.accept = accept + resp = self.controller.GET(req) + self.assertEquals(resp.body, plain_body) + self.assertEquals(resp.content_type, 'text/plain') + + # test conflicting formats + req = Request.blank('/sda1/p/a/plainc?format=plain', + environ={'REQUEST_METHOD': 'GET'}) + req.accept = 'application/json' + resp = self.controller.GET(req) + self.assertEquals(resp.content_type, 'text/plain') + self.assertEquals(resp.body, plain_body) + + def test_GET_xml(self): + # make a container + req = Request.blank('/sda1/p/a/xmlc', environ={'REQUEST_METHOD': 'PUT', + 'HTTP_X_TIMESTAMP': '0'}) + resp = self.controller.PUT(req) + # fill the container + for i in range(3): + req = Request.blank('/sda1/p/a/xmlc/%s'%i, environ= + {'REQUEST_METHOD': 'PUT', + 'HTTP_X_TIMESTAMP': '1', + 'HTTP_X_CONTENT_TYPE': 'text/plain', + 'HTTP_X_ETAG': 'x', + 'HTTP_X_SIZE': 0}) + resp = self.controller.PUT(req) + self.assertEquals(resp.status_int, 201) xml_body = '\n' \ - '' \ + '' \ '0x0' \ 'text/plain' \ '1970-01-01T00:00:01' \ @@ -529,46 +601,21 @@ class TestContainerController(unittest.TestCase): '1970-01-01T00:00:01' \ '' \ '' - plain_body = '0\n1\n2\n' - req = Request.blank('/sda1/p/a/c?format=json', environ={'REQUEST_METHOD': 'GET'}) - resp = self.controller.GET(req) - self.assertEquals(resp.content_type, 'application/json') - result = eval(resp.body) - self.assertEquals(result, json_body) - req = Request.blank('/sda1/p/a/c?format=xml', environ={'REQUEST_METHOD': 'GET'}) + # tests + req = Request.blank('/sda1/p/a/xmlc?format=xml', + environ={'REQUEST_METHOD': 'GET'}) resp = self.controller.GET(req) self.assertEquals(resp.content_type, 'application/xml') - result = resp.body - self.assertEquals(result, xml_body) - req = Request.blank('/sda1/p/a/c', environ={'REQUEST_METHOD': 'GET'}) - req.accept = 'application/json' - resp = self.controller.GET(req) - self.assertEquals(resp.content_type, 'application/json') - result = eval(resp.body) - self.assertEquals(result, json_body) - req = Request.blank('/sda1/p/a/c', environ={'REQUEST_METHOD': 'GET'}) - req.accept = '*/*' - resp = self.controller.GET(req) - self.assertEquals(resp.content_type, 'text/plain') - result = resp.body - self.assertEquals(result, plain_body) - req = Request.blank('/sda1/p/a/c', environ={'REQUEST_METHOD': 'GET'}) - req.accept = 'application/*' - resp = self.controller.GET(req) - result = eval(resp.body) - self.assertEquals(result, json_body) - req = Request.blank('/sda1/p/a/c', environ={'REQUEST_METHOD': 'GET'}) - req.accept = 'application/xml' - resp = self.controller.GET(req) - result = resp.body - self.assertEquals(result, xml_body) - # test conflicting formats - req = Request.blank('/sda1/p/a/c?format=plain', environ={'REQUEST_METHOD': 'GET'}) - req.accept = 'application/json' - resp = self.controller.GET(req) - self.assertEquals(resp.content_type, 'text/plain') - result = resp.body - self.assertEquals(result, plain_body) + self.assertEquals(resp.body, xml_body) + + for xml_accept in ('application/xml', 'application/xml;q=1.0,*/*;q=0.9', + '*/*;q=0.9,application/xml;q=1.0'): + req = Request.blank('/sda1/p/a/xmlc', + environ={'REQUEST_METHOD': 'GET'}) + req.accept = xml_accept + resp = self.controller.GET(req) + self.assertEquals(resp.body, xml_body) + self.assertEquals(resp.content_type, 'application/xml') def test_GET_marker(self): # make a container