90 |
90 |
self.binding = self.port.binding
|
91 |
91 |
self.address = self.port.binding_options['address']
|
92 |
92 |
|
93 |
|
def soap_matcher(self, operation_name):
|
|
93 |
def soap_matcher(self, operation_name, request_check=None):
|
94 |
94 |
operation = self.binding.get(operation_name)
|
95 |
95 |
input_element_qname = operation.input.body.qname
|
96 |
96 |
|
97 |
97 |
def matcher(prepared_request):
|
98 |
98 |
doc = ET.parse(io.BytesIO(prepared_request.body))
|
99 |
99 |
if doc.find(f'.//{str(input_element_qname)}') is not None:
|
100 |
|
return True, f'Element "{str(input_element_qname)}" found'
|
|
100 |
try:
|
|
101 |
return True, f'Element "{str(input_element_qname)}" found'
|
|
102 |
finally:
|
|
103 |
if request_check:
|
|
104 |
request = operation.input.deserialize(doc.getroot())
|
|
105 |
request_check(request)
|
101 |
106 |
return False, None
|
102 |
107 |
|
103 |
108 |
return matcher
|
104 |
109 |
|
105 |
|
def add_soap_response(self, mock, operation_name, response_content):
|
|
110 |
def add_soap_response(self, mock, operation_name, response_content, request_check=None):
|
106 |
111 |
operation = self.binding.get(operation_name)
|
107 |
112 |
doc = ET.parse(io.BytesIO(response_content))
|
108 |
113 |
try:
|
... | ... | |
114 |
119 |
self.address,
|
115 |
120 |
body=response_content,
|
116 |
121 |
status=200,
|
117 |
|
match=(self.soap_matcher(operation_name),),
|
|
122 |
match=(self.soap_matcher(operation_name, request_check),),
|
118 |
123 |
)
|
119 |
124 |
|
120 |
125 |
@contextlib.contextmanager
|
121 |
126 |
def __call__(self):
|
122 |
127 |
with responses.RequestsMock() as mock:
|
123 |
128 |
mock.add(responses.GET, self.wsdl_url, body=self.wsdl_content, status=200)
|
124 |
|
mock.add_soap_response = lambda operation, response_content: self.add_soap_response(
|
125 |
|
mock, operation, response_content
|
|
129 |
mock.add_soap_response = (
|
|
130 |
lambda operation, response_content, request_check=None: self.add_soap_response(
|
|
131 |
mock,
|
|
132 |
operation,
|
|
133 |
response_content,
|
|
134 |
request_check=request_check,
|
|
135 |
)
|
126 |
136 |
)
|
127 |
137 |
yield mock
|
128 |
|
-
|