我的SOAP服务将使用各种无效的负载进行测试,以确认返回了适当的响应。

在进行测试之前,我想对自己的服务进行自己的测试。一些测试涉及删除必填字段。我想使用zeep模拟这些测试,但是zeep不允许我发送请求,如果我要发送的数据中没有任何必填字段,则会给我一个zeep.exceptions.ValidationError

我是否可以配置一些设置,以使zeep不会为缺少的字段引发错误并仍然发送无效的请求?

示例代码:

from zeep import Client
from datetime import datetime

wsdl_url = 'http://myservice.com/egservice?wsdl'

payload = {
    'ServiceType': 'EgService',
    'AvailabilityWindow': [
        {'StartDateTime': datetime.now(),
         'EndDateTime': datetime.now(),
         'Validation': 'VALID'}],
    'Confirmation': 'Confirmed',
    'DateTimeStamp': datetime.now()
}  # N.B No ContractID included

soap_client = Client(wsdl_url)
operation = 'myExampleOperation'
with soap_client.settings(raw_response=True):
    response = soap_client.service[operation](**payload)


wsdl的相关部分:

<xs:complexType name="EgMessage">
    <xs:sequence>
        <xs:element name="ServiceType" type="tns:EgMessage_ServiceTypeType"/>
        <xs:element name="ContractID" type="tns:EgMessage_ContractIDType"/>
        <xs:element name="AUI" type="tns:EgMessage_AUIType" minOccurs="0"/>
        <xs:element name="AvailabilityWindow" type="tns:AvailabilityWindowType" maxOccurs="unbounded"/>
        <xs:element name="Confirmation" type="tns:EgMessage_ConfirmationType"/>
        <xs:element name="FileReason" type="tns:EgMessage_FileReasonType" minOccurs="0"/>
        <xs:element name="DateTimeStamp" type="xs:dateTime"/>
</xs:sequence>
</xs:complexType>


追溯:

Traceback (most recent call last):
  File "C:/Users/Thomas.Gabereau/PycharmProjects/pas_rmq/pas_gateway/inbound/test_local_soap.py", line 157, in <module>
    status, fault_field, fault = send_to_grid(soap_client, operation, payload)
  File "C:/Users/Thomas.Gabereau/PycharmProjects/pas_rmq/pas_gateway/inbound/test_local_soap.py", line 129, in send_to_grid
    response = soap_client.service[operation](**payload)
  File "C:\Users\Thomas.Gabereau\PycharmProjects\pas_rmq\venv\lib\site-packages\zeep\proxy.py", line 45, in __call__
    kwargs,
  File "C:\Users\Thomas.Gabereau\PycharmProjects\pas_rmq\venv\lib\site-packages\zeep\wsdl\bindings\soap.py", line 119, in send
    operation, args, kwargs, client=client, options=options
  File "C:\Users\Thomas.Gabereau\PycharmProjects\pas_rmq\venv\lib\site-packages\zeep\wsdl\bindings\soap.py", line 68, in _create
    serialized = operation_obj.create(*args, **kwargs)
  File "C:\Users\Thomas.Gabereau\PycharmProjects\pas_rmq\venv\lib\site-packages\zeep\wsdl\definitions.py", line 215, in create
    return self.input.serialize(*args, **kwargs)
  File "C:\Users\Thomas.Gabereau\PycharmProjects\pas_rmq\venv\lib\site-packages\zeep\wsdl\messages\soap.py", line 74, in serialize
    self.body.render(body, body_value)
  File "C:\Users\Thomas.Gabereau\PycharmProjects\pas_rmq\venv\lib\site-packages\zeep\xsd\elements\element.py", line 231, in render
    self._render_value_item(parent, value, render_path)
  File "C:\Users\Thomas.Gabereau\PycharmProjects\pas_rmq\venv\lib\site-packages\zeep\xsd\elements\element.py", line 255, in _render_value_item
    return self.type.render(node, value, None, render_path)
  File "C:\Users\Thomas.Gabereau\PycharmProjects\pas_rmq\venv\lib\site-packages\zeep\xsd\types\complex.py", line 279, in render
    element.render(parent, element_value, child_path)
  File "C:\Users\Thomas.Gabereau\PycharmProjects\pas_rmq\venv\lib\site-packages\zeep\xsd\elements\indicators.py", line 242, in render
    element.render(parent, element_value, child_path)
  File "C:\Users\Thomas.Gabereau\PycharmProjects\pas_rmq\venv\lib\site-packages\zeep\xsd\elements\element.py", line 225, in render
    self.validate(value, render_path)
  File "C:\Users\Thomas.Gabereau\PycharmProjects\pas_rmq\venv\lib\site-packages\zeep\xsd\elements\element.py", line 280, in validate
    "Missing element %s" % (self.name), path=render_path
zeep.exceptions.ValidationError: Missing element ContractID(myExampleOperation.ContractID)

最佳答案

我遇到了同样的问题,然后找到了解决该问题的方法。有一个选项可以跳过对必填字段的验证。您必须将要跳过的属性的值设置为xsd.skipvalue。在您的方案中,在有效负载对象中将contractId的值设置为xsd.skipvalue

from zeep import xsd

payload = {
    'ServiceType': 'EgService',
    'ContractID':xsd.SkipValue,
    'AvailabilityWindow': [
        {'StartDateTime': datetime.now(),
         'EndDateTime': datetime.now(),
         'Validation': 'VALID'}],
    'Confirmation': 'Confirmed',
    'DateTimeStamp': datetime.now()
}

10-02 23:52