本文整理汇总了Python中betamax.Betamax.register_request_matcher方法的典型用法代码示例。如果您正苦于以下问题:Python Betamax.register_request_matcher方法的具体用法?Python Betamax.register_request_matcher怎么用?Python Betamax.register_request_matcher使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类betamax.Betamax
的用法示例。
在下文中一共展示了Betamax.register_request_matcher方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: configure
# 需要导入模块: from betamax import Betamax [as 别名]
# 或者: from betamax.Betamax import register_request_matcher [as 别名]
def configure():
config = Betamax.configure()
config.cassette_library_dir = "tests/test_api/betamax/"
config.default_cassette_options['record_mode'] = 'once'
config.default_cassette_options['match_requests_on'] = ['method', 'path_matcher']
if credentials:
auth_key = 'token' if 'token' in credentials else 'password'
config.define_cassette_placeholder(
'<ZENPY-CREDENTIALS>',
str(base64.b64encode(
"{}/token:{}".format(credentials['email'], credentials[auth_key]).encode('utf-8')
))
)
session = requests.Session()
credentials['session'] = session
zenpy_client = Zenpy(**credentials)
recorder = Betamax(session=session)
class PathMatcher(URIMatcher):
"""
I use trial accounts for testing Zenpy and as such the subdomain is always changing.
This matcher ignores the netloc section of the parsed URL which prevents the tests
failing when the subdomain is changed.
"""
name = 'path_matcher'
def parse(self, uri):
parse_result = super(PathMatcher, self).parse(uri)
parse_result.pop('netloc')
return parse_result
Betamax.register_request_matcher(PathMatcher)
recorder.register_serializer(PrettyJSONSerializer)
return zenpy_client, recorder
示例2: test_register_request_matcher
# 需要导入模块: from betamax import Betamax [as 别名]
# 或者: from betamax.Betamax import register_request_matcher [as 别名]
def test_register_request_matcher(self):
class FakeMatcher(object):
name = 'fake'
Betamax.register_request_matcher(FakeMatcher)
assert 'fake' in matchers.matcher_registry
assert isinstance(matchers.matcher_registry['fake'], FakeMatcher)
示例3: next
# 需要导入模块: from betamax import Betamax [as 别名]
# 或者: from betamax.Betamax import register_request_matcher [as 别名]
first_hit = next((x for x in sequence if predicate(x)), None)
self.assertTrue(first_hit)
return first_hit
def none(self, sequence, predicate):
self.assertEqual(
None, next((x for x in sequence if predicate(x)), None))
def setUp(self):
self.configure()
def url(self, path):
return urljoin(self.r.config.permalink_url, path)
Betamax.register_request_matcher(BodyMatcher)
with Betamax.configure() as config:
if os.getenv('TRAVIS'):
config.default_cassette_options['record_mode'] = 'none'
config.cassette_library_dir = 'tests/cassettes'
config.default_cassette_options['match_requests_on'].append('PRAWBody')
def betamax(cassette_name=None, **cassette_options):
"""Utilze betamax to record/replay any network activity of the test.
The wrapped function's `betmax_init` method will be invoked if it exists.
"""
def factory(function):
@wraps(function)
示例4: SalesforceRestMatcher
# 需要导入模块: from betamax import Betamax [as 别名]
# 或者: from betamax.Betamax import register_request_matcher [as 别名]
class SalesforceRestMatcher(BaseMatcher):
name = "salesforce_rest"
excluded_query_keys = ("start", "end")
def to_dict(self, query):
query_dict = parse_qs(query or "") # Protect against None
return dict(filter(lambda i: i[0] not in self.excluded_query_keys, query_dict.items()))
def match(self, request, recorded_request):
request_query = self.to_dict(urlparse(request.url).query)
recorded_query = self.to_dict(urlparse(recorded_request["uri"]).query)
return request_query == recorded_query
Betamax.register_request_matcher(SalesforceRestMatcher)
custom_object_name = primitives.capital_word(charset=string.lowercase)
with Betamax.configure() as config:
config.define_cassette_placeholder("__CUSTOM_OBJECT_NAME__", custom_object_name)
#### Fixtures ####
@pytest.yield_fixture(scope="module")
def metadata_client(request):
Client = getattr(request.module, "metadata_client_class")
client = Client(client_id, client_secret, domain, access_token)
cassette_name = ".".join((request.module.__name__, "metadata"))
示例5: helium_recorder
# 需要导入模块: from betamax import Betamax [as 别名]
# 或者: from betamax.Betamax import register_request_matcher [as 别名]
from __future__ import unicode_literals
import os
import pytest
from betamax import Betamax
from betamax_serializers import pretty_json
from betamax_matchers import json_body
import helium_commander
Betamax.register_serializer(pretty_json.PrettyJSONSerializer)
Betamax.register_request_matcher(json_body.JSONBodyMatcher)
API_TOKEN = os.environ.get('HELIUM_API_KEY', 'X' * 10)
API_URL = os.environ.get('HELIUM_API_URL', 'https://api.helium.com/v1')
RECORD_MODE = os.environ.get('HELIUM_RECORD_MODE', 'none')
RECORD_FOLDER = os.environ.get('HELIUM_RECORD_FOLDER', 'tests/cassettes')
with Betamax.configure() as config:
config.cassette_library_dir = RECORD_FOLDER
record_mode = RECORD_MODE
cassette_options = config.default_cassette_options
cassette_options['record_mode'] = record_mode
cassette_options['serialize_with'] = 'prettyjson'
cassette_options['match_requests_on'].append('json-body')
config.define_cassette_placeholder('<AUTH_TOKEN>', API_TOKEN)
@pytest.fixture
def helium_recorder(request):
"""Generate and start a recorder using a helium.Client."""