How to use the pook.regex.isregex function in pook

To help you get started, we’ve selected a few pook examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github h2non / pook / pook / matchers / url.py View on Github external
def __init__(self, url):
        if not url:
            raise ValueError('url argument cannot be empty')

        # Store original URL value
        self.url = url

        # Process as regex value
        if isregex(url):
            self.regex = True
            self.expectation = url
        else:
            # Add protocol prefix in the URL
            if not protoregex.match(url):
                self.url = 'http://{}'.format(url)
            self.expectation = urlparse(self.url)
github h2non / pook / pook / request.py View on Github external
def url(self, url):
        if isregex(url):
            self._url = url
        else:
            if not protoregex.match(url):
                url = 'http://{}'.format(url)
            self._url = urlparse(url)
            self._query = (parse_qs(self._url.query)
                           if self._url.query else self._query)
github h2non / pook / pook / engine.py View on Github external
def hostname_filter(hostname, req):
            if isregex(hostname):
                return hostname.match(req.url.hostname)
            return req.url.hostname == hostname
github h2non / pook / pook / request.py View on Github external
def rawurl(self):
        return self._url if isregex(self._url) else urlunparse(self._url)
github h2non / pook / pook / assertion.py View on Github external
"""
    Compares to values based on regular expression matching or
    strict equality comparison.

    Arguments:
        x (regex|str): string or regular expression to test.
        y (str): value to match.
        regex_expr (bool): enables regex string based expression matching.

    Raises:
        AssertionError: in case of matching error.

    Returns:
        bool
    """
    return matches(x, y, regex_expr=regex_expr) if isregex(x) else equal(x, y)
github h2non / pook / pook / assertion.py View on Github external
y (str): value to match.
        regex_expr (bool): enables regex string based expression matching.

    Raises:
        AssertionError: in case of mismatching.

    Returns:
        bool
    """
    # Parse regex expression, if needed
    x = strip_regex(x) if regex_expr and isregex_expr(x) else x

    # Run regex assertion
    if PY_3:
        # Retrieve original regex pattern
        x = x.pattern if isregex(x) else x
        # Assert regular expression via unittest matchers
        return test_case().assertRegex(y, x) or True

    # Primitive regex matching for Python 2.7
    if isinstance(x, str):
        x = re.compile(x, re.IGNORECASE)

    assert x.match(y) is not None
github h2non / pook / pook / request.py View on Github external
def __repr__(self):
        """
        Returns an human friendly readable instance data representation.

        Returns:
            str
        """
        entries = []

        entries.append('Method: {}'.format(self._method))
        entries.append('URL: {}'.format(
            self._url if isregex(self._url) else self.rawurl))

        if self._query:
            entries.append('Query: {}'.format(self._query))

        if self._headers:
            entries.append('Headers: {}'.format(self._headers))

        if self._body:
            entries.append('Body: {}'.format(self._body))

        separator = '=' * 50
        return (separator + '\n{}\n' + separator).format('\n'.join(entries))