def test_add_duplicate_middleware(middleware_factory): mw = middleware_factory() with pytest.raises(ValueError): manager = RequestManager(None, BaseProvider(), middlewares=[mw, mw]) manager = RequestManager(None, BaseProvider(), middlewares=[]) manager.middleware_stack.add(mw) with pytest.raises(ValueError): manager.middleware_stack.add(mw) assert tuple(manager.middleware_stack) == (mw, )
def test_middleware_holds_state_across_requests(): provider = BaseProvider() manager = RequestManager(None, provider, middlewares=[stateful_middleware]) state_a = manager.request_blocking('test_statefulness', []) assert len(state_a) == 1 state_b = manager.request_blocking('test_statefulness', []) assert id(state_a) == id(state_b) assert len(state_b) == 2
def test_provider_property_setter_and_getter(): provider_a = BaseProvider() provider_b = BaseProvider() assert provider_a is not provider_b manager = RequestManager(None, provider_a) assert manager.providers[0] is provider_a manager.providers = provider_b assert manager.providers[0] is provider_b
def test_provider_property_setter_and_getter(middleware_factory): middleware_a = middleware_factory('middleware-A') middleware_b = middleware_factory('middleware-B') provider = DummyProvider() manager = RequestManager(None, provider, middlewares=[middleware_a, middleware_b]) response = manager.request_blocking('init', ['init']) assert response['method'] == 'init|middleware-A|middleware-B' assert response['params'] == ['init', 'middleware-A', 'middleware-B'] assert response['middlewares'] == ['middleware-B', 'middleware-A']
def test_provider_property_setter_and_getter(middleware_factory): provider = BaseProvider() middleware_a = middleware_factory() middleware_b = middleware_factory() middleware_c = middleware_factory() assert middleware_a is not middleware_b assert middleware_a is not middleware_c manager = RequestManager(None, provider, middlewares=[]) assert tuple(manager.middleware_stack) == tuple() manager.middleware_stack.add(middleware_a) manager.middleware_stack.add(middleware_b) manager.middleware_stack.clear() assert tuple(manager.middleware_stack) == tuple() manager.middleware_stack.add(middleware_c) manager.middleware_stack.add(middleware_b) manager.middleware_stack.add(middleware_a) with pytest.raises(ValueError): manager.middleware_stack.add(middleware_b) assert tuple(manager.middleware_stack) == ( middleware_a, middleware_b, middleware_c, )
def test_add_named_middleware(middleware_factory): mw = middleware_factory() manager = RequestManager(None, BaseProvider(), middlewares=[(mw, 'the-name')]) assert len(manager.middleware_stack) == 1 assert tuple(manager.middleware_stack) == (mw, )
def test_bury_middleware(middleware_factory): mw1 = middleware_factory() mw2 = middleware_factory() mw3 = middleware_factory() manager = RequestManager(None, BaseProvider(), middlewares=[mw1, mw2]) manager.middleware_stack.inject(mw3, layer=0) assert tuple(manager.middleware_stack) == (mw1, mw2, mw3)
def test_add_middleware(middleware_factory): mw1 = middleware_factory() mw2 = middleware_factory() mw3 = middleware_factory() manager = RequestManager(None, BaseProvider(), middlewares=[mw1, mw2]) manager.middleware_stack.add(mw3) assert tuple(manager.middleware_stack) == (mw3, mw1, mw2)
def __init__(self, providers=empty, middlewares=None, modules=None, ens=empty): self.manager = RequestManager(self, providers, middlewares) if modules is None: modules = get_default_modules() for module_name, module_class in modules.items(): module_class.attach(self, module_name) self.ens = ens
def test_remove_middleware(middleware_factory): mw1 = middleware_factory() mw2 = middleware_factory() mw3 = middleware_factory() manager = RequestManager(None, BaseProvider(), middlewares=[mw1, mw2, mw3]) assert tuple(manager.middleware_stack) == (mw1, mw2, mw3) manager.middleware_stack.remove(mw2) assert tuple(manager.middleware_stack) == (mw1, mw3)
def test_add_named_duplicate_middleware(middleware_factory): mw = middleware_factory() manager = RequestManager(None, BaseProvider(), middlewares=[(mw, 'the-name'), (mw, 'name2')]) assert tuple(manager.middleware_stack) == (mw, mw) manager.middleware_stack.clear() assert len(manager.middleware_stack) == 0 manager.middleware_stack.add(mw, 'name1') manager.middleware_stack.add(mw, 'name2') assert tuple(manager.middleware_stack) == (mw, mw)
def test_bury_named_middleware(middleware_factory): mw1 = middleware_factory() mw2 = middleware_factory() mw3 = middleware_factory() manager = RequestManager(None, BaseProvider(), middlewares=[mw1, mw2]) manager.middleware_stack.inject(mw3, name='middleware3', layer=0) assert tuple(manager.middleware_stack) == (mw1, mw2, mw3) # make sure middleware was injected with correct name, by trying to remove # it by name. manager.middleware_stack.remove('middleware3') assert tuple(manager.middleware_stack) == (mw1, mw2)
def test_replace_middleware_without_name(middleware_factory): mw1 = middleware_factory() mw2 = middleware_factory() mw3 = middleware_factory() manager = RequestManager(None, BaseProvider(), middlewares=[mw1, mw2, mw3]) assert tuple(manager.middleware_stack) == (mw1, mw2, mw3) mw_replacement = middleware_factory() manager.middleware_stack.replace(mw2, mw_replacement) assert tuple(manager.middleware_stack) == (mw1, mw_replacement, mw3) manager.middleware_stack.remove(mw_replacement) assert tuple(manager.middleware_stack) == (mw1, mw3)