diff --git a/wsme/__init__.py b/wsme/__init__.py index 7e2625c..31b424a 100644 --- a/wsme/__init__.py +++ b/wsme/__init__.py @@ -1 +1,2 @@ from controller import * +import restjson diff --git a/wsme/controller.py b/wsme/controller.py index 1293dd0..1b4d774 100644 --- a/wsme/controller.py +++ b/wsme/controller.py @@ -79,11 +79,11 @@ class validate(object): class WSRoot(object): def __init__(self, protocols=None): if protocols is None: - protocols = registered_protocols.values() + protocols = registered_protocols.keys() self.protocols = {} for protocol in protocols: if isinstance(protocol, str): - protocol = registered_protocols[protocol] + protocol = registered_protocols[protocol]() self.protocols[protocol.name] = protocol def _handle_request(self, request): diff --git a/wsme/rest.py b/wsme/rest.py index 0089da3..ca7c2f9 100644 --- a/wsme/rest.py +++ b/wsme/rest.py @@ -10,4 +10,11 @@ class RestProtocol(object): def handle(self, root, request): path = request.path.split('/') + a = root + for name in path: + a = getattr(a, name) + if not hasattr(a, '_ews_description'): + raise ValueError('Invalid path') + fonc = a + kw = self.get_args(req) diff --git a/wsme/restjson.py b/wsme/restjson.py index c673bca..343a136 100644 --- a/wsme/restjson.py +++ b/wsme/restjson.py @@ -1,7 +1,13 @@ +from wsme.rest import RestProtocol +from wsme.controller import register_protocol + class RestJsonProtocol(RestProtocol): name = 'REST+Json' dataformat = 'json' content_types = [None, 'application/json', 'text/json'] + def get_args(self, req): + kw = json.loads(req.body) -controller.register_protocol(RestJsonProtocol) + +register_protocol(RestJsonProtocol) diff --git a/wsme/tests/protocol.py b/wsme/tests/protocol.py new file mode 100644 index 0000000..19f0aef --- /dev/null +++ b/wsme/tests/protocol.py @@ -0,0 +1,26 @@ +import unittest +from webob.dec import wsgify +from webtest import TestApp + +from wsme import * + +class WSTestRoot(WSRoot): + def reset(self): + self.touched = False + + @expose() + def touch(self): + self.touched = True + +class TestProtocol(unittest.TestCase): + def setUp(self): + self.root = WSTestRoot([self.protocol]) + + self.app = TestApp(wsgify(self.root._handle_request)) + + def _call(self, fpath, **kw): + pass + + def test_touch(self): + assert self.call('touch') is None + diff --git a/wsme/tests/test_restjson.py b/wsme/tests/test_restjson.py new file mode 100644 index 0000000..2018ec5 --- /dev/null +++ b/wsme/tests/test_restjson.py @@ -0,0 +1,14 @@ +from wsme.tests.protocol import TestProtocol +import json + +class TestRestJson(TestProtocol): + protocol = 'REST+Json' + def call(self, fpath, **kw): + content = json.dumps(kw) + res = self.app.post( + '/' + fpath, + content, + headers={ + 'Content-Type': 'application/json' + }) + return json.loads(res.body)