def test_default_transaction(self): import transaction root = WSRoot(transaction=True) assert root._transaction is transaction txn = root.begin() txn.abort()
def test_no_available_protocol(self): r = WSRoot() app = webtest.TestApp(r.wsgiapp()) res = app.get('/', expect_errors=True) assert res.status_int == 500 print(res.body) assert res.body.find( b("None of the following protocols can handle this request")) != -1
def test_register_protocol(self): wsme.protocol.register_protocol(DummyProtocol) assert wsme.protocol.registered_protocols["dummy"] == DummyProtocol r = WSRoot() assert len(r.protocols) == 0 r.addprotocol("dummy") assert len(r.protocols) == 1 assert r.protocols[0].__class__ == DummyProtocol r = WSRoot(["dummy"]) assert len(r.protocols) == 1 assert r.protocols[0].__class__ == DummyProtocol
class Root(RootController): class UselessSubClass: # This class is here only to make sure wsmeext.tg1.scan_api # does its job properly pass sub = Subcontroller() ws = WSRoot(webpath='/ws') ws.addprotocol('soap', tns=test_soap.tns, typenamespace=test_soap.typenamespace, baseURL='/ws/' ) ws = wsmeext.tg15.adapt(ws) @wsmeext.tg15.wsexpose(int) @wsmeext.tg15.wsvalidate(int, int) def multiply(self, a, b): return a * b @wsmeext.tg15.wsexpose(int) @wsmeext.tg15.wsvalidate(int, int) def divide(self, a, b): if b == 0: raise cherrypy.HTTPError(400, 'Cannot divide by zero!') return a / b
def test_protocol_selection_error(self): import wsme.protocol class P(wsme.protocol.Protocol): def accept(self, r): raise Exception('test') root = WSRoot() root.addprotocol(P()) from webob import Request req = Request.blank('/test?check=a&check=b&name=Bob') res = root._handle_request(req) assert res.status_int == 500 assert res.content_type == 'text/plain' assert res.text == u('Error while selecting protocol: test'), req.text
def test_return_content_type_guess(self): class DummierProto(DummyProtocol): content_types = ['text/xml', 'text/plain'] r = WSRoot([DummierProto()]) app = webtest.TestApp(r.wsgiapp()) 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
def test_protocol_selection_get_method(self): class P(wsme.protocol.Protocol): name = "test" def accept(self, r): return True root = WSRoot() root.addprotocol(wsme.rest.protocol.RestProtocol()) root.addprotocol(P()) req = Request.blank('/test?check=a&check=b&name=Bob') req.method = 'GET' req.headers['Accept'] = 'test/fake' p = root._select_protocol(req) assert p.name == "test"
def test_register_protocol(self): wsme.protocol.register_protocol(DummyProtocol) assert wsme.protocol.registered_protocols['dummy'] == DummyProtocol r = WSRoot() assert len(r.protocols) == 0 r.addprotocol('dummy') assert len(r.protocols) == 1 assert r.protocols[0].__class__ == DummyProtocol r = WSRoot(['dummy']) assert len(r.protocols) == 1 assert r.protocols[0].__class__ == DummyProtocol
def test_protocol_selection_content_type_mismatch(self): """Verify that we get a 415 error on wrong Content-Type header.""" class P(wsme.protocol.Protocol): name = "test" def accept(self, r): return False root = WSRoot() root.addprotocol(wsme.rest.protocol.RestProtocol()) root.addprotocol(P()) req = Request.blank('/test?check=a&check=b&name=Bob') req.method = 'POST' req.headers['Content-Type'] = "test/unsupported" res = root._handle_request(req) assert res.status_int == 415 assert res.content_type == 'text/plain' assert res.text.startswith( 'Unacceptable Content-Type: test/unsupported not in'), req.text
def test_protocol_selection_accept_mismatch(self): """Verify that we get a 406 error on wrong Accept header.""" class P(wsme.protocol.Protocol): name = "test" def accept(self, r): return False root = WSRoot() root.addprotocol(wsme.rest.protocol.RestProtocol()) root.addprotocol(P()) req = Request.blank('/test?check=a&check=b&name=Bob') req.method = 'GET' res = root._handle_request(req) assert res.status_int == 406 assert res.content_type == 'text/plain' assert res.text.startswith( 'None of the following protocols can handle this request' ), req.text
def test_protocol_selection_content_type_mismatch(self): """Verify that we get a 415 error on wrong Content-Type header.""" class P(wsme.protocol.Protocol): name = "test" def accept(self, r): return False root = WSRoot() root.addprotocol(wsme.rest.protocol.RestProtocol()) root.addprotocol(P()) req = Request.blank('/test?check=a&check=b&name=Bob') req.method = 'POST' req.headers['Content-Type'] = "test/unsupported" res = root._handle_request(req) assert res.status_int == 415 assert res.content_type == 'text/plain' assert res.text.startswith( 'Unacceptable Content-Type: test/unsupported not in' ), req.text