本文整理汇总了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
示例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
示例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
示例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
示例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
示例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
示例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
示例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
示例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
示例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"]
示例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"
示例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"]
示例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"
示例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
示例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'
}