How to use the pook.matchers.base.BaseMatcher.matcher 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
    @BaseMatcher.matcher
    def match(self, req):
        url = self.expectation

        # Match as regex
        if self.regex:
            return self.compare(url, req.url.geturl(), regex_expr=True)

        # Match URL
        return all([
            self.compare(url.scheme, req.url.scheme),
            self.compare(url.hostname, req.url.hostname),
            self.compare(url.port or req.url.port, req.url.port),
            self.match_path(req),
            self.match_query(req)
        ])
github h2non / pook / pook / matchers / headers.py View on Github external
    @BaseMatcher.matcher
    def match(self, req):
        for key in self.expectation:
            # Retrieve value to match
            value = self.expectation[key]

            # Retrieve header value by key
            header = req.headers.get(key)

            # Compare header value
            print('Match:', self.compare(value, header, regex_expr=True))
            if not self.compare(value, header, regex_expr=True):
                return False

        return True
github h2non / pook / pook / matchers / xml.py View on Github external
    @BaseMatcher.matcher
    def match(self, req):
        data = req.body

        if not isinstance(data, str):
            return False

        return self.compare(data)
github h2non / pook / pook / matchers / json.py View on Github external
    @BaseMatcher.matcher
    def match(self, req):
        body = req.body

        if isinstance(body, str):
            try:
                body = json.loads(body)
            except Exception:
                return False

        x = json.dumps(self.expectation, sort_keys=True, indent=4)
        y = json.dumps(body, sort_keys=True, indent=4)

        return self.compare(x, y)
github h2non / pook / pook / matchers / body.py View on Github external
    @BaseMatcher.matcher
    def match(self, req):
        expectation = self.expectation

        # Decode bytes input
        if isinstance(expectation, bytes):
            expectation = expectation.decode('utf-8')

        return self.compare(self.expectation, req.body)
github h2non / pook / pook / matchers / json_schema.py View on Github external
    @BaseMatcher.matcher
    def match(self, req):
        body = req.body

        if isinstance(body, str):
            try:
                body = json.loads(body)
            except Exception:
                return False

        if not body:
            return False

        try:
            validate(body, self.expectation)
        except Exception:
            return False
github h2non / pook / pook / matchers / method.py View on Github external
    @BaseMatcher.matcher
    def match(self, req):
        return (self.expectation == '*' or
                self.compare(req.method.lower(), self.expectation.lower()))
github h2non / pook / pook / matchers / query.py View on Github external
    @BaseMatcher.matcher
    def match(self, req):
        query = self.expectation

        # Parse and assert type
        if isinstance(query, str):
            query = parse_qs(self.expectation)

        # Validate query params
        if not isinstance(query, dict):
            raise ValueError('query params must be a str or dict')

        # Parse request URL query
        req_query = parse_qs(req.url.query)

        # Match query params
        return self.match_query(query, req_query)
github h2non / pook / pook / matchers / path.py View on Github external
    @BaseMatcher.matcher
    def match(self, req):
        return self.compare(self.expectation, req.url.path)