From 664c214dbf2dd33e8fd6a78ca828c02bb1a91c1c Mon Sep 17 00:00:00 2001 From: Mehdi Abaakouk Date: Mon, 30 Sep 2013 17:33:57 +0200 Subject: [PATCH] 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 --- wsme/api.py | 1 + wsme/tests/protocol.py | 37 +++++++++++++++++++++++++++++++++++++ wsme/tests/test_restjson.py | 26 +++++++++++++++++--------- wsme/tests/test_restxml.py | 9 ++++++--- wsme/tests/test_spore.py | 2 +- wsmeext/tests/test_soap.py | 2 +- 6 files changed, 63 insertions(+), 14 deletions(-) diff --git a/wsme/api.py b/wsme/api.py index 6bd181d..b21eff1 100644 --- a/wsme/api.py +++ b/wsme/api.py @@ -105,6 +105,7 @@ class FunctionDefinition(object): def resolve_types(self, registry): self.return_type = registry.resolve_type(self.return_type) + self.body_type = registry.resolve_type(self.body_type) for arg in self.arguments: arg.resolve_type(registry) diff --git a/wsme/tests/protocol.py b/wsme/tests/protocol.py index 98f52f2..35a744d 100644 --- a/wsme/tests/protocol.py +++ b/wsme/tests/protocol.py @@ -308,6 +308,30 @@ class ArgTypes(object): 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): @expose() def divide_by_zero(self): @@ -324,6 +348,7 @@ class MiscFunctions(object): class WSTestRoot(WSRoot): argtypes = ArgTypes() returntypes = ReturnTypes() + bodytypes = BodyTypes() witherrors = WithErrors() nested = NestedOuterApi() misc = MiscFunctions() @@ -653,3 +678,15 @@ class ProtocolTestCase(unittest.TestCase): res = self.call('argtypes/setdatetime', _accept="text/html", _no_result_decode=True) 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) diff --git a/wsme/tests/test_restjson.py b/wsme/tests/test_restjson.py index 2f5b992..dfbdf74 100644 --- a/wsme/tests/test_restjson.py +++ b/wsme/tests/test_restjson.py @@ -140,19 +140,27 @@ class MiniCrud(object): wsme.tests.protocol.WSTestRoot.crud = MiniCrud() -class TestRestJson(wsme.tests.protocol.ProtocolTestCase): +class TestRestJson(wsme.tests.protocol.RestOnlyProtocolTestCase): protocol = 'restjson' def call(self, fpath, _rt=None, _accept=None, _no_result_decode=False, - **kw): - for key in kw: - if isinstance(kw[key], tuple): - value, datatype = kw[key] + body=None, **kw): + if body: + if isinstance(body, tuple): + body, datatype = body else: - value = kw[key] - datatype = type(value) - kw[key] = prepare_value(value, datatype) - content = json.dumps(kw) + datatype = type(body) + body = prepare_value(body, datatype) + content = json.dumps(body) + 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 = { 'Content-Type': 'application/json', } diff --git a/wsme/tests/test_restxml.py b/wsme/tests/test_restxml.py index c0ec720..aab312b 100644 --- a/wsme/tests/test_restxml.py +++ b/wsme/tests/test_restxml.py @@ -117,12 +117,15 @@ def loadxml(el, datatype): return datatype(el.text) -class TestRestXML(wsme.tests.protocol.ProtocolTestCase): +class TestRestXML(wsme.tests.protocol.RestOnlyProtocolTestCase): protocol = 'restxml' def call(self, fpath, _rt=None, _accept=None, _no_result_decode=False, - **kw): - el = dumpxml('parameters', kw) + body=None, **kw): + if body: + el = dumpxml('body', body) + else: + el = dumpxml('parameters', kw) content = et.tostring(el) headers = { 'Content-Type': 'text/xml', diff --git a/wsme/tests/test_spore.py b/wsme/tests/test_spore.py index d356f70..03b0228 100644 --- a/wsme/tests/test_spore.py +++ b/wsme/tests/test_spore.py @@ -18,7 +18,7 @@ class TestSpore(unittest.TestCase): 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'] assert m['path'] == 'argtypes/setbytesarray', m['path'] diff --git a/wsmeext/tests/test_soap.py b/wsmeext/tests/test_soap.py index 34dafcb..73a2cb9 100644 --- a/wsmeext/tests/test_soap.py +++ b/wsmeext/tests/test_soap.py @@ -402,7 +402,7 @@ class TestSOAP(wsme.tests.protocol.ProtocolTestCase): assert len(sd.ports) == 1 port, methods = sd.ports[0] - self.assertEquals(len(methods), 47) + self.assertEquals(len(methods), 49) methods = dict(methods)