def test_route__with_cors_options(self, *, web): handler = Mock() cors_options = { 'http://example.com': ResourceOptions( allow_credentials=True, expose_headers='*', allow_headers='*', max_age=300, allow_methods='*', ), } web.web_app = Mock() web._cors = Mock() web.route( pattern='/foo/', handler=handler, cors_options=cors_options, ) web.web_app.router.add_route.assert_has_calls( [call(method, '/foo/', ANY) for method in NON_OPTIONS_METHODS]) web._cors.add.assert_has_calls([ call(web.web_app.router.add_route(), _prepare_cors_options(cors_options)) for _ in NON_OPTIONS_METHODS ])
def test__prepare_cors_options(): x = ResourceOptions( allow_credentials=True, expose_headers=['foo', 'bar'], allow_headers='*', max_age=13124, allow_methods='*', ) y = ResourceOptions( allow_credentials=False, expose_headers='*', allow_headers='*', max_age=None, allow_methods=['POST'], ) z = aiohttp_cors.ResourceOptions(allow_credentials=False) opts = { 'http://bob.example.com': x, 'http://alice.example.com': y, 'http://moo.example.com': z, } aiohttp_cors_opts = _prepare_cors_options(opts) x1 = aiohttp_cors_opts['http://bob.example.com'] assert isinstance(x1, aiohttp_cors.ResourceOptions) x2 = aiohttp_cors_opts['http://alice.example.com'] assert isinstance(x2, aiohttp_cors.ResourceOptions) assert aiohttp_cors_opts['http://moo.example.com'] assert x1.allow_credentials assert x1.expose_headers == {'foo', 'bar'} assert x1.allow_headers == '*' assert x1.max_age == 13124 assert x1.allow_methods == '*' assert not x2.allow_credentials assert x2.expose_headers == '*' assert x2.allow_headers == '*' assert x2.max_age is None assert x2.allow_methods == {'POST'}
def test__prepare_cors_options(): x = ResourceOptions( allow_credentials=True, expose_headers=["foo", "bar"], allow_headers="*", max_age=13124, allow_methods="*", ) y = ResourceOptions( allow_credentials=False, expose_headers="*", allow_headers="*", max_age=None, allow_methods=["POST"], ) z = aiohttp_cors.ResourceOptions(allow_credentials=False) opts = { "http://bob.example.com": x, "http://alice.example.com": y, "http://moo.example.com": z, } aiohttp_cors_opts = _prepare_cors_options(opts) x1 = aiohttp_cors_opts["http://bob.example.com"] assert isinstance(x1, aiohttp_cors.ResourceOptions) x2 = aiohttp_cors_opts["http://alice.example.com"] assert isinstance(x2, aiohttp_cors.ResourceOptions) assert aiohttp_cors_opts["http://moo.example.com"] assert x1.allow_credentials assert x1.expose_headers == {"foo", "bar"} assert x1.allow_headers == "*" assert x1.max_age == 13124 assert x1.allow_methods == "*" assert not x2.allow_credentials assert x2.expose_headers == "*" assert x2.allow_headers == "*" assert x2.max_age is None assert x2.allow_methods == {"POST"}
def test_page__with_cors_options(self, *, app): with patch('faust.app.base.venusian') as venusian: @app.page( '/foo', cors_options={ 'http://foo.example.com': ResourceOptions( allow_credentials=True, expose_headers='*', allow_headers='*', max_age=None, allow_methods='*', ), }, ) async def view(self, request): ... assert '/foo' in app.web.views venusian.attach.assert_called_once_with(view, category=SCAN_PAGE)
def test_route__with_cors_options(self, *, web): handler = Mock() handler.get_methods = Mock(name="get_methods", return_value=set( {"GET", "PUT", "POST", "DELETE"})) cors_options = { "http://example.com": ResourceOptions( allow_credentials=True, expose_headers="*", allow_headers="*", max_age=300, allow_methods="*", ), } web.web_app = Mock() web._cors = Mock() web.route( pattern="/foo/", handler=handler, cors_options=cors_options, ) web.web_app.router.add_route.assert_has_calls( [call(method, "/foo/", ANY) for method in NON_OPTIONS_METHODS], any_order=True, ) web._cors.add.assert_has_calls( [ call(web.web_app.router.add_route(), _prepare_cors_options(cors_options)) for _ in NON_OPTIONS_METHODS ], any_order=True, )
def assert_config_equivalent(self, id='id', version=303, broker='foo://', store='bar://', cache='baz://', web='xuzzy://', web_enabled=False, autodiscover=True, origin='faust', canonical_url='http://example.com/', broker_client_id='client id', datadir=str(DATADIR), tabledir=str(TABLEDIR), processing_guarantee='exactly_once', broker_request_timeout=10000.05, broker_heartbeat_interval=101.13, broker_session_timeout=30303.30, broker_rebalancing_timeout=606606.60, broker_commit_every=202, broker_commit_interval=30.3, broker_commit_livelock_soft_timeout=60.6, broker_check_crcs=False, broker_producer='moo://', broker_consumer='zoo://', producer_partitioner=_dummy_partitioner, producer_request_timeout=2.66, producer_api_version='0.10', table_cleanup_interval=80.8, table_key_index_size=1999, key_serializer='str', value_serializer='str', table_standby_replicas=48, topic_replication_factor=16, topic_allow_declare=False, topic_disable_leader=True, reply_to='reply_to', reply_create_topic=True, reply_expires=90.9, stream_buffer_maxsize=101, stream_wait_empty=True, stream_publish_on_commit=False, stream_recovery_delay=69.3, web_bind='localhost', web_port=6069, web_host='localhost', web_transport='udp://', web_in_thread=True, web_cors_options={ # noqa: B006 'http://example.com': ResourceOptions( allow_credentials=True, expose_headers='*', allow_headers='*', max_age=3132, allow_methods='*', ), }, worker_redirect_stdouts=False, worker_redirect_stdouts_level='DEBUG', broker_max_poll_records=1000, broker_max_poll_interval=10000, timezone=pytz.timezone('US/Eastern'), logging_config={'foo': 10}, # noqa consumer_auto_offset_reset='latest', ConsumerScheduler=OtherSchedulingStrategy, **kwargs) -> App: livelock_soft_timeout = broker_commit_livelock_soft_timeout app = self.App( id, version=version, broker=broker, broker_consumer=broker_consumer, broker_producer=broker_producer, store=store, cache=cache, web=web, web_enabled=web_enabled, autodiscover=autodiscover, origin=origin, canonical_url=canonical_url, broker_client_id=broker_client_id, datadir=datadir, tabledir=tabledir, processing_guarantee=processing_guarantee, broker_request_timeout=broker_request_timeout, broker_session_timeout=broker_session_timeout, broker_rebalancing_timeout=broker_rebalancing_timeout, broker_heartbeat_interval=broker_heartbeat_interval, broker_commit_every=broker_commit_every, broker_commit_interval=broker_commit_interval, broker_commit_livelock_soft_timeout=livelock_soft_timeout, broker_check_crcs=broker_check_crcs, broker_max_poll_records=broker_max_poll_records, broker_max_poll_interval=broker_max_poll_interval, producer_partitioner=producer_partitioner, producer_request_timeout=producer_request_timeout, producer_api_version=producer_api_version, table_cleanup_interval=table_cleanup_interval, table_key_index_size=table_key_index_size, key_serializer=key_serializer, value_serializer=value_serializer, table_standby_replicas=table_standby_replicas, topic_replication_factor=topic_replication_factor, topic_allow_declare=topic_allow_declare, topic_disable_leader=topic_disable_leader, reply_to=reply_to, reply_create_topic=reply_create_topic, reply_expires=reply_expires, stream_buffer_maxsize=stream_buffer_maxsize, stream_wait_empty=stream_wait_empty, stream_publish_on_commit=stream_publish_on_commit, stream_recovery_delay=stream_recovery_delay, timezone=timezone, web_bind=web_bind, web_port=web_port, web_host=web_host, web_transport=web_transport, web_in_thread=web_in_thread, web_cors_options=web_cors_options, worker_redirect_stdouts=worker_redirect_stdouts, worker_redirect_stdouts_level=worker_redirect_stdouts_level, logging_config=logging_config, consumer_auto_offset_reset=consumer_auto_offset_reset, ConsumerScheduler=ConsumerScheduler, ) conf = app.conf assert conf.id == app.conf._prepare_id(id) assert conf.broker == [URL(broker)] assert conf.broker_consumer == [URL(broker_consumer)] assert conf.broker_producer == [URL(broker_producer)] assert conf.store == URL(str(store)) assert conf.cache == URL(str(cache)) assert conf.web == URL(str(web)) assert not conf.web_enabled assert conf.web_in_thread assert conf.autodiscover == autodiscover assert conf.canonical_url == URL(str(canonical_url)) assert conf.broker_client_id == broker_client_id assert conf.datadir == Path(str(datadir)) if Path(tabledir).is_absolute(): assert conf.tabledir == Path(str(tabledir)) else: assert conf.tabledir.relative_to(conf.appdir) == Path(tabledir) assert conf.processing_guarantee == ProcessingGuarantee.EXACTLY_ONCE assert conf.broker_request_timeout == broker_request_timeout assert conf.broker_heartbeat_interval == broker_heartbeat_interval assert conf.broker_session_timeout == broker_session_timeout assert conf.broker_rebalancing_timeout == broker_rebalancing_timeout assert conf.broker_commit_every == broker_commit_every assert conf.broker_commit_interval == broker_commit_interval assert (conf.broker_commit_livelock_soft_timeout == broker_commit_livelock_soft_timeout) assert conf.broker_check_crcs == broker_check_crcs assert conf.producer_partitioner is producer_partitioner assert conf.producer_request_timeout == producer_request_timeout assert conf.producer_api_version == producer_api_version assert conf.table_cleanup_interval == table_cleanup_interval assert conf.table_key_index_size == table_key_index_size assert conf.key_serializer == key_serializer assert conf.value_serializer == value_serializer assert conf.table_standby_replicas == table_standby_replicas assert conf.topic_replication_factor == topic_replication_factor assert conf.topic_allow_declare == topic_allow_declare assert conf.topic_disable_leader == topic_disable_leader assert conf.reply_to == reply_to assert conf.reply_expires == reply_expires assert conf.stream_buffer_maxsize == stream_buffer_maxsize assert conf.stream_wait_empty == stream_wait_empty assert conf.stream_publish_on_commit == stream_publish_on_commit assert conf.stream_recovery_delay == stream_recovery_delay assert conf.timezone is timezone assert conf.web_bind == web_bind assert conf.web_port == web_port assert conf.web_host == web_host assert conf.web_transport == URL(web_transport) assert conf.web_cors_options == web_cors_options assert conf.worker_redirect_stdouts == worker_redirect_stdouts assert (conf.worker_redirect_stdouts_level == worker_redirect_stdouts_level) assert conf.broker_max_poll_records == broker_max_poll_records assert conf.broker_max_poll_interval == broker_max_poll_interval assert conf.logging_config == logging_config assert conf.consumer_auto_offset_reset == consumer_auto_offset_reset assert conf.ConsumerScheduler is OtherSchedulingStrategy return app