当前位置: 首页>>代码示例>>Python>>正文


Python Betamax.stop方法代码示例

本文整理汇总了Python中betamax.Betamax.stop方法的典型用法代码示例。如果您正苦于以下问题:Python Betamax.stop方法的具体用法?Python Betamax.stop怎么用?Python Betamax.stop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在betamax.Betamax的用法示例。


在下文中一共展示了Betamax.stop方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestSnappyApiSender

# 需要导入模块: from betamax import Betamax [as 别名]
# 或者: from betamax.Betamax import stop [as 别名]
class TestSnappyApiSender(TestCase):
    def setUp(self):
        self.api_key = "dummy_key"
        self.betamax_record = "none"
        if os.environ.get("BESNAPPY_TEST_RECORD_REQUESTS"):
            if BESNAPPY_TEST_API_KEY:
                self.betamax_record = "all"
                self.api_key = BESNAPPY_TEST_API_KEY
            else:
                raise RuntimeError(
                    "BESNAPPY_TEST_RECORD_REQUESTS is set, but"
                    " BESNAPPY_TEST_API_KEY is not.")

        self.no_http_session = TestSession()

        self.betamax_session = Session()
        # We reset the Accept-Encoding header to avoid storing (opaque) gzip
        # response data.
        self.betamax_session.headers["Accept-Encoding"] = ""
        self.betamax = Betamax(
            self.betamax_session, cassette_library_dir=CASSETTE_LIBRARY_DIR)

        self.common_session = Session()
        # We reset the Accept-Encoding header to avoid storing (opaque) gzip
        # response data.
        self.common_session.headers["Accept-Encoding"] = ""
        self.betamax_common = Betamax(
            self.common_session, cassette_library_dir=CASSETTE_LIBRARY_DIR)

        self.betamax_placeholders = []
        self.common_snappy = self._get_common_snappy()

    def tearDown(self):
        self.betamax.stop()
        self.betamax_common.stop()

    def add_betamax_placeholder(self, placeholder, replace):
        placeholder_dict = {"placeholder": placeholder, "replace": replace}
        if placeholder_dict not in self.betamax_placeholders:
            self.betamax_placeholders.append(placeholder_dict)

    def snappy_for_session(self, session, api_key=None, api_url=None):
        """
        Build a ``SnappyApiSender`` instance using the provided session.
        """
        if api_key is None:
            api_key = self.api_key
        return SnappyApiSender(api_key, api_url=api_url, session=session)

    def _snappy_for_betamax(self, betamax, cassette_name, api_key, api_url):
        """
        Build a ``SnappyApiSender`` instance using the provided betamax object.
        """
        if api_key is None:
            api_key = self.api_key
        auth_header_value = basic_auth_header_value(api_key)
        self.add_betamax_placeholder("$AUTH_HEADER$", auth_header_value)
        betamax.use_cassette(
            cassette_name, record=self.betamax_record,
            serialize_with="prettyjson",
            placeholders=self.betamax_placeholders,
            match_requests_on=["method", "uri"])
        betamax.start()
        return self.snappy_for_session(betamax.session, api_key, api_url)

    def get_snappy(self, api_key=None, api_url=None):
        """
        Build a ``SnappyApiSender`` instance using the test session.
        """
        return self._snappy_for_betamax(
            self.betamax, self.id(), api_key, api_url)

    def _get_common_snappy(self, api_key=None, api_url=None):
        """
        Build a ``SnappyApiSender`` instance using the common session.

        This uses a shared betamax cassette and is suitable for requests that
        set up the environment for the test to use rather than being part of
        the test logic.
        """
        return self._snappy_for_betamax(
            self.betamax_common, "common", api_key, api_url)

    def test_api_request_url_construction_default_api_url(self):
        """
        ``._api_request()`` constructs a URL by appending the ``endpoint`` to
        the ``api_url``. In this case, we use the default API URL.
        """
        snappy = self.snappy_for_session(self.no_http_session)
        self.no_http_session.mount(
            "%s/flash" % (snappy.api_url,), TestAdapter("ok"))

        resp = snappy._api_request("GET", "flash")
        self.assertEqual(resp.content, "ok")

    def test_api_request_url_construction_custom_api_url(self):
        """
        ``._api_request()`` constructs a URL by appending the ``endpoint`` to
        the ``api_url``. In this case, we provide a custom API URL.
        """
#.........这里部分代码省略.........
开发者ID:westerncapelabs,项目名称:besnappy,代码行数:103,代码来源:test_tickets.py


注:本文中的betamax.Betamax.stop方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。