Handle [] {} for body in rest protocols
This change allow to use [] or {} for the definition of the body in rest protocols. Fixes bug #1233219 Change-Id: Ib96f0487dd7d78bd657f6d4b3facbd8b611f8702
This commit is contained in:
parent
259fea9529
commit
664c214dbf
@ -105,6 +105,7 @@ class FunctionDefinition(object):
|
|||||||
|
|
||||||
def resolve_types(self, registry):
|
def resolve_types(self, registry):
|
||||||
self.return_type = registry.resolve_type(self.return_type)
|
self.return_type = registry.resolve_type(self.return_type)
|
||||||
|
self.body_type = registry.resolve_type(self.body_type)
|
||||||
for arg in self.arguments:
|
for arg in self.arguments:
|
||||||
arg.resolve_type(registry)
|
arg.resolve_type(registry)
|
||||||
|
|
||||||
|
@ -308,6 +308,30 @@ class ArgTypes(object):
|
|||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
class BodyTypes(object):
|
||||||
|
def assertEquals(self, a, b):
|
||||||
|
if not (a == b):
|
||||||
|
raise AssertionError('%s != %s' % (a, b))
|
||||||
|
|
||||||
|
@expose(int, body={wsme.types.text: int})
|
||||||
|
@validate(int)
|
||||||
|
def setdict(self, body):
|
||||||
|
print(body)
|
||||||
|
self.assertEquals(type(body), dict)
|
||||||
|
self.assertEquals(type(body['test']), int)
|
||||||
|
self.assertEquals(body['test'], 10)
|
||||||
|
return body['test']
|
||||||
|
|
||||||
|
@expose(int, body=[int])
|
||||||
|
@validate(int)
|
||||||
|
def setlist(self, body):
|
||||||
|
print(body)
|
||||||
|
self.assertEquals(type(body), list)
|
||||||
|
self.assertEquals(type(body[0]), int)
|
||||||
|
self.assertEquals(body[0], 10)
|
||||||
|
return body[0]
|
||||||
|
|
||||||
|
|
||||||
class WithErrors(object):
|
class WithErrors(object):
|
||||||
@expose()
|
@expose()
|
||||||
def divide_by_zero(self):
|
def divide_by_zero(self):
|
||||||
@ -324,6 +348,7 @@ class MiscFunctions(object):
|
|||||||
class WSTestRoot(WSRoot):
|
class WSTestRoot(WSRoot):
|
||||||
argtypes = ArgTypes()
|
argtypes = ArgTypes()
|
||||||
returntypes = ReturnTypes()
|
returntypes = ReturnTypes()
|
||||||
|
bodytypes = BodyTypes()
|
||||||
witherrors = WithErrors()
|
witherrors = WithErrors()
|
||||||
nested = NestedOuterApi()
|
nested = NestedOuterApi()
|
||||||
misc = MiscFunctions()
|
misc = MiscFunctions()
|
||||||
@ -653,3 +678,15 @@ class ProtocolTestCase(unittest.TestCase):
|
|||||||
res = self.call('argtypes/setdatetime', _accept="text/html",
|
res = self.call('argtypes/setdatetime', _accept="text/html",
|
||||||
_no_result_decode=True)
|
_no_result_decode=True)
|
||||||
self.assertEquals(res.content_type, 'text/html')
|
self.assertEquals(res.content_type, 'text/html')
|
||||||
|
|
||||||
|
|
||||||
|
class RestOnlyProtocolTestCase(ProtocolTestCase):
|
||||||
|
def test_body_list(self):
|
||||||
|
r = self.call('bodytypes/setlist', body=([10], [int]), _rt=int)
|
||||||
|
self.assertEqual(r, 10)
|
||||||
|
|
||||||
|
def test_body_dict(self):
|
||||||
|
r = self.call('bodytypes/setdict',
|
||||||
|
body=({'test': 10}, {wsme.types.text: int}),
|
||||||
|
_rt=int)
|
||||||
|
self.assertEqual(r, 10)
|
||||||
|
@ -140,19 +140,27 @@ class MiniCrud(object):
|
|||||||
wsme.tests.protocol.WSTestRoot.crud = MiniCrud()
|
wsme.tests.protocol.WSTestRoot.crud = MiniCrud()
|
||||||
|
|
||||||
|
|
||||||
class TestRestJson(wsme.tests.protocol.ProtocolTestCase):
|
class TestRestJson(wsme.tests.protocol.RestOnlyProtocolTestCase):
|
||||||
protocol = 'restjson'
|
protocol = 'restjson'
|
||||||
|
|
||||||
def call(self, fpath, _rt=None, _accept=None, _no_result_decode=False,
|
def call(self, fpath, _rt=None, _accept=None, _no_result_decode=False,
|
||||||
**kw):
|
body=None, **kw):
|
||||||
for key in kw:
|
if body:
|
||||||
if isinstance(kw[key], tuple):
|
if isinstance(body, tuple):
|
||||||
value, datatype = kw[key]
|
body, datatype = body
|
||||||
else:
|
else:
|
||||||
value = kw[key]
|
datatype = type(body)
|
||||||
datatype = type(value)
|
body = prepare_value(body, datatype)
|
||||||
kw[key] = prepare_value(value, datatype)
|
content = json.dumps(body)
|
||||||
content = json.dumps(kw)
|
else:
|
||||||
|
for key in kw:
|
||||||
|
if isinstance(kw[key], tuple):
|
||||||
|
value, datatype = kw[key]
|
||||||
|
else:
|
||||||
|
value = kw[key]
|
||||||
|
datatype = type(value)
|
||||||
|
kw[key] = prepare_value(value, datatype)
|
||||||
|
content = json.dumps(kw)
|
||||||
headers = {
|
headers = {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
}
|
}
|
||||||
|
@ -117,12 +117,15 @@ def loadxml(el, datatype):
|
|||||||
return datatype(el.text)
|
return datatype(el.text)
|
||||||
|
|
||||||
|
|
||||||
class TestRestXML(wsme.tests.protocol.ProtocolTestCase):
|
class TestRestXML(wsme.tests.protocol.RestOnlyProtocolTestCase):
|
||||||
protocol = 'restxml'
|
protocol = 'restxml'
|
||||||
|
|
||||||
def call(self, fpath, _rt=None, _accept=None, _no_result_decode=False,
|
def call(self, fpath, _rt=None, _accept=None, _no_result_decode=False,
|
||||||
**kw):
|
body=None, **kw):
|
||||||
el = dumpxml('parameters', kw)
|
if body:
|
||||||
|
el = dumpxml('body', body)
|
||||||
|
else:
|
||||||
|
el = dumpxml('parameters', kw)
|
||||||
content = et.tostring(el)
|
content = et.tostring(el)
|
||||||
headers = {
|
headers = {
|
||||||
'Content-Type': 'text/xml',
|
'Content-Type': 'text/xml',
|
||||||
|
@ -18,7 +18,7 @@ class TestSpore(unittest.TestCase):
|
|||||||
|
|
||||||
spore = json.loads(spore)
|
spore = json.loads(spore)
|
||||||
|
|
||||||
assert len(spore['methods']) == 47, str(len(spore['methods']))
|
assert len(spore['methods']) == 49, str(len(spore['methods']))
|
||||||
|
|
||||||
m = spore['methods']['argtypes_setbytesarray']
|
m = spore['methods']['argtypes_setbytesarray']
|
||||||
assert m['path'] == 'argtypes/setbytesarray', m['path']
|
assert m['path'] == 'argtypes/setbytesarray', m['path']
|
||||||
|
@ -402,7 +402,7 @@ class TestSOAP(wsme.tests.protocol.ProtocolTestCase):
|
|||||||
|
|
||||||
assert len(sd.ports) == 1
|
assert len(sd.ports) == 1
|
||||||
port, methods = sd.ports[0]
|
port, methods = sd.ports[0]
|
||||||
self.assertEquals(len(methods), 47)
|
self.assertEquals(len(methods), 49)
|
||||||
|
|
||||||
methods = dict(methods)
|
methods = dict(methods)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user