Adapted the restjson tests to the changes I made for the soap tests
This commit is contained in:
@@ -297,6 +297,7 @@ class ProtocolTestCase(unittest.TestCase):
|
||||
|
||||
def test_setnested(self):
|
||||
value = {'inner': {'aint': 54}}
|
||||
assert self.call('argtypes/setnested',
|
||||
r = self.call('argtypes/setnested',
|
||||
value=(value, NestedOuter),
|
||||
_rt=NestedOuter) == value
|
||||
_rt=NestedOuter)
|
||||
assert r == value
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
import decimal
|
||||
import base64
|
||||
|
||||
import wsme.tests.protocol
|
||||
try:
|
||||
import simplejson as json
|
||||
@@ -5,12 +8,47 @@ except:
|
||||
import json
|
||||
|
||||
import wsme.restjson
|
||||
from wsme.utils import *
|
||||
|
||||
def prepare_value(value, datatype):
|
||||
if datatype in (datetime.date, datetime.time, datetime.datetime):
|
||||
return value.isoformat()
|
||||
if datatype == decimal.Decimal:
|
||||
return str(value)
|
||||
if datatype == wsme.types.binary:
|
||||
return base64.encodestring(value)
|
||||
return value
|
||||
|
||||
def prepare_result(value, datatype):
|
||||
if datatype == datetime.date:
|
||||
return parse_isodate(value)
|
||||
if datatype == datetime.time:
|
||||
return parse_isotime(value)
|
||||
if datatype == datetime.datetime:
|
||||
return parse_isodatetime(value)
|
||||
if hasattr(datatype, '_wsme_attributes'):
|
||||
for name, attr in datatype._wsme_attributes:
|
||||
if name not in value:
|
||||
continue
|
||||
value[name] = prepare_result(value[name], attr.datatype)
|
||||
return value
|
||||
if datatype == wsme.types.binary:
|
||||
return base64.decodestring(value)
|
||||
if type(value) != datatype:
|
||||
return datatype(value)
|
||||
return value
|
||||
|
||||
class TestRestJson(wsme.tests.protocol.ProtocolTestCase):
|
||||
protocol = 'REST+Json'
|
||||
|
||||
def call(self, fpath, **kw):
|
||||
def call(self, fpath, _rt=None, **kw):
|
||||
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)
|
||||
res = self.app.post(
|
||||
'/' + fpath,
|
||||
@@ -22,7 +60,10 @@ class TestRestJson(wsme.tests.protocol.ProtocolTestCase):
|
||||
print "Received:", res.body
|
||||
r = json.loads(res.body)
|
||||
if 'result' in r:
|
||||
return r['result']
|
||||
r = r['result']
|
||||
if _rt and r:
|
||||
r = prepare_result(r, _rt)
|
||||
return r
|
||||
else:
|
||||
raise wsme.tests.protocol.CallException(
|
||||
r['faultcode'],
|
||||
|
||||
Reference in New Issue
Block a user