def test_multi_ns(self): dispatcher = SoapDispatcher( name = "MTClientWS", location = "http://localhost:8008/ws/MTClientWS", action = 'http://localhost:8008/ws/MTClientWS', # SOAPAction namespace = "http://external.mt.moboperator", prefix="external", documentation = 'moboperator MTClientWS', namespaces = { 'external': 'http://external.mt.moboperator', 'model': 'http://model.common.mt.moboperator' }, ns = True, pretty=False, debug=True) dispatcher.register_function('activateSubscriptions', self._multi_ns_func, returns=self._multi_ns_func.returns, args=self._multi_ns_func.args) dispatcher.register_function('updateDeliveryStatus', self._updateDeliveryStatus, returns=self._updateDeliveryStatus.returns, args=self._updateDeliveryStatus.args) self.assertEqual(dispatcher.dispatch(REQ), MULTI_NS_RESP) self.assertEqual(dispatcher.dispatch(REQ1), MULTI_NS_RESP1)
def test_multi_ns(self): dispatcher = SoapDispatcher( name="MTClientWS", location="http://localhost:8008/ws/MTClientWS", action='http://localhost:8008/ws/MTClientWS', # SOAPAction namespace="http://external.mt.moboperator", prefix="external", documentation='moboperator MTClientWS', namespaces={ 'external': 'http://external.mt.moboperator', 'model': 'http://model.common.mt.moboperator' }, ns=True, pretty=False, debug=True) dispatcher.register_function('activateSubscriptions', self._multi_ns_func, returns=self._multi_ns_func.returns, args=self._multi_ns_func.args) dispatcher.register_function( 'updateDeliveryStatus', self._updateDeliveryStatus, returns=self._updateDeliveryStatus.returns, args=self._updateDeliveryStatus.args) self.assertEqual(dispatcher.dispatch(REQ), MULTI_NS_RESP) self.assertEqual(dispatcher.dispatch(REQ1), MULTI_NS_RESP1)
class TestSoapDispatcher(unittest.TestCase): def setUp(self): self.disp = SoapDispatcher( name="PySimpleSoapSample", location="http://localhost:8008/", action='http://localhost:8008/', # SOAPAction namespace="http://example.com/pysimplesoapsamle/", prefix="ns0", documentation='Example soap service using PySimpleSoap', debug=True, ns=True) self.disp.register_function('dummy', dummy, returns={'out0': str}, args={'in0': str} ) self.disp.register_function('dummy_response_element', dummy, returns={'out0': str}, args={'in0': str}, response_element_name='diffRespElemName' ) def test_zero(self): response = """<?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soap:Body><dummyResponse xmlns="http://example.com/pysimplesoapsamle/"><out0>Hello world</out0></dummyResponse></soap:Body></soap:Envelope>""" request = """\ <?xml version="1.0" encoding="UTF-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <dummy xmlns="http://example.com/sample.wsdl"> <in0 xsi:type="xsd:string">Hello world</in0> </dummy> </soap:Body> </soap:Envelope>""" self.assertEqual(self.disp.dispatch(request), response) def test_response_element_name(self): response = """<?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soap:Body><diffRespElemName xmlns="http://example.com/pysimplesoapsamle/"><out0>Hello world</out0></diffRespElemName></soap:Body></soap:Envelope>""" request = """\ <?xml version="1.0" encoding="UTF-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <dummy_response_element xmlns="http://example.com/sample.wsdl"> <in0 xsi:type="xsd:string">Hello world</in0> </dummy_response_element> </soap:Body> </soap:Envelope>""" self.assertEqual(self.disp.dispatch(request), response)
class ServerSoapFaultTest(unittest.TestCase): def setUp(self): self.dispatcher = SoapDispatcher('Test', action='http://localhost:8008/soap', location='http://localhost:8008/soap') def divider(a, b): if b == 0: raise SoapFault(faultcode='DivisionByZero', faultstring='Division by zero not allowed', detail='test') return float(a) / b self.dispatcher.register_function('Divider', divider, returns={'DivideResult': float}, args={ 'a': int, 'b': int }) def test_exception(self): xml = """<?xml version="1.0" encoding="UTF-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <Divider xmlns="http://example.com/sample.wsdl"> <a>100</a><b>2</b> </Divider> </soap:Body> </soap:Envelope>""" response = SimpleXMLElement(self.dispatcher.dispatch(xml)) self.assertEqual(str(response.DivideResult), '50.0') xml = """<?xml version="1.0" encoding="UTF-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <Divider xmlns="http://example.com/sample.wsdl"> <a>100</a><b>0</b> </Divider> </soap:Body> </soap:Envelope>""" response = SimpleXMLElement(self.dispatcher.dispatch(xml)) body = getattr(getattr(response, 'soap:Body'), 'soap:Fault') self.assertIsNotNone(body) self.assertEqual(str(body.faultcode), 'Server.DivisionByZero') self.assertEqual(str(body.faultstring), 'Division by zero not allowed') self.assertEqual(str(body.detail), 'test')
def soap(self): from pysimplesoap.server import SoapDispatcher import uliweb.contrib.soap as soap from uliweb.utils.common import import_attr from uliweb import application as app, response, url_for from functools import partial global __soap_dispatcher__ if not __soap_dispatcher__: location = "%s://%s%s" % ( request.environ['wsgi.url_scheme'], request.environ['HTTP_HOST'], request.path) namespace = functions.get_var(self.config).get('namespace') or location documentation = functions.get_var(self.config).get('documentation') dispatcher = SoapDispatcher( name = functions.get_var(self.config).get('name'), location = location, action = '', # SOAPAction namespace = namespace, prefix=functions.get_var(self.config).get('prefix'), documentation = documentation, exception_handler = partial(exception_handler, response=response), ns = True) for name, (func, returns, args, doc) in soap.__soap_functions__.get(self.config, {}).items(): if isinstance(func, (str, unicode)): func = import_attr(func) dispatcher.register_function(name, func, returns, args, doc) else: dispatcher = __soap_dispatcher__ if 'wsdl' in request.GET: # Return Web Service Description response.headers['Content-Type'] = 'text/xml' response.write(dispatcher.wsdl()) return response elif request.method == 'POST': def _call(func, args): rule = SimpleRule() rule.endpoint = func mod, handler_cls, handler = app.prepare_request(request, rule) result = app.call_view(mod, handler_cls, handler, request, response, _wrap_result, kwargs=args) r = _fix_soap_datatype(result) return r # Process normal Soap Operation response.headers['Content-Type'] = 'text/xml' log.debug("---request message---") log.debug(request.data) result = dispatcher.dispatch(request.data, call_function=_call) log.debug("---response message---") log.debug(result) response.write(result) return response
def test_single_ns(self): dispatcher = SoapDispatcher( name = "MTClientWS", location = "http://localhost:8008/ws/MTClientWS", action = 'http://localhost:8008/ws/MTClientWS', # SOAPAction namespace = "http://external.mt.moboperator", prefix="external", documentation = 'moboperator MTClientWS', ns = True, pretty=False, debug=True) dispatcher.register_function('activateSubscriptions', self._single_ns_func, returns=self._single_ns_func.returns, args=self._single_ns_func.args) # I don't fully know if that is a valid response for a given request, # but I tested it, to be sure that a multi namespace function # doesn't brake anything. self.assertEqual(dispatcher.dispatch(REQ), SINGLE_NS_RESP)
def test_single_ns(self): dispatcher = SoapDispatcher( name="MTClientWS", location="http://localhost:8008/ws/MTClientWS", action='http://localhost:8008/ws/MTClientWS', # SOAPAction namespace="http://external.mt.moboperator", prefix="external", documentation='moboperator MTClientWS', ns=True, pretty=False, debug=True) dispatcher.register_function('activateSubscriptions', self._single_ns_func, returns=self._single_ns_func.returns, args=self._single_ns_func.args) # I don't fully know if that is a valid response for a given request, # but I tested it, to be sure that a multi namespace function # doesn't brake anything. self.assertEqual(dispatcher.dispatch(REQ), SINGLE_NS_RESP)
class TestSoapDispatcher(unittest.TestCase): def eq(self, value, expectation, msg=None): if msg is not None: msg += ' %s' % value self.assertEqual(value, expectation, msg) else: self.assertEqual(value, expectation, "%s\n---\n%s" % (value, expectation)) def setUp(self): self.dispatcher = SoapDispatcher( name="PySimpleSoapSample", location="http://localhost:8008/", action='http://localhost:8008/', # SOAPAction namespace="http://example.com/pysimplesoapsamle/", prefix="ns0", documentation='Example soap service using PySimpleSoap', debug=True, ns=True) self.dispatcher.register_function('Adder', adder, returns={'AddResult': {'ab': int, 'dd': str}}, args={'p': {'a': int, 'b': int}, 'dt': Date, 'c': [{'d': Decimal}]}) self.dispatcher.register_function('Dummy', dummy, returns={'out0': str}, args={'in0': str}) self.dispatcher.register_function('Echo', echo) def test_classic_dialect(self): # adder local test (classic soap dialect) resp = """<?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soap:Body><AdderResponse xmlns="http://example.com/pysimplesoapsamle/"><dd>5000000.3</dd><ab>3</ab><dt>2011-07-24</dt></AdderResponse></soap:Body></soap:Envelope>""" xml = """<?xml version="1.0" encoding="UTF-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <Adder xmlns="http://example.com/sample.wsdl"> <p><a>1</a><b>2</b></p><c><d>5000000.1</d><d>.2</d></c><dt>2010-07-24</dt> </Adder> </soap:Body> </soap:Envelope>""" self.eq(self.dispatcher.dispatch(xml), resp) def test_modern_dialect(self): # adder local test (modern soap dialect, SoapUI) resp = """<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:pys="http://example.com/pysimplesoapsamle/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><pys:AdderResponse><dd>15.021</dd><ab>12</ab><dt>1970-07-20</dt></pys:AdderResponse></soapenv:Body></soapenv:Envelope>""" xml = """ <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pys="http://example.com/pysimplesoapsamle/"> <soapenv:Header/> <soapenv:Body> <pys:Adder> <pys:p><pys:a>9</pys:a><pys:b>3</pys:b></pys:p> <pys:dt>1969-07-20<!--1969-07-20T21:28:00--></pys:dt> <pys:c><pys:d>10.001</pys:d><pys:d>5.02</pys:d></pys:c> </pys:Adder> </soapenv:Body> </soapenv:Envelope> """ self.eq(self.dispatcher.dispatch(xml), resp) def test_echo(self): # echo local test (generic soap service) resp = """<?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soap:Body><EchoResponse xmlns="http://example.com/pysimplesoapsamle/"><value xsi:type="xsd:string">Hello world</value></EchoResponse></soap:Body></soap:Envelope>""" xml = """<?xml version="1.0" encoding="UTF-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <Echo xmlns="http://example.com/sample.wsdl"> <value xsi:type="xsd:string">Hello world</value> </Echo> </soap:Body> </soap:Envelope>""" self.eq(self.dispatcher.dispatch(xml), resp)
class TestSoapDispatcher(unittest.TestCase): def eq(self, value, expectation, msg=None): if msg is not None: msg += ' %s' % value self.assertEqual(value, expectation, msg) else: self.assertEqual(value, expectation, "%s\n---\n%s" % (value, expectation)) def setUp(self): self.dispatcher = SoapDispatcher( name="PySimpleSoapSample", location="http://localhost:8008/", action='http://localhost:8008/', # SOAPAction namespace="http://example.com/pysimplesoapsamle/", prefix="ns0", documentation='Example soap service using PySimpleSoap', debug=True, ns=True) self.dispatcher.register_function( 'Adder', adder, returns={'AddResult': { 'ab': int, 'dd': str }}, args={ 'p': { 'a': int, 'b': int }, 'dt': Date, 'c': [{ 'd': Decimal }] }) self.dispatcher.register_function('Dummy', dummy, returns={'out0': str}, args={'in0': str}) self.dispatcher.register_function('Echo', echo) def test_classic_dialect(self): # adder local test (clasic soap dialect) resp = """<?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soap:Body><AdderResponse xmlns="http://example.com/pysimplesoapsamle/"><dd>5000000.3</dd><ab>3</ab><dt>2011-07-24</dt></AdderResponse></soap:Body></soap:Envelope>""" xml = """<?xml version="1.0" encoding="UTF-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <Adder xmlns="http://example.com/sample.wsdl"> <p><a>1</a><b>2</b></p><c><d>5000000.1</d><d>.2</d></c><dt>2010-07-24</dt> </Adder> </soap:Body> </soap:Envelope>""" self.eq(self.dispatcher.dispatch(xml), resp) def test_modern_dialect(self): # adder local test (modern soap dialect, SoapUI) resp = """<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:pys="http://example.com/pysimplesoapsamle/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><pys:AdderResponse><dd>15.021</dd><ab>12</ab><dt>1970-07-20</dt></pys:AdderResponse></soapenv:Body></soapenv:Envelope>""" xml = """ <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pys="http://example.com/pysimplesoapsamle/"> <soapenv:Header/> <soapenv:Body> <pys:Adder> <pys:p><pys:a>9</pys:a><pys:b>3</pys:b></pys:p> <pys:dt>1969-07-20<!--1969-07-20T21:28:00--></pys:dt> <pys:c><pys:d>10.001</pys:d><pys:d>5.02</pys:d></pys:c> </pys:Adder> </soapenv:Body> </soapenv:Envelope> """ self.eq(self.dispatcher.dispatch(xml), resp) def test_echo(self): # echo local test (generic soap service) resp = """<?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soap:Body><EchoResponse xmlns="http://example.com/pysimplesoapsamle/"><value xsi:type="xsd:string">Hello world</value></EchoResponse></soap:Body></soap:Envelope>""" xml = """<?xml version="1.0" encoding="UTF-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <Echo xmlns="http://example.com/sample.wsdl"> <value xsi:type="xsd:string">Hello world</value> </Echo> </soap:Body> </soap:Envelope>""" self.eq(self.dispatcher.dispatch(xml), resp)
def soap(self): from pysimplesoap.server import SoapDispatcher import uliweb.contrib.soap as soap from uliweb.utils.common import import_attr from uliweb import application as app, response, url_for from functools import partial global __soap_dispatcher__ if not __soap_dispatcher__: location = "%s://%s%s" % (request.environ['wsgi.url_scheme'], request.environ['HTTP_HOST'], request.path) namespace = functions.get_var( self.config).get('namespace') or location documentation = functions.get_var(self.config).get('documentation') dispatcher = SoapDispatcher( name=functions.get_var(self.config).get('name'), location=location, action='', # SOAPAction namespace=namespace, prefix=functions.get_var(self.config).get('prefix'), documentation=documentation, exception_handler=partial(exception_handler, response=response), ns=True) for name, (func, returns, args, doc) in soap.__soap_functions__.get(self.config, {}).items(): if isinstance(func, (str, unicode)): func = import_attr(func) dispatcher.register_function(name, func, returns, args, doc) else: dispatcher = __soap_dispatcher__ if 'wsdl' in request.GET: # Return Web Service Description response.headers['Content-Type'] = 'text/xml' response.write(dispatcher.wsdl()) return response elif request.method == 'POST': def _call(func, args): rule = SimpleRule() rule.endpoint = func mod, handler_cls, handler = app.prepare_request(request, rule) result = app.call_view(mod, handler_cls, handler, request, response, _wrap_result, kwargs=args) r = _fix_soap_datatype(result) return r # Process normal Soap Operation response.headers['Content-Type'] = 'text/xml' log.debug("---request message---") log.debug(request.data) result = dispatcher.dispatch(request.data, call_function=_call) log.debug("---response message---") log.debug(result) response.write(result) return response