def test_restriction_data_types(): client_unnamed = tests.client_from_wsdl( tests.wsdl_output( """\ <xsd:element name="Elemento"> <xsd:simpleType> <xsd:restriction base="xsd:int"> <xsd:enumeration value="1" /> <xsd:enumeration value="3" /> <xsd:enumeration value="5" /> </xsd:restriction> </xsd:simpleType> </xsd:element>""", "Elemento")) client_named = tests.client_from_wsdl( tests.wsdl_output( """\ <xsd:simpleType name="MyType"> <xsd:restriction base="xsd:int"> <xsd:enumeration value="1" /> <xsd:enumeration value="3" /> <xsd:enumeration value="5" /> </xsd:restriction> </xsd:simpleType> <xsd:element name="Elemento" type="ns:MyType" />""", "Elemento")) client_twice_restricted = tests.client_from_wsdl( tests.wsdl_output( """\ <xsd:simpleType name="MyTypeGeneric"> <xsd:restriction base="xsd:int"> <xsd:enumeration value="1" /> <xsd:enumeration value="2" /> <xsd:enumeration value="3" /> <xsd:enumeration value="4" /> <xsd:enumeration value="5" /> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="MyType"> <xsd:restriction base="ns:MyTypeGeneric"> <xsd:enumeration value="1" /> <xsd:enumeration value="3" /> <xsd:enumeration value="5" /> </xsd:restriction> </xsd:simpleType> <xsd:element name="Elemento" type="ns:MyType" />""", "Elemento")) for client in (client_unnamed, client_named, client_twice_restricted): response = client.service.f(__inject=dict(reply=suds.byte_str("""\ <?xml version="1.0"?> <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <Elemento xmlns="my-namespace">5</Elemento> </env:Body> </env:Envelope>"""))) assert response.__class__ is int assert response == 5
def test_restriction_data_types(): client_unnamed = tests.client_from_wsdl(tests.wsdl_output("""\ <xsd:element name="Elemento"> <xsd:simpleType> <xsd:restriction base="xsd:int"> <xsd:enumeration value="1" /> <xsd:enumeration value="3" /> <xsd:enumeration value="5" /> </xsd:restriction> </xsd:simpleType> </xsd:element>""", "Elemento")) client_named = tests.client_from_wsdl(tests.wsdl_output("""\ <xsd:simpleType name="MyType"> <xsd:restriction base="xsd:int"> <xsd:enumeration value="1" /> <xsd:enumeration value="3" /> <xsd:enumeration value="5" /> </xsd:restriction> </xsd:simpleType> <xsd:element name="Elemento" type="ns:MyType" />""", "Elemento")) client_twice_restricted = tests.client_from_wsdl(tests.wsdl_output("""\ <xsd:simpleType name="MyTypeGeneric"> <xsd:restriction base="xsd:int"> <xsd:enumeration value="1" /> <xsd:enumeration value="2" /> <xsd:enumeration value="3" /> <xsd:enumeration value="4" /> <xsd:enumeration value="5" /> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="MyType"> <xsd:restriction base="ns:MyTypeGeneric"> <xsd:enumeration value="1" /> <xsd:enumeration value="3" /> <xsd:enumeration value="5" /> </xsd:restriction> </xsd:simpleType> <xsd:element name="Elemento" type="ns:MyType" />""", "Elemento")) for client in (client_unnamed, client_named, client_twice_restricted): response = client.service.f(__inject=dict(reply=suds.byte_str("""\ <?xml version="1.0"?> <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <Elemento xmlns="my-namespace">5</Elemento> </env:Body> </env:Envelope>"""))) assert response.__class__ is int assert response == 5
def test_simple_bare_and_wrapped_output(): # Prepare web service proxies. client_bare = tests.client_from_wsdl( tests.wsdl_output( """\ <xsd:element name="fResponse" type="xsd:string" />""", "fResponse")) client_wrapped = tests.client_from_wsdl( tests.wsdl_output( """\ <xsd:element name="Wrapper"> <xsd:complexType> <xsd:sequence> <xsd:element name="fResponse" type="xsd:string" /> </xsd:sequence> </xsd:complexType> </xsd:element>""", "Wrapper")) # Make sure suds library inteprets our WSDL definitions as wrapped or # bare output interfaces as expected. assert not _isOutputWrapped(client_bare, "f") assert _isOutputWrapped(client_wrapped, "f") # Both bare & wrapped single parameter output web service operation # results get presented the same way even though the wrapped one actually # has an extra wrapper element around its received output data. data = "The meaning of life." get_response = lambda client, x: client.service.f(__inject=dict( reply=suds.byte_str(x))) response_bare = get_response( client_bare, """<?xml version="1.0"?> <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <fResponse xmlns="my-namespace">%s</fResponse> </env:Body> </env:Envelope>""" % data) assert response_bare.__class__ is suds.sax.text.Text assert response_bare == data response_wrapped = get_response( client_wrapped, """<?xml version="1.0"?> <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <Wrapper xmlns="my-namespace"> <fResponse>%s</fResponse> </Wrapper> </env:Body> </env:Envelope>""" % data) assert response_wrapped.__class__ is suds.sax.text.Text assert response_wrapped == data
def test_disabling_automated_simple_interface_unwrapping(): client = tests.client_from_wsdl(tests.wsdl_output("""\ <xsd:element name="Wrapper"> <xsd:complexType> <xsd:sequence> <xsd:element name="Elemento" type="xsd:string" /> </xsd:sequence> </xsd:complexType> </xsd:element>""", "Wrapper"), unwrap=False) assert not _isOutputWrapped(client, "f") response = client.service.f(__inject=dict(reply=suds.byte_str("""\ <?xml version="1.0"?> <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <Wrapper xmlns="my-namespace"> <Elemento>La-di-da-da-da</Elemento> </Wrapper> </env:Body> </env:Envelope>"""))) assert response.__class__.__name__ == "Wrapper" assert len(response.__class__.__bases__) == 1 assert response.__class__.__bases__[0] is suds.sudsobject.Object assert response.Elemento.__class__ is suds.sax.text.Text assert response.Elemento == "La-di-da-da-da"
def test_missing_wrapper_response(): """ Suds library's automatic structure unwrapping should not be applied to interpreting received SOAP Response XML. """ client = tests.client_from_wsdl( tests.wsdl_output( """\ <xsd:element name="Wrapper"> <xsd:complexType> <xsd:sequence> <xsd:element name="fResponse" type="xsd:string" /> </xsd:sequence> </xsd:complexType> </xsd:element>""", "Wrapper")) assert _isOutputWrapped(client, "f") response_with_missing_wrapper = client.service.f(__inject=dict( reply=suds.byte_str("""<?xml version="1.0"?> <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <fResponse xmlns="my-namespace">Anything</fResponse> </env:Body> </env:Envelope>"""))) assert response_with_missing_wrapper is None
def test_simple_bare_and_wrapped_output(): # Prepare web service proxies. client_bare = tests.client_from_wsdl(tests.wsdl_output("""\ <xsd:element name="fResponse" type="xsd:string" />""", "fResponse")) client_wrapped = tests.client_from_wsdl(tests.wsdl_output("""\ <xsd:element name="Wrapper"> <xsd:complexType> <xsd:sequence> <xsd:element name="fResponse" type="xsd:string" /> </xsd:sequence> </xsd:complexType> </xsd:element>""", "Wrapper")) # Make sure suds library inteprets our WSDL definitions as wrapped or # bare output interfaces as expected. assert not _isOutputWrapped(client_bare, "f") assert _isOutputWrapped(client_wrapped, "f") # Both bare & wrapped single parameter output web service operation # results get presented the same way even though the wrapped one actually # has an extra wrapper element around its received output data. data = "The meaning of life." get_response = lambda client, x : client.service.f(__inject=dict( reply=suds.byte_str(x))) response_bare = get_response(client_bare, """<?xml version="1.0"?> <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <fResponse xmlns="my-namespace">%s</fResponse> </env:Body> </env:Envelope>""" % data) assert response_bare.__class__ is suds.sax.text.Text assert response_bare == data response_wrapped = get_response(client_wrapped, """<?xml version="1.0"?> <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <Wrapper xmlns="my-namespace"> <fResponse>%s</fResponse> </Wrapper> </env:Body> </env:Envelope>""" % data) assert response_wrapped.__class__ is suds.sax.text.Text assert response_wrapped == data
def test_wrapped_sequence_output(): client = tests.client_from_wsdl( tests.wsdl_output( """\ <xsd:element name="Wrapper"> <xsd:complexType> <xsd:sequence> <xsd:element name="result1" type="xsd:string" /> <xsd:element name="result2" type="xsd:string" /> <xsd:element name="result3" type="xsd:string" /> </xsd:sequence> </xsd:complexType> </xsd:element>""", "Wrapper")) assert _isOutputWrapped(client, "f") response = client.service.f(__inject=dict(reply=suds.byte_str("""\ <?xml version="1.0"?> <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <Wrapper xmlns="my-namespace"> <result1>Uno</result1> <result2>Due</result2> <result3>Tre</result3> </Wrapper> </env:Body> </env:Envelope>"""))) # Composite replies always get unmarshalled as a dynamically constructed # class named 'reply'. assert len(response.__class__.__bases__) == 1 assert response.__class__.__name__ == "reply" assert response.__class__.__bases__[0] is suds.sudsobject.Object # Check response content. assert len(response) == 3 assert response.result1 == "Uno" assert response.result2 == "Due" assert response.result3 == "Tre" assert response.result1.__class__ is suds.sax.text.Text assert response.result2.__class__ is suds.sax.text.Text assert response.result3.__class__ is suds.sax.text.Text
def test_wrapped_sequence_output(): client = tests.client_from_wsdl(tests.wsdl_output("""\ <xsd:element name="Wrapper"> <xsd:complexType> <xsd:sequence> <xsd:element name="result1" type="xsd:string" /> <xsd:element name="result2" type="xsd:string" /> <xsd:element name="result3" type="xsd:string" /> </xsd:sequence> </xsd:complexType> </xsd:element>""", "Wrapper")) assert _isOutputWrapped(client, "f") response = client.service.f(__inject=dict(reply=suds.byte_str("""\ <?xml version="1.0"?> <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <Wrapper xmlns="my-namespace"> <result1>Uno</result1> <result2>Due</result2> <result3>Tre</result3> </Wrapper> </env:Body> </env:Envelope>"""))) # Composite replies always get unmarshalled as a dynamically constructed # class named 'reply'. assert len(response.__class__.__bases__) == 1 assert response.__class__.__name__ == "reply" assert response.__class__.__bases__[0] is suds.sudsobject.Object # Check response content. assert len(response) == 3 assert response.result1 == "Uno" assert response.result2 == "Due" assert response.result3 == "Tre" assert response.result1.__class__ is suds.sax.text.Text assert response.result2.__class__ is suds.sax.text.Text assert response.result3.__class__ is suds.sax.text.Text
def test_missing_wrapper_response(): """ Suds library's automatic structure unwrapping should not be applied to interpreting received SOAP Response XML. """ client = tests.client_from_wsdl(tests.wsdl_output("""\ <xsd:element name="Wrapper"> <xsd:complexType> <xsd:sequence> <xsd:element name="fResponse" type="xsd:string" /> </xsd:sequence> </xsd:complexType> </xsd:element>""", "Wrapper")) assert _isOutputWrapped(client, "f") response_with_missing_wrapper = client.service.f(__inject=dict( reply=suds.byte_str("""<?xml version="1.0"?> <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <fResponse xmlns="my-namespace">Anything</fResponse> </env:Body> </env:Envelope>"""))) assert response_with_missing_wrapper is None
def test_builtin_data_types(monkeypatch): monkeypatch.delitem(locals(), "e", False) integer_type_mapping = { "byte":int, "int":int, "integer":int, "long":int, "negativeInteger":int, "nonNegativeInteger":int, "nonPositiveInteger":int, "positiveInteger":int, "short":int, "unsignedByte":int, "unsignedInt":int, "unsignedLong":int, "unsignedShort":int} for tag, type in list(integer_type_mapping.items()): client = tests.client_from_wsdl(tests.wsdl_output("""\ <xsd:element name="value" type="xsd:%s" />""" % tag, "value")) response = client.service.f(__inject=dict(reply=suds.byte_str("""\ <?xml version="1.0"?> <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <value xmlns="my-namespace">15</value> </env:Body> </env:Envelope>"""))) assert response.__class__ is type assert response == 15 boolean_mapping = {"0":False, "1":True, "false":False, "true":True} client = tests.client_from_wsdl(tests.wsdl_output("""\ <xsd:element name="value" type="xsd:boolean" />""", "value")) for value, expected_value in list(boolean_mapping.items()): response = client.service.f(__inject=dict(reply=suds.byte_str("""\ <?xml version="1.0"?> <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <value xmlns="my-namespace">%s</value> </env:Body> </env:Envelope>""" % value))) assert response.__class__ is bool assert response == expected_value # Suds implements no extra range checking. client = tests.client_from_wsdl(tests.wsdl_output("""\ <xsd:element name="value" type="xsd:byte" />""", "value")) response = client.service.f(__inject=dict(reply=suds.byte_str("""\ <?xml version="1.0"?> <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <value xmlns="my-namespace">1500</value> </env:Body> </env:Envelope>"""))) assert response.__class__ is int assert response == 1500 # Suds raises raw Python exceptions when it fails to convert received # response element data to its mapped Python integer data type, according # to the used WSDL schema. client = tests.client_from_wsdl(tests.wsdl_output("""\ <xsd:element name="value" type="xsd:int" />""", "value")) e = pytest.raises(ValueError, client.service.f, __inject=dict( reply=suds.byte_str("""\ <?xml version="1.0"?> <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <value xmlns="my-namespace">Fifteen</value> </env:Body> </env:Envelope>"""))).value # ValueError instance received here has different string representations # depending on the Python version used: # Python 2.4: # "invalid literal for int(): Fifteen" # Python 2.7.3, 3.2.3: # "invalid literal for int() with base 10: 'Fifteen'" assert re.match("invalid literal for int\(\)( with base 10)?: ('?)Fifteen" "\\2$", str(e)) # Suds returns invalid boolean values as None. invalid_boolean_values = ("True", "", "False", "2", "Fedora", "Z", "-1") client = tests.client_from_wsdl(tests.wsdl_output("""\ <xsd:element name="value" type="xsd:boolean" />""", "value")) for value in invalid_boolean_values: response = client.service.f(__inject=dict(reply=suds.byte_str("""\ <?xml version="1.0"?> <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <value xmlns="my-namespace">%s</value> </env:Body> </env:Envelope>""" % value))) assert response is None
<faultstring>Dummy error.</faultstring> <detail> <errorcode>ultimate</errorcode> </detail> </env:Fault> </env:Body> </env:Envelope> """) _fault_reply__without_detail = suds.byte_str("""\ <?xml version="1.0"?> <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <env:Fault> <faultcode>env:Client</faultcode> <faultstring>Dummy error.</faultstring> </env:Fault> </env:Body> </env:Envelope> """) _wsdl__simple = tests.wsdl_output("""\ <xsd:element name="fResponse"> <xsd:complexType> <xsd:sequence> <xsd:element name="output_i" type="xsd:integer" /> <xsd:element name="output_s" type="xsd:string" /> </xsd:sequence> </xsd:complexType> </xsd:element>""", "fResponse")
def test_builtin_data_types(): integer_type_mapping = { "byte":int, "int":int, "integer":int, "long":long, "negativeInteger":int, "nonNegativeInteger":int, "nonPositiveInteger":int, "positiveInteger":int, "short":int, "unsignedByte":int, "unsignedInt":int, "unsignedLong":long, "unsignedShort":int} for tag, type in integer_type_mapping.items(): client = tests.client_from_wsdl(tests.wsdl_output("""\ <xsd:element name="value" type="xsd:%s" />""" % tag, "value")) response = client.service.f(__inject=dict(reply=suds.byte_str("""\ <?xml version="1.0"?> <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <value xmlns="my-namespace">15</value> </env:Body> </env:Envelope>"""))) assert response.__class__ is type assert response == 15 boolean_mapping = {"0":False, "1":True, "false":False, "true":True} client = tests.client_from_wsdl(tests.wsdl_output("""\ <xsd:element name="value" type="xsd:boolean" />""", "value")) for value, expected_value in boolean_mapping.items(): response = client.service.f(__inject=dict(reply=suds.byte_str("""\ <?xml version="1.0"?> <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <value xmlns="my-namespace">%s</value> </env:Body> </env:Envelope>""" % value))) assert response.__class__ is bool assert response == expected_value # Suds implements no extra range checking. client = tests.client_from_wsdl(tests.wsdl_output("""\ <xsd:element name="value" type="xsd:byte" />""", "value")) response = client.service.f(__inject=dict(reply=suds.byte_str("""\ <?xml version="1.0"?> <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <value xmlns="my-namespace">1500</value> </env:Body> </env:Envelope>"""))) assert response.__class__ is int assert response == 1500 # Suds raises raw Python exceptions when it fails to convert received # response element data to its mapped Python integer data type, according # to the used WSDL schema. client = tests.client_from_wsdl(tests.wsdl_output("""\ <xsd:element name="value" type="xsd:int" />""", "value")) try: client.service.f(__inject=dict(reply=suds.byte_str("""\ <?xml version="1.0"?> <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <value xmlns="my-namespace">Fifteen</value> </env:Body> </env:Envelope>"""))) except ValueError, e: # ValueError instance received here has different string # representations depending on the Python version used: # Python 2.4: # "invalid literal for int(): Fifteen" # Python 2.7.3, 3.2.3: # "invalid literal for int() with base 10: 'Fifteen'" assert re.match("invalid literal for int\(\)( with base 10)?: " "('?)Fifteen\\2$", str(e))
</env:Envelope>"""))) except ValueError, e: # ValueError instance received here has different string # representations depending on the Python version used: # Python 2.4: # "invalid literal for int(): Fifteen" # Python 2.7.3, 3.2.3: # "invalid literal for int() with base 10: 'Fifteen'" assert re.match("invalid literal for int\(\)( with base 10)?: " "('?)Fifteen\\2$", str(e)) else: pytest.fail("Expected ValueError exception not raised.") # Suds returns invalid boolean values as None. invalid_boolean_values = ("True", "", "False", "2", "Fedora", "Z", "-1") client = tests.client_from_wsdl(tests.wsdl_output("""\ <xsd:element name="value" type="xsd:boolean" />""", "value")) for value in invalid_boolean_values: response = client.service.f(__inject=dict(reply=suds.byte_str("""\ <?xml version="1.0"?> <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <value xmlns="my-namespace">%s</value> </env:Body> </env:Envelope>""" % value))) assert response is None def test_disabling_automated_simple_interface_unwrapping(): client = tests.client_from_wsdl(tests.wsdl_output("""\ <xsd:element name="Wrapper"> <xsd:complexType>