Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@fluent
def file(self, path):
"""
Defines the response body from file contents.
Arguments:
path (str): disk file path to load.
Returns:
self: ``pook.Response`` current instance.
"""
with open(path, 'r') as f:
self.body = str(f.read())
@fluent
def error(self, error):
"""
Defines a simulated exception error that will be raised.
Arguments:
error (str|Exception): error to raise.
Returns:
self: current Mock instance.
"""
self._error = RuntimeError(error) if isinstance(error, str) else error
@fluent
def times(self, times=1):
"""
Defines the TTL limit for the current mock.
The TTL number will determine the maximum number of times that the
current mock can be matched and therefore consumed.
Arguments:
times (int): TTL number. Defaults to ``1``.
Returns:
self: current Mock instance.
"""
self._times = times
@fluent
def url(self, url):
"""
Defines the mock URL to match.
It can be a full URL with path and query params.
Protocol schema is optional, defaults to ``http://``.
Arguments:
url (str): mock URL to match. E.g: ``server.com/api``.
Returns:
self: current Mock instance.
"""
self.add_matcher(matcher('URLMatcher', url))
@fluent
def type(self, name):
"""
Defines the response ``Content-Type`` header.
Alias to ``Response.content(mime)``.
You can pass one of the following type aliases instead of the full
MIME type representation:
- ``json`` = ``application/json``
- ``xml`` = ``application/xml``
- ``html`` = ``text/html``
- ``text`` = ``text/plain``
- ``urlencoded`` = ``application/x-www-form-urlencoded``
- ``form`` = ``application/x-www-form-urlencoded``
- ``form-data`` = ``application/x-www-form-urlencoded``
@fluent
def status(self, code=200):
"""
Defines the response status code.
Arguments:
code (int): response status code.
Returns:
self: ``pook.Response`` current instance.
"""
self._status = int(code)
@fluent
def param(self, name, value):
"""
Defines an URL param key and value to match.
Arguments:
name (str): param name value to match.
value (str): param name value to match.
Returns:
self: current Mock instance.
"""
self.params({name: value})
@fluent
def callback(self, *callbacks):
"""
Registers one or multiple callback that will be called every time the
current mock matches an outgoing HTTP request.
Arguments:
*callbacks (function): callback functions to call.
Returns:
self: current Mock instance.
"""
_append_funcs(self.callbacks, callbacks)
@fluent
def param(self, name, value):
"""
Defines an URL param key and value to match.
Arguments:
name (str): param name value to match.
value (str): param name value to match.
Returns:
self: current Mock instance.
"""
self.params({name: value})
@fluent
def use(self, *matchers):
"""
Adds one or multiple custom matchers instances.
Matchers must implement the following interface:
- ``.__init__(expectation)``
- ``.match(request)``
- ``.name = str``
Matchers can optionally inherit from ``pook.matchers.BaseMatcher``.
Arguments:
*matchers (pook.matchers.BaseMatcher): matchers to add.
Returns: