Start implementing the rest json proto

--HG--
rename : wsme/tests/__init__.py => wsme/tests/protocol.py
This commit is contained in:
Christophe de Vienne
2011-09-18 22:19:38 +02:00
parent e8129df805
commit 3fa27294de
6 changed files with 57 additions and 3 deletions

View File

@@ -1 +1,2 @@
from controller import *
import restjson

View File

@@ -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):

View File

@@ -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)

View File

@@ -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)

26
wsme/tests/protocol.py Normal file
View File

@@ -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

View File

@@ -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)