wswe.controller code coverage is back to 100%

This commit is contained in:
Christophe de Vienne 2011-10-11 13:30:02 +02:00
parent 4ce24d6b5c
commit fe12f239e5
6 changed files with 86 additions and 15 deletions

View File

@ -81,6 +81,7 @@ class FunctionDefinition(object):
return arg
return None
def register_protocol(protocol):
global registered_protocols
registered_protocols[protocol.name] = protocol
@ -155,7 +156,9 @@ class WSRoot(object):
def _select_protocol(self, request):
log.debug("Selecting a protocol for the following request :\n"
"headers: %s\nbody: %s", request.headers,
len(request.body) > 512 and request.body[:512] or request.body)
len(request.body) > 512
and request.body[:512]
or request.body)
protocol = None
if 'wsmeproto' in request.params:
protocol = self.protocols[request.params['wsmeproto']]
@ -176,7 +179,8 @@ class WSRoot(object):
msg = ("None of the following protocols can handle this "
"request : %s" % ','.join(self.protocols.keys()))
res.status = 500
res.text = msg
res.content_type = 'text/plain'
res.body = msg
log.error(msg)
return res
path = protocol.extract_path(request)

View File

@ -229,7 +229,8 @@ class ProtocolTestCase(unittest.TestCase):
assert "No error raised"
except CallException, e:
assert e.faultcode == 'Client'
assert e.faultstring.lower() == u'unknown function name: invalid_function'
assert e.faultstring.lower() == \
u'unknown function name: invalid_function'
def test_serverside_error(self):
try:
@ -242,7 +243,7 @@ class ProtocolTestCase(unittest.TestCase):
def test_touch(self):
r = self.call('touch')
assert r is None, r
assert r is None, r
def test_return_str(self):
r = self.call('returntypes/getstr')
@ -369,3 +370,17 @@ class ProtocolTestCase(unittest.TestCase):
value=(value, [NestedOuter]),
_rt=[NestedOuter])
assert r == value
def test_missing_argument(self):
try:
r = self.call('argtypes/setdatetime')
assert "No error raised"
except CallException, e:
print e
assert e.faultcode == 'Client'
assert e.faultstring == u'Missing argument: "value"'
def test_html_format(self):
res = self.call('argtypes/setdatetime', _accept="text/html",
_no_result_decode=True)
assert res.content_type == 'text/html', res.content_type

View File

@ -73,7 +73,7 @@ class TestController(unittest.TestCase):
r = WSRoot()
assert len(r.protocols) == 0
r.addprotocol('dummy')
assert r.protocols['dummy']
@ -134,3 +134,32 @@ class TestController(unittest.TestCase):
assert p.lastreq.path == '/touch'
assert p.hits == 2
def test_no_available_protocol(self):
r = WSRoot()
app = webtest.TestApp(r)
res = app.get('/', expect_errors=True)
assert res.status_int == 500
print res.body
assert res.body.find(
"None of the following protocols can handle this request") != -1
def test_return_content_type_guess(self):
class DummierProto(DummyProtocol):
content_types = ['text/xml', 'text/plain']
r = WSRoot([DummierProto()])
app = webtest.TestApp(r)
res = app.get('/', expect_errors=True, headers={
'Accept': 'text/xml,q=0.8'})
assert res.status_int == 400
assert res.content_type == 'text/xml', res.content_type
res = app.get('/', expect_errors=True, headers={
'Accept': 'text/plain'})
assert res.status_int == 400
assert res.content_type == 'text/plain', res.content_type

View File

@ -49,7 +49,8 @@ def prepare_result(value, datatype):
class TestRestJson(wsme.tests.protocol.ProtocolTestCase):
protocol = 'REST+Json'
def call(self, fpath, _rt=None, **kw):
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]
@ -58,14 +59,21 @@ class TestRestJson(wsme.tests.protocol.ProtocolTestCase):
datatype = type(value)
kw[key] = prepare_value(value, datatype)
content = json.dumps(kw)
headers = {
'Content-Type': 'application/json',
}
if _accept is not None:
headers["Accept"] = _accept
res = self.app.post(
'/' + fpath,
content,
headers={
'Content-Type': 'application/json',
},
headers=headers,
expect_errors=True)
print "Received:", res.body
if _no_result_decode:
return res
r = json.loads(res.body)
if 'result' in r:
r = r['result']

View File

@ -72,17 +72,25 @@ def loadxml(el, datatype):
class TestRestXML(wsme.tests.protocol.ProtocolTestCase):
protocol = 'REST+XML'
def call(self, fpath, _rt=None, **kw):
def call(self, fpath, _rt=None, _accept=None,
_no_result_decode=False, **kw):
el = dumpxml('parameters', kw)
content = et.tostring(el)
headers = {
'Content-Type': 'text/xml',
}
if _accept is not None:
headers['Accept'] = _accept
res = self.app.post(
'/' + fpath,
content,
headers={
'Content-Type': 'text/xml',
},
headers=headers,
expect_errors=True)
print "Received:", res.body
if _no_result_decode:
return res
el = et.fromstring(res.body)
if el.tag == 'error':
raise wsme.tests.protocol.CallException(

View File

@ -145,7 +145,8 @@ class TestSOAP(wsme.tests.protocol.ProtocolTestCase):
print res.body
assert res.status.startswith('200')
def call(self, fpath, _rt=None, **kw):
def call(self, fpath, _rt=None, _accept=None,
_no_result_decode=False, **kw):
path = fpath.strip('/').split('/')
# get the actual definition so we can build the adequate request
if kw:
@ -159,11 +160,17 @@ class TestSOAP(wsme.tests.protocol.ProtocolTestCase):
methodname = ''.join((i.capitalize() for i in path))
message = build_soap_message(methodname, params)
print message
headers = {"Content-Type": "application/soap+xml; charset=utf-8"}
if _accept is not None:
headers['Accept'] = _accept
res = self.app.post('/', message,
headers={"Content-Type": "application/soap+xml; charset=utf-8"},
headers=headers,
expect_errors=True)
print "Status: ", res.status, "Received:", res.body
if _no_result_decode:
return res
el = et.fromstring(res.body)
body = el.find(body_qn)
print body