How to use the respx.models.ResponseTemplate function in respx

To help you get started, we’ve selected a few respx 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 lundberg / respx / respx / transports.py View on Github external
# 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
github lundberg / respx / respx / transports.py View on Github external
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
github lundberg / respx / respx / models.py View on Github external
def clone(self, context: Optional[Kwargs] = None) -> "ResponseTemplate":
        return ResponseTemplate(
            self.status_code, self._headers, self._content, context=context
        )
github lundberg / respx / respx / models.py View on Github external
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()
github lundberg / respx / respx / transports.py View on Github external
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!"