本文整理汇总了Python中betamax.Betamax.configure方法的典型用法代码示例。如果您正苦于以下问题:Python Betamax.configure方法的具体用法?Python Betamax.configure怎么用?Python Betamax.configure使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类betamax.Betamax
的用法示例。
在下文中一共展示了Betamax.configure方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: configure
# 需要导入模块: from betamax import Betamax [as 别名]
# 或者: from betamax.Betamax import configure [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_preserve_exact_body_bytes
# 需要导入模块: from betamax import Betamax [as 别名]
# 或者: from betamax.Betamax import configure [as 别名]
def test_preserve_exact_body_bytes(self):
with Betamax.configure() as config:
config.preserve_exact_body_bytes = True
with Betamax(self.session) as b:
b.use_cassette('global_preserve_exact_body_bytes')
r = self.session.get('https://httpbin.org/get')
assert 'headers' in r.json()
interaction = b.current_cassette.interactions[0].json
assert 'base64_string' in interaction['response']['body']
示例3: setUpClass
# 需要导入模块: from betamax import Betamax [as 别名]
# 或者: from betamax.Betamax import configure [as 别名]
def setUpClass(cls):
cls.session = requests.Session()
cls.recorder = Betamax(cls.session)
cls.api_key = config.MAILCHIMP_API_KEY
# the subdomain to use in the api url
# is always the last 3 characters of the api key
cls.subdomain = cls.api_key[-3:]
# define the string that will be passed in the MailChimp request
# 'Authorization' header
MAILCHIMP_REQUEST_AUTH_HEADER_NAME = 'apikey'
MAILCHIMP_REQUEST_AUTH_HEADER = '{}:{}'.format(
MAILCHIMP_REQUEST_AUTH_HEADER_NAME, cls.api_key)
# create the directory to store betamax cassettes
abs_cassette_dir = os.path.join(os.path.dirname(
os.path.abspath(__file__)), cls.cassette_dir)
os.makedirs(abs_cassette_dir, exist_ok=True)
Betamax.register_serializer(pretty_json.PrettyJSONSerializer)
with Betamax.configure() as betamaxconfig:
betamaxconfig.cassette_library_dir = abs_cassette_dir
betamaxconfig.default_cassette_options['record_mode'] = 'once'
betamaxconfig.default_cassette_options[
'serialize_with'] = 'prettyjson'
betamaxconfig.default_cassette_options['match_requests_on'] = [
'method'
]
betamaxconfig.define_cassette_placeholder(
'<MAILCHIMP_AUTH_B64>',
base64.b64encode(
MAILCHIMP_REQUEST_AUTH_HEADER.encode()
).decode()
)
betamaxconfig.define_cassette_placeholder(
'<MAILCHIMP_SUBDOMAIN>',
cls.subdomain
)
# suppress these warnings (due to requests module):
# ResourceWarning: unclosed <ssl.SSLSocket
warnings.simplefilter("ignore", ResourceWarning)
示例4: configure_betamax
# 需要导入模块: from betamax import Betamax [as 别名]
# 或者: from betamax.Betamax import configure [as 别名]
def configure_betamax(api, **additional_apis):
with Betamax.configure() as config:
config.cassette_library_dir = 'tests/fixtures/cassettes'
config.match_requests_on = ['method', 'uri', 'body']
def _set_api(api, template):
for attr in ('username', 'password', 'password_hash'):
value = getattr(api, attr, None)
if value:
config.define_cassette_placeholder(template % attr.upper(), value)
# Configure primary API
config.define_cassette_placeholder('<URL>', api.url)
_set_api(api, '<%s>')
# Any additional APIs
for name, api in additional_apis.items():
template = '<' + name.upper() + '_%s>' if name else '<%s>'
_set_api(api, template)
示例5: configure_betamax
# 需要导入模块: from betamax import Betamax [as 别名]
# 或者: from betamax.Betamax import configure [as 别名]
def configure_betamax(api, **additional_apis):
with Betamax.configure() as config:
config.cassette_library_dir = 'tests/fixtures/cassettes'
config.default_cassette_options['match_options'] = ['method', 'uri', 'body']
def _set_api(api, template):
for attr in ('username', 'password', 'password_hash'):
value = getattr(api, attr, None)
if value:
config.define_cassette_placeholder(template % attr.upper(), value)
# Configure primary API
config.define_cassette_placeholder('<URL>', api.url)
_set_api(api, '<%s>')
# placeholders aren't replaced in gzipped responses
api.session.headers['Accept-Encoding'] = ''
# Any additional APIs
for name, api in additional_apis.items():
template = '<' + name.upper() + '_%s>' if name else '<%s>'
_set_api(api, template)
# placeholders aren't replaced in gzipped responses
api.session.headers['Accept-Encoding'] = ''
示例6: none
# 需要导入模块: from betamax import Betamax [as 别名]
# 或者: from betamax.Betamax import configure [as 别名]
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)
def betamax_function(obj):
示例7: setUp
# 需要导入模块: from betamax import Betamax [as 别名]
# 或者: from betamax.Betamax import configure [as 别名]
def setUp(self):
super(TestPlaceholders, self).setUp()
config = Betamax.configure()
config.define_cassette_placeholder('<AUTHORIZATION>', b64_foobar)
示例8: setUp
# 需要导入模块: from betamax import Betamax [as 别名]
# 或者: from betamax.Betamax import configure [as 别名]
def setUp(self):
with Betamax.configure() as config:
config.default_cassette_options['record_mode'] = 'none'
示例9: tearDown
# 需要导入模块: from betamax import Betamax [as 别名]
# 或者: from betamax.Betamax import configure [as 别名]
def tearDown(self):
with Betamax.configure() as config:
config.default_cassette_options['record_mode'] = 'once'