shore up accept header parsing
This commit is contained in:
commit
779f96cd9c
@ -204,16 +204,15 @@ 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:]
|
||||
if query_format:
|
||||
req.accept = 'application/%s' % query_format.lower()
|
||||
out_content_type = req.accept.best_match(
|
||||
['text/plain', 'application/json',
|
||||
'application/xml', 'text/xml'],
|
||||
default_match='text/plain')
|
||||
account_list = broker.list_containers_iter(limit, marker, prefix,
|
||||
delimiter)
|
||||
if format == 'json':
|
||||
out_content_type = 'application/json'
|
||||
if out_content_type == 'application/json':
|
||||
json_pattern = ['"name":%s', '"count":%s', '"bytes":%s']
|
||||
json_pattern = '{' + ','.join(json_pattern) + '}'
|
||||
json_out = []
|
||||
@ -225,8 +224,7 @@ class AccountController(object):
|
||||
json_out.append(json_pattern %
|
||||
(name, object_count, bytes_used))
|
||||
account_list = '[' + ','.join(json_out) + ']'
|
||||
elif format == 'xml':
|
||||
out_content_type = 'application/xml'
|
||||
elif out_content_type.endswith('/xml'):
|
||||
output_list = ['<?xml version="1.0" encoding="UTF-8"?>',
|
||||
'<account name="%s">' % account]
|
||||
for (name, object_count, bytes_used, is_subdir) in account_list:
|
||||
@ -243,7 +241,6 @@ class AccountController(object):
|
||||
else:
|
||||
if not account_list:
|
||||
return HTTPNoContent(request=req, headers=resp_headers)
|
||||
out_content_type = 'text/plain'
|
||||
account_list = '\n'.join(r[0] for r in account_list) + '\n'
|
||||
ret = Response(body=account_list, request=req, headers=resp_headers)
|
||||
ret.content_type = out_content_type
|
||||
|
@ -278,16 +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:]
|
||||
if query_format:
|
||||
req.accept = 'application/%s' % query_format.lower()
|
||||
out_content_type = req.accept.best_match(
|
||||
['text/plain', 'application/json',
|
||||
'application/xml', 'text/xml'],
|
||||
default_match='text/plain')
|
||||
container_list = broker.list_objects_iter(limit, marker, prefix,
|
||||
delimiter, path)
|
||||
if format == 'json':
|
||||
out_content_type = 'application/json'
|
||||
if out_content_type == 'application/json':
|
||||
json_pattern = ['"name":%s', '"hash":"%s"', '"bytes":%s',
|
||||
'"content_type":%s, "last_modified":"%s"']
|
||||
json_pattern = '{' + ','.join(json_pattern) + '}'
|
||||
@ -307,8 +306,7 @@ class ContainerController(object):
|
||||
content_type,
|
||||
created_at))
|
||||
container_list = '[' + ','.join(json_out) + ']'
|
||||
elif format == 'xml':
|
||||
out_content_type = 'application/xml'
|
||||
elif out_content_type.endswith('/xml'):
|
||||
xml_output = []
|
||||
for (name, created_at, size, content_type, etag) in container_list:
|
||||
# escape name and format date here
|
||||
@ -330,7 +328,6 @@ class ContainerController(object):
|
||||
else:
|
||||
if not container_list:
|
||||
return HTTPNoContent(request=req, headers=resp_headers)
|
||||
out_content_type = 'text/plain'
|
||||
container_list = '\n'.join(r[0] for r in container_list) + '\n'
|
||||
ret = Response(body=container_list, request=req, headers=resp_headers)
|
||||
ret.content_type = out_content_type
|
||||
|
@ -479,18 +479,20 @@ 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',
|
||||
req = Request.blank('/sda1/p/a/jsonc', environ={'REQUEST_METHOD': 'PUT',
|
||||
'HTTP_X_TIMESTAMP': '0'})
|
||||
resp = self.controller.PUT(req)
|
||||
# test an empty container
|
||||
req = Request.blank('/sda1/p/a/c', environ={'REQUEST_METHOD': 'GET'})
|
||||
req = Request.blank('/sda1/p/a/jsonc?format=json',
|
||||
environ={'REQUEST_METHOD': 'GET'})
|
||||
resp = self.controller.GET(req)
|
||||
self.assertEquals(resp.status_int, 204)
|
||||
self.assertEquals(resp.status_int, 200)
|
||||
self.assertEquals(eval(resp.body), [])
|
||||
# fill the container
|
||||
for i in range(3):
|
||||
req = Request.blank('/sda1/p/a/c/%s'%i, environ=
|
||||
req = Request.blank('/sda1/p/a/jsonc/%s'%i, environ=
|
||||
{'REQUEST_METHOD': 'PUT',
|
||||
'HTTP_X_TIMESTAMP': '1',
|
||||
'HTTP_X_CONTENT_TYPE': 'text/plain',
|
||||
@ -514,8 +516,88 @@ class TestContainerController(unittest.TestCase):
|
||||
"bytes":0,
|
||||
"content_type":"text/plain",
|
||||
"last_modified":"1970-01-01T00:00:01"}]
|
||||
|
||||
req = Request.blank('/sda1/p/a/jsonc?format=json',
|
||||
environ={'REQUEST_METHOD': 'GET'})
|
||||
resp = self.controller.GET(req)
|
||||
self.assertEquals(resp.content_type, 'application/json')
|
||||
self.assertEquals(eval(resp.body), json_body)
|
||||
|
||||
for accept in ('application/json', 'application/json;q=1.0,*/*;q=0.9',
|
||||
'*/*;q=0.9,application/json;q=1.0', 'application/*'):
|
||||
req = Request.blank('/sda1/p/a/jsonc',
|
||||
environ={'REQUEST_METHOD': 'GET'})
|
||||
req.accept = accept
|
||||
resp = self.controller.GET(req)
|
||||
self.assertEquals(eval(resp.body), json_body,
|
||||
'Invalid body for Accept: %s' % accept)
|
||||
self.assertEquals(resp.content_type, 'application/json',
|
||||
'Invalid content_type for Accept: %s' % accept)
|
||||
|
||||
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)
|
||||
# test an empty container
|
||||
req = Request.blank('/sda1/p/a/plainc', environ={'REQUEST_METHOD': 'GET'})
|
||||
resp = self.controller.GET(req)
|
||||
self.assertEquals(resp.status_int, 204)
|
||||
# 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', '*/*',
|
||||
'text/plain,application/xml'):
|
||||
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,
|
||||
'Invalid body for Accept: %s' % accept)
|
||||
self.assertEquals(resp.content_type, 'text/plain',
|
||||
'Invalid content_type for Accept: %s' % accept)
|
||||
|
||||
# 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 = '<?xml version="1.0" encoding="UTF-8"?>\n' \
|
||||
'<container name="c">' \
|
||||
'<container name="xmlc">' \
|
||||
'<object><name>0</name><hash>x</hash><bytes>0</bytes>' \
|
||||
'<content_type>text/plain</content_type>' \
|
||||
'<last_modified>1970-01-01T00:00:01' \
|
||||
@ -529,46 +611,30 @@ class TestContainerController(unittest.TestCase):
|
||||
'<last_modified>1970-01-01T00:00:01' \
|
||||
'</last_modified></object>' \
|
||||
'</container>'
|
||||
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'
|
||||
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', 'application/xml,text/xml'):
|
||||
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,
|
||||
'Invalid body for Accept: %s' % xml_accept)
|
||||
self.assertEquals(resp.content_type, 'application/xml',
|
||||
'Invalid content_type for Accept: %s' % xml_accept)
|
||||
|
||||
req = Request.blank('/sda1/p/a/xmlc',
|
||||
environ={'REQUEST_METHOD': 'GET'})
|
||||
req.accept = 'text/xml'
|
||||
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.content_type, 'text/xml')
|
||||
self.assertEquals(resp.body, xml_body)
|
||||
|
||||
def test_GET_marker(self):
|
||||
# make a container
|
||||
|
Loading…
x
Reference in New Issue
Block a user