pook.matchers package

Submodules

pook.matchers.api module

class pook.matchers.api.BaseMatcher(expectation, negate=False)[source]

Bases: object

BaseMatcher implements the basic HTTP request matching interface.

compare(value, expectation, regex_expr=False)[source]

Compares two values with regular expression matching support.

Parameters:
  • value (mixed) – value to compare.

  • expectation (mixed) – value to match.

  • regex_expr (bool, optional) – enables string based regex matching.

Returns:

bool

property expectation
abstract match(request)[source]

Match performs the value matching. This is an abstract method that must be implemented by child classes.

Parameters:

request (pook.Request) – request object to match.

static matcher(fn)[source]
property name
negate = False
to_dict()[source]

Returns the current matcher representation as dictionary.

Returns:

dict

class pook.matchers.api.BodyMatcher(expectation, negate=False)[source]

Bases: BaseMatcher

BodyMatchers matches the request body via strict value comparison or regular expression based matching.

match(req)[source]

Match performs the value matching. This is an abstract method that must be implemented by child classes.

Parameters:

request (pook.Request) – request object to match.

class pook.matchers.api.HeadersMatcher(headers)[source]

Bases: BaseMatcher

Headers HTTP request matcher.

match(req)[source]

Match performs the value matching. This is an abstract method that must be implemented by child classes.

Parameters:

request (pook.Request) – request object to match.

to_comparable_value(value)[source]

Return a comparable version of value.

Parameters:

value (mixed) – the value to cast.

Returns:

str|re.Pattern|None

class pook.matchers.api.JSONMatcher(data)[source]

Bases: BaseMatcher

Match JSON documents of equivalent value.

JSON documents are matched on the structured data in the document, rather than on the strict organisation of the document.

The following two JSON snippets are treated as identical by this matcher:

{“a”: “one”, “b”: [“two”]}

… is considered idential to …

{“b”: [“two”], “a”: “one”}

In other words, the order does not matter in comparison.

Use BodyMatcher to strictly match the exact textual structure.

match(req)[source]

Match performs the value matching. This is an abstract method that must be implemented by child classes.

Parameters:

request (pook.Request) – request object to match.

class pook.matchers.api.JSONSchemaMatcher(schema)[source]

Bases: BaseMatcher

JSONSchema matcher validates a request body against a given JSONSchema definition schema.

match(req)[source]

Match performs the value matching. This is an abstract method that must be implemented by child classes.

Parameters:

request (pook.Request) – request object to match.

class pook.matchers.api.MethodMatcher(expectation, negate=False)[source]

Bases: BaseMatcher

MethodMatcher implements.

match(req)[source]

Match performs the value matching. This is an abstract method that must be implemented by child classes.

Parameters:

request (pook.Request) – request object to match.

class pook.matchers.api.PathMatcher(expectation, negate=False)[source]

Bases: BaseMatcher

PathMatcher implements an URL path matcher.

match(req)[source]

Match performs the value matching. This is an abstract method that must be implemented by child classes.

Parameters:

request (pook.Request) – request object to match.

class pook.matchers.api.QueryMatcher(expectation, negate=False)[source]

Bases: BaseMatcher

QueryMatcher implements an URL query params matcher.

match(req)[source]

Match performs the value matching. This is an abstract method that must be implemented by child classes.

Parameters:

request (pook.Request) – request object to match.

match_query(query, req_query)[source]
class pook.matchers.api.URLMatcher(url)[source]

Bases: BaseMatcher

URLMatcher implements an URL schema matcher.

match(req)[source]

Match performs the value matching. This is an abstract method that must be implemented by child classes.

Parameters:

request (pook.Request) – request object to match.

match_path(req)[source]
match_query(req)[source]
regex = False
class pook.matchers.api.XMLMatcher(data)[source]

Bases: BaseMatcher

Match XML documents of equivalent structural value.

XML documents are matched on the structured data in the document, rather than on the strict organisation of the document.

The following two XML snippets are treated as identical by this matcher:

<a value=”one”></a> <b>two</b>

… is considered idential to …

<b>two</b> <a value=”one”></a>

In other words, the order does not matter in comparison.

Use BodyMatcher to strictly match the exact textual structure.

compare(data)[source]

Compares two values with regular expression matching support.

Parameters:
  • value (mixed) – value to compare.

  • expectation (mixed) – value to match.

  • regex_expr (bool, optional) – enables string based regex matching.

Returns:

bool

match(req)[source]

Match performs the value matching. This is an abstract method that must be implemented by child classes.

Parameters:

request (pook.Request) – request object to match.

pook.matchers.api.add(*matcher)[source]

Registers one or multiple matchers to be used by default from mocking engine.

Parameters:

*matcher (list[pook.BaseMatcher]) – variadic matchers to add.

pook.matchers.api.get(name)[source]

Returns a matcher instance by class or alias name.

Parameters:

name (str) – matcher class name or alias.

Returns:

found matcher instance, otherwise None.

Return type:

matcher

pook.matchers.api.init(name, *args, **kwargs)[source]

Initializes a matcher instance passing variadic arguments to its constructor. Acts as a delegator proxy.

Parameters:
  • name (str) – matcher class name or alias to execute.

  • *args (mixed) – variadic argument

  • **kwargs (dict) – key word arguments

Returns:

matcher instance.

Return type:

matcher

Raises:

ValueError – if matcher was not found.

pook.matchers.base module

class pook.matchers.base.BaseMatcher(expectation, negate=False)[source]

Bases: object

BaseMatcher implements the basic HTTP request matching interface.

compare(value, expectation, regex_expr=False)[source]

Compares two values with regular expression matching support.

Parameters:
  • value (mixed) – value to compare.

  • expectation (mixed) – value to match.

  • regex_expr (bool, optional) – enables string based regex matching.

Returns:

bool

property expectation
abstract match(request)[source]

Match performs the value matching. This is an abstract method that must be implemented by child classes.

Parameters:

request (pook.Request) – request object to match.

static matcher(fn)[source]
property name
negate = False
to_dict()[source]

Returns the current matcher representation as dictionary.

Returns:

dict

class pook.matchers.base.ExistsMatcher(expectation, negate=False)[source]

Bases: BaseMatcher

Base class for matchers that only check for existence.

get_request_attribute(request)[source]

Retrieve attribute from the request in which existence should be checked.

match(request)[source]

Match performs the value matching. This is an abstract method that must be implemented by child classes.

Parameters:

request (pook.Request) – request object to match.

abstract property request_attr

The attribute from the request in which to check for existence of the expectation.

pook.matchers.body module

class pook.matchers.body.BodyMatcher(expectation, negate=False)[source]

Bases: BaseMatcher

BodyMatchers matches the request body via strict value comparison or regular expression based matching.

match(req)[source]

Match performs the value matching. This is an abstract method that must be implemented by child classes.

Parameters:

request (pook.Request) – request object to match.

pook.matchers.headers module

class pook.matchers.headers.HeaderExistsMatcher(expectation, negate=False)[source]

Bases: ExistsMatcher

request_attr = 'headers'
class pook.matchers.headers.HeadersMatcher(headers)[source]

Bases: BaseMatcher

Headers HTTP request matcher.

match(req)[source]

Match performs the value matching. This is an abstract method that must be implemented by child classes.

Parameters:

request (pook.Request) – request object to match.

to_comparable_value(value)[source]

Return a comparable version of value.

Parameters:

value (mixed) – the value to cast.

Returns:

str|re.Pattern|None

pook.matchers.json module

class pook.matchers.json.JSONMatcher(data)[source]

Bases: BaseMatcher

Match JSON documents of equivalent value.

JSON documents are matched on the structured data in the document, rather than on the strict organisation of the document.

The following two JSON snippets are treated as identical by this matcher:

{“a”: “one”, “b”: [“two”]}

… is considered idential to …

{“b”: [“two”], “a”: “one”}

In other words, the order does not matter in comparison.

Use BodyMatcher to strictly match the exact textual structure.

match(req)[source]

Match performs the value matching. This is an abstract method that must be implemented by child classes.

Parameters:

request (pook.Request) – request object to match.

pook.matchers.json_schema module

class pook.matchers.json_schema.JSONSchemaMatcher(schema)[source]

Bases: BaseMatcher

JSONSchema matcher validates a request body against a given JSONSchema definition schema.

match(req)[source]

Match performs the value matching. This is an abstract method that must be implemented by child classes.

Parameters:

request (pook.Request) – request object to match.

pook.matchers.method module

class pook.matchers.method.MethodMatcher(expectation, negate=False)[source]

Bases: BaseMatcher

MethodMatcher implements.

match(req)[source]

Match performs the value matching. This is an abstract method that must be implemented by child classes.

Parameters:

request (pook.Request) – request object to match.

pook.matchers.path module

class pook.matchers.path.PathMatcher(expectation, negate=False)[source]

Bases: BaseMatcher

PathMatcher implements an URL path matcher.

match(req)[source]

Match performs the value matching. This is an abstract method that must be implemented by child classes.

Parameters:

request (pook.Request) – request object to match.

pook.matchers.query module

class pook.matchers.query.QueryMatcher(expectation, negate=False)[source]

Bases: BaseMatcher

QueryMatcher implements an URL query params matcher.

match(req)[source]

Match performs the value matching. This is an abstract method that must be implemented by child classes.

Parameters:

request (pook.Request) – request object to match.

match_query(query, req_query)[source]
class pook.matchers.query.QueryParameterExistsMatcher(expectation, allow_empty, negate=False)[source]

Bases: ExistsMatcher

is_empty(value)[source]

Check for empty query parameter values.

urllib.parse.parse_qs returns a value of [‘’] for parameters that are present but without value.

match(request)[source]

Match performs the value matching. This is an abstract method that must be implemented by child classes.

Parameters:

request (pook.Request) – request object to match.

request_attr = 'query'

pook.matchers.url module

class pook.matchers.url.URLMatcher(url)[source]

Bases: BaseMatcher

URLMatcher implements an URL schema matcher.

match(req)[source]

Match performs the value matching. This is an abstract method that must be implemented by child classes.

Parameters:

request (pook.Request) – request object to match.

match_path(req)[source]
match_query(req)[source]
regex = False

pook.matchers.xml module

class pook.matchers.xml.XMLMatcher(data)[source]

Bases: BaseMatcher

Match XML documents of equivalent structural value.

XML documents are matched on the structured data in the document, rather than on the strict organisation of the document.

The following two XML snippets are treated as identical by this matcher:

<a value=”one”></a> <b>two</b>

… is considered idential to …

<b>two</b> <a value=”one”></a>

In other words, the order does not matter in comparison.

Use BodyMatcher to strictly match the exact textual structure.

compare(data)[source]

Compares two values with regular expression matching support.

Parameters:
  • value (mixed) – value to compare.

  • expectation (mixed) – value to match.

  • regex_expr (bool, optional) – enables string based regex matching.

Returns:

bool

match(req)[source]

Match performs the value matching. This is an abstract method that must be implemented by child classes.

Parameters:

request (pook.Request) – request object to match.

Module contents