Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# Pass-through request
response = None
else:
raise ValueError(
(
"Matched request pattern must return either a "
'ResponseTemplate or a Request, got "{}"'
).format(type(match))
)
if matched_pattern is None:
# Assert we always get a pattern match, if check is enabled
assert not self._assert_all_mocked, f"RESPX: {request[1]!r} not mocked!"
# Auto mock a successfull empty response
response = ResponseTemplate()
return matched_pattern, request, response
url: Optional[Union[str, Pattern]] = None,
status_code: Optional[int] = None,
content: Optional[ContentDataTypes] = None,
content_type: Optional[str] = None,
headers: Optional[HeaderTypes] = None,
pass_through: bool = False,
alias: Optional[str] = None,
) -> RequestPattern:
"""
Adds a request pattern with given mocked response details.
"""
if isinstance(method, RequestPattern):
pattern = method
else:
response = ResponseTemplate(
status_code, headers, content, content_type=content_type
)
pattern = RequestPattern(
method,
url,
response,
pass_through=pass_through,
alias=alias,
base_url=self._base_url,
)
self.patterns.append(pattern)
if pattern.alias:
self.aliases[pattern.alias] = pattern
return pattern
def clone(self, context: Optional[Kwargs] = None) -> "ResponseTemplate":
return ResponseTemplate(
self.status_code, self._headers, self._content, context=context
)
alias: Optional[str] = None,
base_url: Optional[str] = None,
) -> None:
self._match_func: Optional[Callable] = None
if callable(method):
self.method = None
self.url = None
self.pass_through = None
self._match_func = method
else:
self.method = method
self.set_url(url, base=base_url)
self.pass_through = pass_through
self.response = response or ResponseTemplate()
self.alias = alias
self.stats = asynctest.mock.MagicMock()
response: Optional[ResponseTemplate] = None
for i, pattern in enumerate(self.patterns):
match = pattern.match(request)
if not match:
continue
if matched_pattern_index is not None:
# Multiple matches found, drop and use the first one
self.patterns.pop(matched_pattern_index)
break
matched_pattern = pattern
matched_pattern_index = i
if isinstance(match, ResponseTemplate):
# Mock response
response = match
elif match == request:
# Pass-through request
response = None
else:
raise ValueError(
(
"Matched request pattern must return either a "
'ResponseTemplate or a Request, got "{}"'
).format(type(match))
)
if matched_pattern is None:
# Assert we always get a pattern match, if check is enabled
assert not self._assert_all_mocked, f"RESPX: {request[1]!r} not mocked!"