def test_client_with_soap_fault(self, mock_most): url = "http://localhost:9999/ws/hello" request = fixtures_dir.joinpath("hello/HelloRQ.xml").read_text() response = fixtures_dir.joinpath( "hello/HelloRS_SoapFault.xml").read_bytes() headers = {"content-type": "text/xml"} mock_most.return_value = response config = Config.from_service(HelloGetHelloAsString) serializer = XmlSerializer(config=SerializerConfig(pretty_print=True)) client = Client(config=config, serializer=serializer) result = client.send( {"body": { "get_hello_as_string": { "arg0": "chris" } }}) self.assertIsInstance(result, HelloGetHelloAsString.output) fault = HelloGetHelloAsStringOutput.Body.Fault( faultcode="S:Server", faultstring="foobar", detail=HelloGetHelloAsStringOutput.Body.Fault.Detail( hello_error=HelloError(message="foobar")), ) self.assertEqual(fault, result.body.fault) mock_most.assert_called_once_with(url, data=request, headers=headers)
def test_client(self, mock_most): url = "http://localhost:9999/ws/hello" request = fixtures_dir.joinpath("hello/HelloRQ.xml").read_text() response = fixtures_dir.joinpath("hello/HelloRS.xml").read_bytes() headers = {"content-type": "text/xml"} mock_most.return_value = response config = Config.from_service(HelloGetHelloAsString) serializer = XmlSerializer(config=SerializerConfig(pretty_print=True)) client = Client(config=config, serializer=serializer) result = client.send( {"body": { "get_hello_as_string": { "arg0": "chris" } }}) self.assertIsInstance(result, HelloGetHelloAsString.output) body = HelloGetHelloAsStringOutput.Body( get_hello_as_string_response=GetHelloAsStringResponse( return_value="Hello chris")) self.assertEqual(body, result.body) mock_most.assert_called_once_with(url, data=request, headers=headers)
def test_prepare_headers_raises_error_with_unsupported_binding_transport( self): config = Config.from_service(CalculatorSoapAdd, transport="foobar") client = Client(config=config) with self.assertRaises(ClientValueError) as cm: client.prepare_headers({}) self.assertEqual("Unsupported binding transport: `foobar`", str(cm.exception))
def test_live(self): config = Config.from_service(HelloGetHelloAsString) serializer = XmlSerializer(config=SerializerConfig(pretty_print=True)) client = Client(config=config, serializer=serializer) result = client.send( {"body": { "get_hello_as_string": { "arg0": "chris" } }}) print(result)
def test_client(self, mock_most): url = "http://www.dneonline.com/calculator.asmx" request = fixtures_dir.joinpath("calculator/AddRQ.xml").read_text() response = fixtures_dir.joinpath("calculator/AddRS.xml").read_bytes() headers = {"content-type": "text/xml", "SOAPAction": "http://tempuri.org/Add"} mock_most.return_value = response config = Config.from_service(CalculatorSoapAdd) serializer = XmlSerializer(config=SerializerConfig(pretty_print=True)) client = Client(config=config, serializer=serializer) result = client.send({"Body": {"Add": {"intA": 1, "intB": 3}}}) self.assertIsInstance(result, CalculatorSoapAddOutput) mock_most.assert_called_once_with(url, data=request, headers=headers)
def test_prepare_payload_raises_error_with_type_mismatch(self): client = Client.from_service(CalculatorSoapAdd) with self.assertRaises(ClientValueError) as cm: client.prepare_payload(CalculatorSoapAddOutput()) self.assertEqual( "Invalid input service type, expected " "`CalculatorSoapAddInput` got `CalculatorSoapAddOutput`", str(cm.exception), )
def test_from_service(self): client = Client.from_service(CalculatorSoapAdd, location="http://testurl.com") actual = asdict(client.config) expected = { "style": "document", "input": CalculatorSoapAddInput, "location": "http://testurl.com", "soap_action": "http://tempuri.org/Add", "output": CalculatorSoapAddOutput, "transport": "http://schemas.xmlsoap.org/soap/http", } self.assertEqual(expected, actual)
def test_send_with_instance_object(self, mock_post): mock_post.return_value = response.encode() client = Client.from_service(CalculatorSoapAdd) obj = CalculatorSoapAddInput(body=CalculatorSoapAddInput.Body( add=Add(3, 4))) result = client.send(obj) self.assertIsInstance(result, CalculatorSoapAddOutput) self.assertEqual(7, result.body.add_response.add_result) request = client.serializer.render(obj) mock_post.assert_called_once_with( "http://www.dneonline.com/calculator.asmx", data=request, headers={ "content-type": "text/xml", "SOAPAction": "http://tempuri.org/Add", }, )
def test_prepare_headers(self): config = Config( style="document", location="", transport=TransportTypes.SOAP, soap_action="", input=None, output=None, ) client = Client(config=config) headers = {"foo": "bar"} result = client.prepare_headers(headers) self.assertEqual({"content-type": "text/xml", "foo": "bar"}, result) self.assertEqual(1, len(headers)) config = replace(config, soap_action="add") client = Client(config=config) result = client.prepare_headers({}) self.assertEqual({ "SOAPAction": "add", "content-type": "text/xml" }, result)