diff --git a/wsme/controller.py b/wsme/controller.py index d3707e4..303dd6e 100644 --- a/wsme/controller.py +++ b/wsme/controller.py @@ -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) diff --git a/wsme/tests/protocol.py b/wsme/tests/protocol.py index db76eb9..8a59185 100644 --- a/wsme/tests/protocol.py +++ b/wsme/tests/protocol.py @@ -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 diff --git a/wsme/tests/test_controller.py b/wsme/tests/test_controller.py index 8e01bcb..1a42eac 100644 --- a/wsme/tests/test_controller.py +++ b/wsme/tests/test_controller.py @@ -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 diff --git a/wsme/tests/test_restjson.py b/wsme/tests/test_restjson.py index 3aa59d5..20b7fec 100644 --- a/wsme/tests/test_restjson.py +++ b/wsme/tests/test_restjson.py @@ -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'] diff --git a/wsme/tests/test_restxml.py b/wsme/tests/test_restxml.py index 0b974c9..67b91af 100644 --- a/wsme/tests/test_restxml.py +++ b/wsme/tests/test_restxml.py @@ -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( diff --git a/wsme/tests/test_soap.py b/wsme/tests/test_soap.py index b8bffa5..8d86c57 100644 --- a/wsme/tests/test_soap.py +++ b/wsme/tests/test_soap.py @@ -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