def test_can_include_imported_schemas_during_validation(self): # In case the SOAPDispatcher would not use imported schemas for # validation it would fail because the 'code' tag is only defined in # the imported schema handler, handler_state = echo_handler() service = echo_service(handler) class CodeType(xsd.String): pattern = r'[0-9]{5}' class Container(xsd.ComplexType): value = xsd.Element(CodeType) code_schema = xsd.Schema( 'http://soap.example/included', location='http://soap.example/included', elementFormDefault=xsd.ElementFormDefault.UNQUALIFIED, simpleTypes=[CodeType], complexTypes=[Container], elements={'foo': xsd.Element(Container)}, ) service.methods[0].input = 'foo' service.schemas[0].imports = [code_schema] # The setup is a bit simplistic because the <code> tag is not parsed # into a soapfish model element for the handler but this was enough # to trigger the bug dispatcher = SOAPDispatcher(service) wsgi_environ = dict(SOAPACTION='echo', REQUEST_METHOD='POST') soap_message = '<ns0:foo xmlns:ns0="http://soap.example/included"><value>12345</value></ns0:foo>' request = SOAPRequest(wsgi_environ, self._wrap_with_soap_envelope(soap_message)) response = dispatcher.dispatch(request) self.assert_is_successful_response(response, handler_state) assert_equals('12345', handler_state.input_.value)
def test_can_include_imported_schemas_during_validation(self): # In case the SOAPDispatcher would not use imported schemas for # validation it would fail because the 'code' tag is only defined in # the imported schema handler, handler_state = echo_handler() service = echo_service(handler) class CodeType(xsd.String): pattern = r'[0-9]{5}' class Container(xsd.ComplexType): value = xsd.Element(CodeType) code_schema = xsd.Schema('http://soap.example/included', location='http://soap.example/included', elementFormDefault=xsd.ElementFormDefault.UNQUALIFIED, simpleTypes=[CodeType], complexTypes=[Container], elements={'foo': xsd.Element(Container)}, ) service.methods[0].input = 'foo' service.schema.imports = (code_schema, ) # The setup is a bit simplistic because the <code> tag is not parsed # into a soapfish model element for the handler but this was enough # to trigger the bug dispatcher = SOAPDispatcher(service) wsgi_environ = dict(SOAPACTION='echo', REQUEST_METHOD='POST') soap_message = '<ns0:foo xmlns:ns0="http://soap.example/included"><value>12345</value></ns0:foo>' request = SOAPRequest(wsgi_environ, self._wrap_with_soap_envelope(soap_message)) response = dispatcher.dispatch(request) self.assert_is_successful_response(response, handler_state) assert_equals('12345', handler_state.input_.value)
def test_can_dispatch_good_soap_message(self): handler, handler_state = echo_handler() dispatcher = SOAPDispatcher(echo_service(handler)) soap_message = ( '<ns1:echoRequest xmlns:ns1="http://soap.example/echo/types">' '<value>foobar</value>' '</ns1:echoRequest>') request_message = self._wrap_with_soap_envelope(soap_message) request = SOAPRequest(dict(SOAPACTION='echo', REQUEST_METHOD='POST'), request_message) response = dispatcher.dispatch(request) self.assert_is_successful_response(response, handler_state) assert_equals('foobar', handler_state.input_.value) response_document = etree.fromstring(response.http_content) response_xml = etree.tostring(response_document, pretty_print=True) expected_xml = ( b'<ns0:Envelope xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/">\n' b' <ns0:Body>\n' b' <ns0:echoResponse xmlns:ns0="http://soap.example/echo/types">\n' b' <value>foobar</value>\n' b' </ns0:echoResponse>\n' b' </ns0:Body>\n' b'</ns0:Envelope>\n') assert_equals(expected_xml, response_xml)
def test_can_dispatch_good_soap_message(self): handler, handler_state = echo_handler() dispatcher = SOAPDispatcher(echo_service(handler)) soap_message = ('<ns1:echoRequest xmlns:ns1="http://soap.example/echo/types">' '<value>foobar</value>' '</ns1:echoRequest>') request_message = self._wrap_with_soap_envelope(soap_message) request = SOAPRequest(dict(SOAPACTION='echo', REQUEST_METHOD='POST'), request_message) response = dispatcher.dispatch(request) self.assert_is_successful_response(response, handler_state) assert_equals('foobar', handler_state.input_.value) response_document = etree.fromstring(response.http_content) response_xml = etree.tostring(response_document, pretty_print=True) expected_xml = ( b'<ns0:Envelope xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/">\n' b' <ns0:Body>\n' b' <ns0:echoResponse xmlns:ns0="http://soap.example/echo/types">\n' b' <value>foobar</value>\n' b' </ns0:echoResponse>\n' b' </ns0:Body>\n' b'</ns0:Envelope>\n' ) assert_equals(expected_xml, response_xml)
def test_can_handle_empty_output_header(self): handler, handler_state = echo_handler() dispatcher = SOAPDispatcher(echo_service(handler, output_header=EchoOutputHeader)) soap_message = ('<tns:echoRequest xmlns:tns="http://soap.example/echo/types">' '<value>foobar</value>' '</tns:echoRequest>') request_message = self._wrap_with_soap_envelope(soap_message) request = SOAPRequest(dict(SOAPACTION='echo', REQUEST_METHOD='POST'), request_message) response = dispatcher.dispatch(request) self.assert_is_successful_response(response, handler_state)
def test_can_dispatch_requests_based_on_soap_body(self): handler, handler_state = echo_handler() dispatcher = SOAPDispatcher(echo_service(handler)) soap_message = ('<ns1:echoRequest xmlns:ns1="http://soap.example/echo/types">' '<value>foobar</value>' '</ns1:echoRequest>') request_message = self._wrap_with_soap_envelope(soap_message) request = SOAPRequest(dict(SOAPACTION='""', REQUEST_METHOD='POST'), request_message) response = dispatcher.dispatch(request) self.assert_is_successful_response(response, handler_state)
def test_evaluate_service_location(self): handler, _ = echo_handler() service = echo_service(handler) service.location = '${scheme}://${host}/ws' dispatcher = SOAPDispatcher(service) request = SOAPRequest({'REQUEST_METHOD': 'GET', 'QUERY_STRING': 'wsdl', 'HTTP_HOST': 'soap.example'}, '') response = dispatcher.dispatch(request) self.assert_is_successful_response(response) self.assertNotIn('${scheme}', response.http_content.decode()) self.assertNotIn('${host}', response.http_content.decode())
def test_evaluate_service_location(self): handler, _ = echo_handler() service = echo_service(handler) service.location = '{scheme}://{host}/ws' dispatcher = SOAPDispatcher(service) request = SOAPRequest(dict(REQUEST_METHOD='GET', QUERY_STRING='wsdl', HTTP_HOST='soap.example'), '') response = dispatcher.dispatch(request) self.assert_is_successful_response(response) assert_not_contains('{scheme}', response.http_content.decode()) assert_not_contains('{http}', response.http_content.decode())
def test_can_validate_soap_header(self): handler, handler_state = echo_handler() dispatcher = SOAPDispatcher(echo_service(handler, input_header=EchoInputHeader)) soap_header = ('<tns:invalid>42</tns:invalid>') soap_message = ('<tns:echoRequest>' '<value>foobar</value>' '</tns:echoRequest>') request_message = self._wrap_with_soap_envelope(soap_message, header=soap_header) request = SOAPRequest(dict(SOAPACTION='echo', REQUEST_METHOD='POST'), request_message) response = dispatcher.dispatch(request) self.assert_is_soap_fault(response, partial_fault_string="DocumentInvalid")
def test_can_validate_soap_message(self): handler, handler_state = echo_handler() dispatcher = SOAPDispatcher(echo_service(handler)) soap_message = ('<ns1:echoRequest xmlns:ns1="http://soap.example/echo/types">' '<invalid>foobar</invalid>' '</ns1:echoRequest>') request_message = self._wrap_with_soap_envelope(soap_message) request = SOAPRequest(dict(SOAPACTION='echo', REQUEST_METHOD='POST'), request_message) response = dispatcher.dispatch(request) assert_false(handler_state.was_called) self.assert_is_soap_fault(response, partial_fault_string=u"Element 'invalid': This element is not expected. Expected is ( value ).")
def test_can_propagate_custom_input_header(self): handler, handler_state = echo_handler() dispatcher = SOAPDispatcher(echo_service(handler, input_header=EchoInputHeader)) soap_header = ('<tns:InputVersion>42</tns:InputVersion>') soap_message = ('<tns:echoRequest>' '<value>foobar</value>' '</tns:echoRequest>') request_message = self._wrap_with_soap_envelope(soap_message, header=soap_header) request = SOAPRequest(dict(SOAPACTION='echo', REQUEST_METHOD='POST'), request_message) response = dispatcher.dispatch(request) self.assert_is_successful_response(response, handler_state) assert_not_none(handler_state.input_header) assert_equals('42', handler_state.input_header.InputVersion)
def test_can_propagate_custom_output_header(self): handler, handler_state = echo_handler() def _handler(request, _input): resp = handler(request, _input) resp.soap_header = EchoOutputHeader(OutputVersion='42') return resp dispatcher = SOAPDispatcher(echo_service(_handler, output_header=EchoOutputHeader)) soap_header = ('<tns:InputVersion>42</tns:InputVersion>') soap_message = ('<tns:echoRequest xmlns:tns="http://soap.example/echo/types">' '<value>foobar</value>' '</tns:echoRequest>') request_message = self._wrap_with_soap_envelope(soap_message, header=soap_header) request = SOAPRequest(dict(SOAPACTION='echo', REQUEST_METHOD='POST'), request_message) response = dispatcher.dispatch(request) self.assert_is_successful_response(response, handler_state) assert_contains(b'<ns0:OutputVersion>42</ns0:OutputVersion>', response.http_content)
def test_service_bind_function(self): handler, handler_state = echo_handler() service = echo_service(handler) @service.route('echoOperation') def echo_func(request, input_): handler_state.new_func_was_called = True return handler(request, input_) dispatcher = SOAPDispatcher(service) soap_message = ('<ns1:echoRequest xmlns:ns1="http://soap.example/echo/types">' '<value>foobar</value>' '</ns1:echoRequest>') request_message = self._wrap_with_soap_envelope(soap_message) request = SOAPRequest(dict(SOAPACTION='echo', REQUEST_METHOD='POST'), request_message) response = dispatcher.dispatch(request) self.assertTrue(handler_state.new_func_was_called) self.assert_is_successful_response(response, handler_state)
def test_service_bind_function(self): handler, handler_state = echo_handler() service = echo_service(handler) @service.route('echoOperation') def echo_func(request, input_): handler_state.new_func_was_called = True return handler(request, input_) dispatcher = SOAPDispatcher(service) soap_message = ('<ns1:echoRequest xmlns:ns1="http://soap.example/echo/types">' '<value>foobar</value>' '</ns1:echoRequest>') request_message = self._wrap_with_soap_envelope(soap_message) request = SOAPRequest(dict(SOAPACTION='echo', REQUEST_METHOD='POST'), request_message) response = dispatcher.dispatch(request) assert_true(handler_state.new_func_was_called) self.assert_is_successful_response(response, handler_state)