當前位置: 首頁>>代碼示例>>Python>>正文


Python requests_mock.Mocker方法代碼示例

本文整理匯總了Python中requests_mock.Mocker方法的典型用法代碼示例。如果您正苦於以下問題:Python requests_mock.Mocker方法的具體用法?Python requests_mock.Mocker怎麽用?Python requests_mock.Mocker使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在requests_mock的用法示例。


在下文中一共展示了requests_mock.Mocker方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_service_view_remote

# 需要導入模塊: import requests_mock [as 別名]
# 或者: from requests_mock import Mocker [as 別名]
def test_service_view_remote(rf, settings):
    settings.HEALTH_CHECKS = {
        'remote_service': 'https://test.com/api/healthchecks/',
    }

    with requests_mock.Mocker() as mock:
        mock.get(
            'https://test.com/api/healthchecks/',
            json={"cache_default": True})

        request = rf.get('/')
        view = views.HealthCheckServiceView()
        result = view.dispatch(request, service='remote_service')

    expected = {'cache_default': True}
    data = json.loads(result.content.decode(result.charset))

    assert result.status_code == 200
    assert result['Content-Type'] == 'application/json'
    assert data == expected 
開發者ID:mvantellingen,項目名稱:django-healthchecks,代碼行數:22,代碼來源:test_views.py

示例2: test_create_report

# 需要導入模塊: import requests_mock [as 別名]
# 或者: from requests_mock import Mocker [as 別名]
def test_create_report(settings):
    settings.HEALTH_CHECKS = {
        'database': 'django_healthchecks.contrib.check_dummy_true',
        'remote_service': 'https://test.com/api/healthchecks/',
    }

    with requests_mock.Mocker() as mock:
        mock.get(
            'https://test.com/api/healthchecks/',
            text='{"cache_default": true}')
        result, is_healthy = checker.create_report()

    expected = {
        'database': True,
        'remote_service': {'cache_default': True},
    }

    assert result == expected
    assert is_healthy is True 
開發者ID:mvantellingen,項目名稱:django-healthchecks,代碼行數:21,代碼來源:test_checker.py

示例3: test_service_timeout

# 需要導入模塊: import requests_mock [as 別名]
# 或者: from requests_mock import Mocker [as 別名]
def test_service_timeout(settings):
    settings.HEALTH_CHECKS = {
        'database': 'django_healthchecks.contrib.check_dummy_true',
        'timeout_service': 'http://timeout.com/api/healthchecks/',
    }

    with requests_mock.Mocker() as mock:
        mock.register_uri(
            'GET', 'http://timeout.com/api/healthchecks/',
            exc=requests.exceptions.Timeout)
        result, is_healthy = checker.create_report()

    expected = {
        'database': True,
        'timeout_service': False
    }

    assert result == expected
    assert is_healthy is False 
開發者ID:mvantellingen,項目名稱:django-healthchecks,代碼行數:21,代碼來源:test_checker.py

示例4: test_logoff

# 需要導入模塊: import requests_mock [as 別名]
# 或者: from requests_mock import Mocker [as 別名]
def test_logoff(self):
        """Test Session.logoff() (and also Session.is_logon())."""

        with requests_mock.Mocker() as m:

            self.mock_server_1(m)

            # Create a session in logged-off state
            session = Session('fake-host', 'fake-userid', 'fake-pw')

            session.logon()

            logged_on = session.is_logon()
            assert logged_on

            # The code to be tested:
            session.logoff()

            assert session.session_id is None
            assert session.session is None
            assert 'X-API-Session' not in session.headers
            assert len(session.headers) == 2

            logged_on = session.is_logon()
            assert not logged_on 
開發者ID:zhmcclient,項目名稱:python-zhmcclient,代碼行數:27,代碼來源:test_session.py

示例5: test_panoptes_influxdb_consumer

# 需要導入模塊: import requests_mock [as 別名]
# 或者: from requests_mock import Mocker [as 別名]
def test_panoptes_influxdb_consumer(self):
        """Test sending metrics through the InfluxDB client"""

        output_data_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), u'output/influx_line00.data')

        output_data = open(output_data_file).read()

        MockPanoptesConsumer.files = [
            u'consumers/influxdb/input/metrics_group00.json',
        ]

        with requests_mock.Mocker() as m:
            m.register_uri('POST', "http://127.0.0.1:8086/write", status_code=204)
            PanoptesInfluxDBConsumer.factory()
            self.assertEquals(m.last_request.body.decode("utf-8"), output_data)

        # Fail on first write to try _send_one_by_one
        with requests_mock.Mocker() as m:
            m.register_uri('POST', "http://127.0.0.1:8086/write", response_list=[
                {u'status_code': 400}, {u'status_code': 204}])
            PanoptesInfluxDBConsumer.factory()
            self.assertEquals(m.last_request.body.decode("utf-8"), output_data)

        # TODO: Test sending points in a single batch 
開發者ID:yahoo,項目名稱:panoptes,代碼行數:26,代碼來源:test_influxdb_consumer.py

示例6: test_balance_payload

# 需要導入模塊: import requests_mock [as 別名]
# 或者: from requests_mock import Mocker [as 別名]
def test_balance_payload(self):
        control = AntiCaptchaControl.AntiCaptchaControl(anticaptcha_key=self.anticaptcha_key_true)
        # check response type
        assert isinstance(control, AntiCaptchaControl.AntiCaptchaControl)

        with requests_mock.Mocker() as req_mock:
            req_mock.post(AntiCaptchaControl.get_balance_url, json=self.ERROR_RESPONSE_JSON)
            control.get_balance()

        history = req_mock.request_history

        assert len(history) == 1

        request_payload = history[0].json()

        # check all dict keys
        assert ["clientKey",] == list(request_payload.keys())
        assert request_payload["clientKey"] == self.anticaptcha_key_true 
開發者ID:AndreiDrang,項目名稱:python3-anticaptcha,代碼行數:20,代碼來源:test_Control.py

示例7: test_stats_payload

# 需要導入模塊: import requests_mock [as 別名]
# 或者: from requests_mock import Mocker [as 別名]
def test_stats_payload(self):
        control = AntiCaptchaControl.AntiCaptchaControl(anticaptcha_key=self.anticaptcha_key_true)
        # check response type
        assert isinstance(control, AntiCaptchaControl.AntiCaptchaControl)
        mode = random.choice(AntiCaptchaControl.mods)

        with requests_mock.Mocker() as req_mock:
            req_mock.post(AntiCaptchaControl.get_app_stats_url, json=self.ERROR_RESPONSE_JSON)
            control.get_app_stats(softId=config.app_key, mode=mode)

        history = req_mock.request_history

        assert len(history) == 1

        request_payload = history[0].json()

        # check all dict keys
        assert ["clientKey", "softId", "mode"] == list(request_payload.keys())
        assert request_payload["clientKey"] == self.anticaptcha_key_true
        assert request_payload["softId"] == config.app_key
        assert request_payload["mode"] == mode 
開發者ID:AndreiDrang,項目名稱:python3-anticaptcha,代碼行數:23,代碼來源:test_Control.py

示例8: test_complaint_image_payload

# 需要導入模塊: import requests_mock [as 別名]
# 或者: from requests_mock import Mocker [as 別名]
def test_complaint_image_payload(self):
        control = AntiCaptchaControl.AntiCaptchaControl(anticaptcha_key=self.anticaptcha_key_true)
        # check response type
        assert isinstance(control, AntiCaptchaControl.AntiCaptchaControl)
        task_id = 123456

        with requests_mock.Mocker() as req_mock:
            req_mock.post(
                AntiCaptchaControl.incorrect_imagecaptcha_url, json=self.ERROR_RESPONSE_JSON
            )
            control.complaint_on_result(
                reported_id=task_id, captcha_type=AntiCaptchaControl.complaint_types[0]
            )

        history = req_mock.request_history

        assert len(history) == 1

        request_payload = history[0].json()

        # check all dict keys
        assert ["clientKey", "taskId"] == list(request_payload.keys())
        assert request_payload["clientKey"] == self.anticaptcha_key_true
        assert request_payload["taskId"] == task_id 
開發者ID:AndreiDrang,項目名稱:python3-anticaptcha,代碼行數:26,代碼來源:test_Control.py

示例9: test_complaint_re_payload

# 需要導入模塊: import requests_mock [as 別名]
# 或者: from requests_mock import Mocker [as 別名]
def test_complaint_re_payload(self):
        control = AntiCaptchaControl.AntiCaptchaControl(anticaptcha_key=self.anticaptcha_key_true)
        # check response type
        assert isinstance(control, AntiCaptchaControl.AntiCaptchaControl)
        task_id = 123456

        with requests_mock.Mocker() as req_mock:
            req_mock.post(
                AntiCaptchaControl.incorrect_recaptcha_url, json=self.ERROR_RESPONSE_JSON
            )
            control.complaint_on_result(
                reported_id=task_id, captcha_type=AntiCaptchaControl.complaint_types[1]
            )

        history = req_mock.request_history

        assert len(history) == 1

        request_payload = history[0].json()

        # check all dict keys
        assert ["clientKey", "taskId"] == list(request_payload.keys())
        assert request_payload["clientKey"] == self.anticaptcha_key_true
        assert request_payload["taskId"] == task_id 
開發者ID:AndreiDrang,項目名稱:python3-anticaptcha,代碼行數:26,代碼來源:test_Control.py

示例10: test_get_result_payload

# 需要導入模塊: import requests_mock [as 別名]
# 或者: from requests_mock import Mocker [as 別名]
def test_get_result_payload(self):
        customcaptcha = ImageToTextTask.ImageToTextTask(anticaptcha_key=self.anticaptcha_key_fail)
        # check response type
        assert isinstance(customcaptcha, ImageToTextTask.ImageToTextTask)

        with requests_mock.Mocker() as req_mock:
            req_mock.register_uri("GET", self.image_url, json=self.VALID_RESPONSE_JSON)
            req_mock.register_uri("POST", config.create_task_url, json=self.VALID_RESPONSE_JSON)
            req_mock.register_uri(
                "POST", config.get_result_url, json=self.VALID_RESPONSE_RESULT_JSON
            )
            customcaptcha.captcha_handler(captcha_link=self.image_url)

        history = req_mock.request_history

        assert len(history) == 3

        request_payload = history[2].json()

        # check all dict keys
        assert ["clientKey", "taskId"] == list(request_payload.keys())
        assert request_payload["taskId"] == self.VALID_RESPONSE_JSON["taskId"] 
開發者ID:AndreiDrang,項目名稱:python3-anticaptcha,代碼行數:24,代碼來源:test_ImageToText.py

示例11: test_create_task_payload

# 需要導入模塊: import requests_mock [as 別名]
# 或者: from requests_mock import Mocker [as 別名]
def test_create_task_payload(self):
        customcaptcha = CustomCaptchaTask.CustomCaptchaTask(
            anticaptcha_key=self.anticaptcha_key_fail, assignment=self.CUSTOM_TASK
        )
        # check response type
        assert isinstance(customcaptcha, CustomCaptchaTask.CustomCaptchaTask)

        with requests_mock.Mocker() as req_mock:
            req_mock.post(config.create_task_url, json=self.ERROR_RESPONSE_JSON)
            customcaptcha.captcha_handler(imageUrl=self.image_url)

        history = req_mock.request_history

        assert len(history) == 1

        request_payload = history[0].json()

        # check all dict keys
        assert ["clientKey", "task", "softId"] == list(request_payload.keys())
        assert request_payload["softId"] == config.app_key
        assert ["type", "assignment", "imageUrl"] == list(request_payload["task"].keys())
        assert request_payload["task"]["type"] == "CustomCaptchaTask" 
開發者ID:AndreiDrang,項目名稱:python3-anticaptcha,代碼行數:24,代碼來源:test_CustomCaptcha.py

示例12: test_get_result_payload

# 需要導入模塊: import requests_mock [as 別名]
# 或者: from requests_mock import Mocker [as 別名]
def test_get_result_payload(self):
        customcaptcha = CustomCaptchaTask.CustomCaptchaTask(
            anticaptcha_key=self.anticaptcha_key_fail, assignment=self.CUSTOM_TASK
        )
        # check response type
        assert isinstance(customcaptcha, CustomCaptchaTask.CustomCaptchaTask)

        with requests_mock.Mocker() as req_mock:
            req_mock.register_uri("POST", config.create_task_url, json=self.VALID_RESPONSE_JSON)
            req_mock.register_uri(
                "POST", config.get_result_url, json=self.VALID_RESPONSE_RESULT_JSON
            )
            customcaptcha.captcha_handler(imageUrl=self.image_url)

        history = req_mock.request_history

        assert len(history) == 2

        request_payload = history[1].json()

        # check all dict keys
        assert ["clientKey", "taskId"] == list(request_payload.keys())
        assert request_payload["taskId"] == self.VALID_RESPONSE_JSON["taskId"] 
開發者ID:AndreiDrang,項目名稱:python3-anticaptcha,代碼行數:25,代碼來源:test_CustomCaptcha.py

示例13: test_create_task_payload

# 需要導入模塊: import requests_mock [as 別名]
# 或者: from requests_mock import Mocker [as 別名]
def test_create_task_payload(self):
        no_captcha = NoCaptchaTask.NoCaptchaTask(anticaptcha_key=self.anticaptcha_key_fail)
        # check response type
        assert isinstance(no_captcha, NoCaptchaTask.NoCaptchaTask)

        with requests_mock.Mocker() as req_mock:
            req_mock.post(config.create_task_url, json=self.ERROR_RESPONSE_JSON)
            no_captcha.captcha_handler(websiteURL=self.WEBSITE_URL, websiteKey=self.WEBSITE_KEY)

        history = req_mock.request_history

        assert len(history) == 1

        request_payload = history[0].json()

        # check all dict keys
        assert ["clientKey", "task", "softId"] == list(request_payload.keys())
        assert request_payload["softId"] == config.app_key
        assert ["type", "websiteURL", "websiteKey", 'recaptchaDataSValue'] == list(request_payload["task"].keys())
        assert request_payload["task"]["type"] == "NoCaptchaTask" 
開發者ID:AndreiDrang,項目名稱:python3-anticaptcha,代碼行數:22,代碼來源:test_NoCaptcha.py

示例14: test_download_file

# 需要導入模塊: import requests_mock [as 別名]
# 或者: from requests_mock import Mocker [as 別名]
def test_download_file(sample_zip_data, tmpdir):
    dl_path = 'http://some.url/thezipfile.zip'
    target_path = os.path.join(tmpdir.strpath, 'target.zip')

    with requests_mock.Mocker() as mock:
        # Return any size (doesn't matter, only for prints)
        mock.head(requests_mock.ANY, headers={'Content-Length': '100'})

        mock.get(dl_path, content=sample_zip_data)

        download.download_file(dl_path, target_path)

    assert os.path.isfile(target_path)

    with open(target_path, 'rb') as f:
        assert f.read() == sample_zip_data 
開發者ID:ynop,項目名稱:audiomate,代碼行數:18,代碼來源:test_download.py

示例15: test_available_files

# 需要導入模塊: import requests_mock [as 別名]
# 或者: from requests_mock import Mocker [as 別名]
def test_available_files(self, sample_response):
        with requests_mock.Mocker() as mock:
            # Return any size (doesn't matter, only for prints)
            mock.head(requests_mock.ANY, headers={'Content-Length': '100'})

            url = 'http://someurl.com/some/download/dir'
            mock.get(url, text=sample_response)
            files = voxforge.VoxforgeDownloader.available_files(url)

            assert set(files) == {
                'http://someurl.com/some/download/dir/1337ad-20170321-amr.tgz',
                'http://someurl.com/some/download/dir/1337ad-20170321-bej.tgz',
                'http://someurl.com/some/download/dir/1337ad-20170321-blf.tgz',
                'http://someurl.com/some/download/dir/1337ad-20170321-czb.tgz',
                'http://someurl.com/some/download/dir/1337ad-20170321-hii.tgz'
            } 
開發者ID:ynop,項目名稱:audiomate,代碼行數:18,代碼來源:test_voxforge.py


注:本文中的requests_mock.Mocker方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。